Tony
Tony

Reputation: 9571

Awk string and add text

I have an awk statement below, I'm looking to add some text both before and after each output line.

grep  "id\": \"http://mysite.com/movies/new/id/" new_id.txt | head -n1217 | awk -F/ ' { print $7 } ' | awk -F\" ' { print $1 } '

That gets me a list of the relevant ID's but I need to take each line it outputs and prepend and append some text. How can I do this, with awk? sed?

Upvotes: 0

Views: 3750

Answers (1)

Birei
Birei

Reputation: 36252

One way to prepend and append lines with GNU sed:

Assuming infile with data:

one
two
three

And following script.sed:

1,$ {
    i\  
Text prepended (line 1)\ 
Text prepended (line 2
    a\  
Text appended (line 1)\ 
Text appended (line 2)
}

Run it like:

sed -f script.sed infile

That yields:

Text prepended (line 1)
Text prepended (line 2
one
Text appended (line 1)
Text appended (line 2)
Text prepended (line 1)
Text prepended (line 2
two
Text appended (line 1)
Text appended (line 2)
Text prepended (line 1)
Text prepended (line 2
three
Text appended (line 1)
Text appended (line 2)

So, you will need to adapt it to your needs and add it to the end of your pipe chain.


EDIT: A sed one-liner:

sed -e 's/^/Text prepended\n/; s/$/\nText appended/' infile

Upvotes: 1

Related Questions