Using sed
As I understand it, you have a file text which is something like:
$ cat text 1 2 3 4 5
And, you want to extract the 3rd and 5th lines and put them in the middle of some echo statements. In that case, try:
$ { echo 'Some text'; echo 'Some text'; echo -n 'Some text'; echo -n "$(sed -n 3p text)$(sed -n 5p text)"; echo 'Some text'; echo 'Some text'; } >file $ cat file Some text Some text Some text35Some text Some text
Simplification
The above can be accomplished with a single echo command:
$ echo $'Some text\nSome text\nSome text'"$(sed -n 3p text)$(sed -n 5p text)"$'Some text\nSome text' >file $ cat file Some text Some text Some text35Some text Some text
Or, as a single printf command:
$ printf 'Some text\nSome text\nSome text%s%sSome text\nSome text\n' "$(sed -n 3p text)" "$(sed -n 5p text)" >file $ cat file Some text Some text Some text35Some text Some text
How the sed command works
The above uses sed commands such as:
sed -n 3p text
Here, -n is an option that tells sed not print anything unless we explicitly ask it to. 3p is a command that tells sed to print the third line. Similarly, we use 5p if we want to print the fifth line.
Using awk
An awk solution (hat tip: Runium):
awk ' BEGIN{ printf "%s","Some text\nSome text\nSome text" } NR>5 {exit} NR==3 || NR==5 {printf "%s", $0} END{ printf "%s","Some text\nSome text\n" }' text
edited nn mins agoand select rollback.)