Hello,
I have two files. All urls are space seperated.
source
http://xx.yy.zz http://df.ss.sd.xz http://09.09.090.01
http://11.22.33 http://canada.xx.yy http://01.02.03.04
http://33.44.55 http://98.87.76.65 http://russia.xx.zz
http://aa.tt.xx.zz http://1w.2e.3r.4t http://china.rr.tt
readfile
canada
87.76
2e
df
Expected output
http://canada.xx.yy
http://98.87.76.65
http://1w.2e.3r.4t
http://df.ss.sd.xz
What I tried:
while read -r line
do
grep -Po '$line\K[^ ]+' source
done <readfile > output
exit 0
For this case, could you please let me know how I may get any word containing given string in each line?
Thank you
Boris
grep -of <(sed 's/.*/\\S*&\\S*/' readfile) source
1 Like
RudiC
January 25, 2019, 6:48am
3
How about - given your system / shell (which you fail to mention) provides "process substitution" -
grep -of <(sed 's/^/[^ ]*/; s/$/[^ ]*/' file2) file1
http://df.ss.sd.xz
http://canada.xx.yy
http://98.87.76.65
http://1w.2e.3r.4t
1 Like
RudiC
January 25, 2019, 6:59am
4
Your own approach wasn't too far off. Be aware that single quotes prevent the shell from expanding variables, and that you'd need to extend the regex by adding provision for a prefix (like "http://").
while read -r line; do grep -Eo "[^ ]*$line[^ ]+" file1; done <file2
http://canada.xx.yy
http://98.87.76.65
http://1w.2e.3r.4t
http://df.ss.sd.xz
1 Like
Thank You nezabudka and RudiC.
Both work as expected.
Kind regards
Boris