Tag Archives: grep

AWK / SED / GREP / FIND – some quick oneliners for unix

The below command, reads input.txt, and prints the First column of that file, into output.txt
awk -F $’\t\/’ ‘{print $1}’ input.txt > output.txt

The delimiter here is a tab followed by ‘/’ – which needs to be preceded by an escape character, which is what \ does.

The following sed command copies line numbers 795 through to 1159 to an output file.
sed -n ‘795,1159p’ input.log > /tmp/output.log

A really cool sed command below, replaces a regex with another regex using a regex 🙂
I had a case where, inside a config file I had some regex – mainly for apache redirects.
It was a huge file, and I wanted to find and replace a part of it.
Find: a-z0-9A-Z
Replace with: a-z0-9A-Z\’\+

here is what worked for me:
sed -i “s/a-z0-9A-Z/a-z0-9A-Z\\\’\\\+/g” test_file

The below grep command prints the line number for a specific “search_string”
grep -n “search_string” input.log

The below find command searches all the files within the current directory for a string that matches “PATTERN”
find . -type f -exec grep -l “PATTERN” {} \;|more

The below finds files modified in the last 2 days
find . -name “*” -mtime -2 -ls|more

Find the largest files on the current filesystem:
find . -xdev -ls | sort +6 -nr | head -20