Quantcast
Channel: grep pattern and the content after that, and remove others - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 2

Answer by Stephen Harris for grep pattern and the content after that, and remove others

$
0
0

sed would seem to be the right task for this:

% sed -n 's/.* \(iwantthis\) .* \(url=[^ ]*\) .*/\1 \2/p' url.txt
iwantthis url=https://www.google.com
iwantthis url=yahoo.com

How this works:

-n -- only print lines that match with a "p" command

s/.../p -- search and replace, printing lines that match

.* \(iwantthis\) .* \(url=[^ ]*\) .* -- This will look for the word "iwantthis" surrounded by spaces and remember it, and also look for "url=" followed by non-spaces, and rememeber that. The .* at each end mean that stuff before "iwantthis" and stuff after the URL are discarded.

/\1 \2 -- Replace it with the two remembered words


Viewing all articles
Browse latest Browse all 2

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>