Tuesday, January 24, 2012

Some common usable Unix commands

Simple linux command

Find out the PID of the program on a given port

sudo lsof -i :8080 | grep LISTEN

1. grep -R "search string" "file"
2. find "filename" -type d -iname "string" ---- i is to ignore case

3. ###### to find all newlines and replace it with comma, use AWK ##########
Nice awk tutorial : http://www.savs.hcc.edu.tw/~chuavv/awk/nawk_23.html

http://bogdan.org.ua/2010/11/16/how-to-replace-newlines-with-commas-tabs-etc-merge-lines.html


awk '{print $1}' result.txt | tr '\n' ',' > sq.sql

awk uses file field to be separated by space.

if a different fs is required then
awk 'BEGIN { FS = "," } ; { print $2 }' sq.sql

4. SED ### this is to replace a particular regex with another regex
suppose a file has one line as
a,b,c,d

but i want then line by line like
a
b
c
d

command : sed 's/,/\n/g' file.txt > new.txt

so nw.txt now has
a
b
c
d

SOME EXAMPLES:
Most of the characters used in SED do not need backslashing like the below example
sed 's/-h${db_server}/-h${db_server} -P${db_port}/g' init.sh

4. TR ### this command also replace a separator with another separator
Like in the previous example new.txt has
a
b
c
d

we want out as a,b,c,d
then use awk to parse it line by line and then tr to replace

awk '{print $1}' new.txt | tr '\n' ',' > new1.txt

so new1.txt has
a,b,c,d

5. CUT ###########
a.) http://en.kioskea.net/faq/1451-sed-delete-one-or-more-lines-from-a-file
b. ) http://linux.die.net/man/1/cut

No comments:

Post a Comment