Reputation: 47
I have file1 containing some text, like:
abcdef 123456 abcdef
ghijkl 789123 abcdef
mnopqr 123456 abcdef
and I have file2 containing single line of text which I want to use as pattern:
ghijkl 789123
How can I use second file as a pattern to print lines containing it to third file using sed? like file3:
ghijkl 789123 abcdef
I've tried to use
sed -ne "s/r file2//p" file1 > file3
But the content of file3 is blank for some reason P.S. using Windows
Upvotes: 1
Views: 1261
Reputation: 654
This is the simplest sed solution on linux: sed -n /`<file2`/p file1 > file3
, but windows does not provides backticks. So the windows work-around would be:
set /p PATERN=<file2
sed -n /%PATERN%/p file1 > file3
Upvotes: 0
Reputation: 11829
The sed
solution is:
cat f2.txt | xargs -I {} sed -n "/{}/p" f1.txt > f3.txt
but, as @Cyrus correctly notes, grep
is the proper tool for this solution and it's much nicer:
grep -f f2.txt f1.txt > f3.txt
Note: using these incredibly powerful *nix tools like sed
, grep
, cat
, xargs
, bash
, etc. on Microsoft Windows can be frustrating. Consider spinning up a Linux environment, instead -- you'll save yourself many hours of grief dealing with subtle path and environment issues from emulators like Cygwin, etc.
Upvotes: 0
Reputation: 88756
If you have sed
, do have access to grep
?
grep -f file2 file1 > file3
Upvotes: 2