user3631848 user3631848 - 1 year ago 87
Bash Question

Add an extra column after grep content

I understand that grep can extract the specific content from a file line by line.
Just wondering how can add another column before or after each line as an index.
For example:

grep "aaa" text.txt > tmp.txt

In the tmp.txt file, we can see the content as follows,


However, I would like to add a specific index as an extra column.
Therefore, the tmp.txt might look like this:

John aaawekjre
John qejrteraaa
John wrgeaaaere

Answer Source

try this

   grep "aaa"  text.txt | awk '{print "John " $0}' > tmp.txt
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download