开发者

how to insert a line using into a file in alphabetical order using shell script

How can i insert lines into a file using shell script such that they are inserted in alphabetical order, following would explain better.

input file contains :

1 hsdfd
2 bsdfd
3 ekksdf
.
.

i want to insert these contents into another file using shell script such that they are as below,sorted on the alphabetical order:

< root > <br>
< devices name="bsdfd" value=2 > <br>
 < details info="xxxxxxxxxxxxxxxxx"> <br>
 < details info="yyyyyyyyyyyyyyyyy"> <br>
< /devices > <br>
< devices name="ekksdf" value=3 > <br>
 < details info="xxxxxxxxxxxxxxxxx"> <br>
 < details info="yyyyyyyyyyyyyyyyy"> <br>
< /devices > <br>
< devices name="hsdfd" value=1 > <开发者_如何学编程br>
 < details info="xxxxxxxxxxxxxxxxx"> <br>
 < details info="yyyyyyyyyyyyyyyyy"> <br>
< /devices > <br>
< /root>


echo "1 hsdfd
2 bsdfd
3 ekksdf " | sort -k 2 

2 bsdfd
3 ekksdf 
1 hsdfd

So if there is a second file, and you want the result being sorted:

cat file1 file2 | sort -k 2 > sorted

updated: After your edit, I would use sed for the main work, after invoking sort:

echo "1 hsdfd
2 bsdfd
3 ekksdf" | sort -k 2  | sed -r 's/(.*) (.*)/foo \2 bar \1/;a\nyell\nprok'

foo bsdfd bar 2
nyell
prok
foo ekksdf bar 3
nyell
prok
foo hsdfd bar 1
nyell
prok

Some cosmetics with cat in the end, for footer and header. Done. :)


sort -k 2,2 input.txt > output.txt

See http://ss64.com/bash/sort.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜