Thursday, November 15, 2012

How to filter files using the sed command in Unix

If you need to filter a file and keep only those line thta contain specific chracter/string you can use grep command

but also you can use sed

sed -n 's/yes/&/p' file.txt>file2.txt

sed -n will output nothing unless the printing command is found (p). In this case sed will find those lines that contain "yes" and substitute them with the same string (&), i.e. there will be no substitute but it will print this line that is found in the output (p).

Output will be saved in file2.txt.

Nice tutorial on sed command:

No comments:

Post a Comment