2

I want to join the 3rd and 5th line of a file to the output of a script without trailing new lines.

Is it possible to implement this construction by using the code:

{ echo 'Some text' echo 'Some text' echo -n 'Some text' cat text cat text echo -n 'Some text' echo 'Some text' } > file 

where I just need to add some option after cat?

Suppose that 3rd and 5th line of the file text are 3 and 5. After processing it must take the form:

 Some text Some text Some text35Some text Some text 

If I understand correctly, the implementation by sed commands is terrible in my case.

6
  • 1
    Please post input samples and expected output. Commented May 26, 2016 at 20:58
  • @don_crissti : I've added. Commented May 26, 2016 at 21:04
  • @don_crissti : I've completely edited my question. Commented May 26, 2016 at 21:14
  • So... you have an empty file and you want to add text in that empty file on third line ? Does that make sense to you ? Commented May 26, 2016 at 21:21
  • 1
    Hello! I added an update to your question. If you think I have missed something or you (for what ever reason) want to revert to original - please do so. It was a rather big edit ... :) - (If so: click the edited nn mins ago and select rollback.) Commented May 26, 2016 at 22:03

1 Answer 1

2

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 
2
  • 2
    Or with awk … awk 'NR>5 {exit} NR==3 || NR==5 {printf "%s", $0}' text Commented May 26, 2016 at 21:46
  • @Runium Good point. I updated the answer with an awk solution. Commented May 27, 2016 at 18:20

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.