Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i print word after regex but not a similar word?

I want an awk or sed command to print the word after regexp.

I want to find the WORD after a WORD but not the WORD that looks similar.

The file looks like this:

 somethingsomething
 X-Windows-Icon=xournal
 somethingsomething
 Icon=xournal
 somethingsomething
 somethingsomething 

I want "xournal" from the one that say "Icon=xournal". This is how far i have come until now. I have tried an AWK string too but it was also unsuccessful.

cat "${file}" | grep 'Icon=' | sed 's/.*Icon=//' >> /tmp/text.txt

But i get both so the text file gives two xournal which i don't want.

like image 538
AlMehdi Avatar asked Nov 19 '25 21:11

AlMehdi


1 Answers

Use ^ to anchor the pattern at the beginning of the line. And you can even do the grepping directly within sed:

sed -n '/^Icon=/ { s/.*=//; p; }' "$file" >> /tmp/text.txt

You could also use awk, which I think reads a little better. Using = as the field separator, if field 1 is Icon then print field 2:

awk -F= '$1=="Icon" {print $2}' "$file" >> /tmp/text.txt
like image 99
John Kugelman Avatar answered Nov 22 '25 11:11

John Kugelman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!