Tuesday, November 22, 2011

Linux filtering and transforming text - Command Line Reference

View defined directives in a config file:

grep . -v '^#' /etc/vsftpd/vsftpd.conf

View a line matching “Initializing CPU” and 5 lines immediately after this match using 'grep' and 'sed'

grep -A 5 "Initializing CPU#1" dmesg
sed -n 101,110p /var/log/cron - Displays from Line 101 to 110 of the log file

Exclude the empty lines:

grep -v '^#' /etc/vsftpd/vsftpd.conf | grep .
grep -v '^#' /etc/ssh/sshd_config | sed -e /^$/d
grep -v '^#' /etc/ssh/sshd_config | awk /./{print}

More examples of GREP :

grep smug *.txt {search *.txt files for 'smug'}
grep BOB tmpfile
{search 'tmpfile' for 'BOB' anywhere in a line}
grep -i -w blkptr *
{search files in CWD for word blkptr, any case}
grep run[- ]time *.txt
{find 'run time' or 'run-time' in all txt files}
who | grep root
{pipe who to grep, look for root}
grep smug files
{search files for lines with 'smug'}
grep '^smug' files
{'smug' at the start of a line}
grep 'smug files
{'smug' at the end of a line}
grep '^smug files
{lines containing only 'smug'}
grep '\^s' files
{lines starting with '^s', "\" escapes the ^}
grep '[Ss]mug' files
{search for 'Smug' or 'smug'}
grep 'B[oO][bB]' files 
{search for BOB, Bob, BOb or BoB }
grep '^ files
{search for blank lines}
grep '[0-9][0-9]' file
{search for pairs of numeric digits}grep '^From: ' /usr/mail/$USER {list your mail}
grep '[a-zA-Z]'
{any line with at least one letter}
grep '[^a-zA-Z0-9]
{anything not a letter or number}
grep '[0-9]\{3\}-[0-9]\{4\}'
{999-9999, like phone numbers}
grep '^.
{lines with exactly one character}
grep '"smug"'
{'smug' within double quotes}
grep '"*smug"*'
{'smug', with or without quotes}
grep '^\.'
{any line that starts with a Period "."}
grep '^\.[a-z][a-z]'
{line start with "." and 2 lc letters}

Grep command symbols used to search files:

^ (Caret) = match expression at the start of a line, as in ^A.
$ (Question) = match expression at the end of a line, as in A$.
\ (Back Slash) = turn off the special meaning of the next character, as in \^.
[ ] (Brackets) = match any one of the enclosed characters, as in [aeiou].
Use Hyphen "-" for a range, as in [0-9].
[^ ] = match any one character except those enclosed in [ ], as in [^0-9].
. (Period) = match a single character of any value, except end of line.
* (Asterisk) = match zero or more of the preceding character or expression.
\{x,y\} = match x to y occurrences of the preceding.
\{x\} = match exactly x occurrences of the preceding.
\{x,\} = match x or more occurrences of the preceding.