File line prefix

I tried to do it myself with the following:

for i in $(grep something toprefix.log); do sed -i -e 's/^/prefix/' $i; done 

First of all, apologies for the bone - I know this does not work!

Basically, I want to apply a prefix to matching lines in a log file using grep and sed or awk.

 prefix-matching prefix-matching not-matching prefix-matching not-matching not-matching prefix-matching 

Thanks in advance.

+4
source share
2 answers
 $ sed -i '/something/s/^/prefix/' toprefix.log 

Edit: See http://www.gnu.org/software/sed/manual/html_node/Addresses.html for details

Edit: Deleted '<' since '-i' is used.

Edit: Tabs \t , therefore

 $ sed -i '/something/s/^/prefix\t/' toprefix.log 

Adds prefix and tab.

+3
source

In awk :

 awk '/something/{print "prefix\t"$0;next}1' myfile > mynewfile 
+1
source

Source: https://habr.com/ru/post/1391407/


All Articles