Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

How to grep for tab character(s) in Linux

Answer:

To grep for file contains tab characters, you can execute the command below:

# grep $'\t' input.txt

Only show filenames in grep command

Answer:

Sometimes you want the grep command only return the filenames that matched a pattern, you can try the following:

# grep -r -l "foo" .

The key is the -l option.

How to use sed to simulate grep -v?

Answer:

There are two methods to simulate grep -v (print out line(s) if not match) in sed.

# sed -n '/regexp/!p'

or

# sed '/regexp/d'

How to grep a string in a binary file?

Answer:

Assume you have a binary file "foo.bin", you can combine strings with the grep command.

strings foo.bin | grep bar

Remove empty lines in a file

Answer

Pick any one of the following methods.

1. grep

grep . test.txt

2. sed

sed '/^$/d' test.txt

3. awk

awk NF test.txt

4. tr

cat test.txt | tr -s "\n"

5. perl

perl -n -e 'print unless /^$/' test.txt