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

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


echo "1 hsdfd2 bsdfd3 ekksdf " | sort -k 2 2 bsdfd3 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 hsdfd2 bsdfd3 ekksdf" | sort -k 2  | sed -r 's/(.*) (.*)/foo \2 bar \1/;a\nyell\nprok'foo bsdfd bar 2nyellprokfoo ekksdf bar 3nyellprokfoo hsdfd bar 1nyellprok

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