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.

Multiple separators in awk

Answer:

Sometimes when you split a string using awk, you need to support more than 1 separators, you can use the -F option.

E.g.

# cat foo.txt | awk -F "[ ?]" '{print $7}'

Now the awk split by a space and by a ?.

Extract column data using awk

Answer:

A simple awk command print the 1st column of top command

# top -bc -n1 | awk '{print $1}'

If you want to specify the field separator, you can do the following

# awk -F':' '{print $1}' /etc/passwd

Which print the 1st column of file /etc/password, as if they are split by :

How to remove BOM from UTF-8?

Answer:

# awk '{if(NR==1)sub(/^\xef\xbb\xbf/,"");print}' text.txt

Source: http://stackoverflow.com/questions/1068650/using-awk-to-remove-the-byte-order-mark

Updated: (Suggested by Van Overveldt Peter)

# tail --bytes=+4 text.txt

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