PySpark, BigData, SQL, Hive, AWS, Python, Unix/Linux, Shortcuts, Examples, Scripts, Perl
Showing posts with label shell scripts. Show all posts
Showing posts with label shell scripts. Show all posts
Nov 25, 2012
How to print first two columns of a file - AWK
1) It will print first line alone
>>awk 'NR==1' test.txt #1st line from the top
>>awk 'NR==7' test.txt #7th line from the top
2) How to print first line/lines of the file
>>head -8 test.txt
Labels:
awk,
command line,
scripts,
shell scripts,
unix,
unix commands
Awk Examples
1) To fetch selected columns from a command using AWK
e.g.,
>> ls -l
-rw-r----- 1 kotaprax users 203 Nov 22 12:51 new.txt
-rw-r----- 1 kotaprax users 100 Nov 22 12:51 new1.txt
-rw-r----- 1 kotaprax users 126 Nov 23 12:57 output.txt
-rw-r----- 1 kotaprax users 282 Nov 23 10:18 test.txt
-rw-r----- 1 kotaprax users 17 Nov 16 15:25 test1.txt
From the abvoe command, we are deriving some columns
>> ls -l | awk '{print $3 "\t" $8 "\t" $9}'
kotaprax 12:51 new.txt
kotaprax 12:51 new1.txt
kotaprax 12:57 output.txt
kotaprax 10:18 test.txt
kotaprax 15:25 test1.txt
2) How to print first column from the file using AWK
e.g.,
>> cat test.txt
prabhath kota
lakshmi muvvala
PRABHATH KOTA
LAKSHMI MUVVALA
>>awk '{print $1}' test.txt
(or)
>>awk < test.txt '{ print $1 }'
o/p:
>>cat test.txt
prabhath
lakshmi
PRABHATH
LAKSHMI
3) How to copy the files using AWK
>> ls
a.txt
b.txt
c.txt
It will copy file.txt to file.txt.new for all the txt files
>>ls *.txt | awk '{print "cp "$1" "$1".new"}' | sh
o/p:
>> ls
a.txt
b.txt
c.txt
a.txt.new
b.txt.new
c.txt.new
4) How to list only directories using awk
>>ls
a.txt
b.txt
c.txt
prabhath_test
o/p:
>> ls -l | grep '^d'
#Lists only directories, since directories start with
drwxr-x--- 2 kotaprax users 1024 Nov 23 13:57 prabhath_test
>>ls -l | grep -v '^d' #Lists only files but not directories
-rw-r----- 1 kotaprax users 203 Nov 22 12:51 a.txt
-rw-r----- 1 kotaprax users 282 Nov 23 10:18 b.txt
-rw-r----- 1 kotaprax users 17 Nov 16 15:25 c.txt
5) Remove files using AWK
>>ls
a.txt
b.txt
c.txt
prabhath_test
a.tt
b.tt
>> ls -l *.tt | awk '{print "rm -r "$9}' | sh
#"rm" is a command, in order to execute, "sh" is used
o/p:
>>ls
a.txt
b.txt
c.txt
prabhath_test
6) AWK - Difference between $0 and $1
>> echo "hello1 hello2 hello3" | awk ' {print $0} ' #$0 prints the whole
hello1 hello2 hello3
>> echo "hello1 hello2 hello3" | awk ' {print $3} ' #$1 prints 1st column
hello3
7) How to print 1st column of a file into another file ?
>>cat test.txt
prabhath kota
lakshmi muvvala
PRABHATH KOTA
LAKSHMI MUVVALA
>> awk '{print $1}' test.txt > output.txt
o/p:
>>cat test.txt
prabhath
lakshmi
PRABHATH
LAKSHMI
Unix Grep Examples
Grep
######
1) How to exclude lines in a grep
syntax: grep -v <pattern> <filename>
e.g.,
>> cat test.txt
prabhath
ramesh
vamsi
lakshmi muvvala
PRABHATH
LAKSHMI MUVVALA
>> grep -v "prabhath" test.txt
It will exclude the lines which contain "prabhath"
o/p:
ramesh
vamsi
lakshmi muvvala
PRABHATH
LAKSHMI MUVVALA
2) How to exclude lines in a grep with ignore case
syntax: grep -iv <pattern> <filename>
e.g.,
>> cat test.txt
prabhath
ramesh
vamsi
lakshmi muvvala
PRABHATH
LAKSHMI MUVVALA
>> grep -iv "prabhath" test.txt
It will exclude the lines which contain "prabhath", "Prabhath", "PRABAHTAH" etc.,
o/p:
ramesh
vamsi
lakshmi muvvala
LAKSHMI MUVVALA
3) How to exclude lines which contain multiple patterns at one shot
Syntax: grep -v -e <pattern> -e <pattern> <filename>
e.g.,
>> cat test.txt
prabhath
ramesh
vamsi
lakshmi muvvala
PRABHATH
LAKSHMI MUVVALA
>>grep -v -e "prabhath" -e "lakshmi" test.txt
It will exclude the lines which contains the pattern "prabhath" & pattern "lakshmi"
o/p:
ramesh
vamsi
PRABHATH
LAKSHMI MUVVALA
>>grep -iv -e "prabhath" -e "lakshmi" test.txt
Please note "i" - ignore case
It will exclude the lines which contains the pattern "prabhath", "PRABHATH" & pattern "lakshmi", "LAKSHMI"
o/p:
ramesh
vamsi
4) Counting the number of matches using grep -c
Syntax: grep -c "pattern" filename
e.g.,
>> cat test.txt
prabhath
ramesh
vamsi
lakshmi muvvala
PRABHATH
LAKSHMI MUVVALA
>>grep -c "prabhath" test.txt
1
>>grep -ic "prabhath" test.txt
2
5) Counting the number of lines that does not match the pattern
e.g.,
>> cat test.txt
prabhath
ramesh
vamsi
lakshmi muvvala
PRABHATH
LAKSHMI MUVVALA
>>grep -vc prabhath test.txt
5
(or)
>>grep -v -c prabhath test.txt
5
6) Display N lines around match (Before & After)
Syntax: > grep -C 2 "Pattern" File
e.g.,
>> cat test.txt
prabhath
ramesh
vamsi
lakshmi muvvala
PRABHATH
LAKSHMI MUVVALA
>> grep -C 2 "lakshmi" test.txt
o/p:
ramesh
vamsi
lakshmi muvvala //Actual Match
PRABHATH
LAKSHMI MUVVALA
7) Display N lines Before Match
Syntax: > grep -B 2 "Pattern" File
e.g.,
>> cat test.txt
prabhath
ramesh
vamsi
lakshmi muvvala
PRABHATH
LAKSHMI MUVVALA
>> grep -B 2 "lakshmi" test.txt
o/p:
ramesh
vamsi
lakshmi muvvala //Actual Match
8) Display N lines After Match
Syntax: >> grep -A 2 "Pattern" File
e.g.,
>> cat test.txt
prabhath
ramesh
vamsi
lakshmi muvvala
PRABHATH
LAKSHMI MUVVALA
>> grep -A 2 "lakshmi" test.txt
o/p:
lakshmi muvvala //Actaul Match
PRABHATH
LAKSHMI MUVVALA
9) Checking for full words, not for sub-strings using grep -w
e.g.,
>> cat test.txt
prabhath
prabhathkota
prabhath kota
ramesh
vamsi
lakshmi muvvala
PRABHATH
PRABHATHKOTA
PRABHATH KOTA
LAKSHMI MUVVALA
Syntax: >> grep -w "Pattern" File
>>grep -iw "kota" test.txt
o/p:
prabhath kota
PRABHATH KOTA
>>grep -i "kota" test.txt
o/p:
prabhathkota
prabhath kota
PRABHATHKOTA
PRABHATH KOTA
10) How to get line numbers in grep
>> cat test.txt
prabhath
prabhathkota
prabhath kota
ramesh
vamsi
lakshmi muvvala
PRABHATH
PRABHATHKOTA
PRABHATH KOTA
LAKSHMI MUVVALA
>> grep -n 'PRABHATH' test.txt
o/p:
7 PRABHATH
8 PRABHATHKOTA
9 PRABHATH KOTA
>> grep -in 'PRABHATH' test.txt
o/p:
1 prabhath
2 prabhathkota
3 prabhath kota
7 PRABHATH
8 PRABHATHKOTA
9 PRABHATH KOTA
11) Grep Pipe Examples
1) Display cpu model name
#With Pipe
]# cat /proc/cpuinfo | grep -i 'Model'
#Without Pipe
]# grep -i 'Model' /proc/cpuinfo
2) grep dbname *.dbl | grep mysql
12) Some complex examples using Grep...
>> cat test.txt
prabhath
prabhathkota
prabhath kota
ramesh
vamsi
lakshmi muvvala
PRABHATH
PRABHATHKOTA
PRABHATH KOTA
LAKSHMI MUVVALA
>> grep -nC 2 'lakshmi' test.txt
Note: "-C 2" will grep for 2 lines above and 2 lines below
"-n" will give the line numbers
o/p:
4 ramesh
5 vamsi
6 lakshmi muvvala
7 PRABHATH
8 PRABHATHKOTA
13) Grep OR
1) grep 'pattern1\|pattern2' filename
$ cat employee.txt
100 Thomas Manager Sales $5,000
200 Jason Developer Technology $5,500
300 Raj Sysadmin Technology $7,000
400 Nisha Manager Marketing $9,500
500 Randy Manager Sales $6,000
$ grep 'Tech\|Sales' employee.txt
100 Thomas Manager Sales $5,000
200 Jason Developer Technology $5,500
300 Raj Sysadmin Technology $7,000
500 Randy Manager Sales $6,000
2) grep -E 'pattern1|pattern2' filename
$ grep -E 'Tech|Sales' employee.txt
100 Thomas Manager Sales $5,000
200 Jason Developer Technology $5,500
300 Raj Sysadmin Technology $7,000
500 Randy Manager Sales $6,000
3) egrep 'pattern1|pattern2' filename
$ egrep 'Tech|Sales' employee.txt
100 Thomas Manager Sales $5,000
200 Jason Developer Technology $5,500
300 Raj Sysadmin Technology $7,000
500 Randy Manager Sales $6,000
14) Grep AND
grep -E 'pattern1.*pattern2' filename
grep -E 'pattern1.*pattern2|pattern2.*
$ grep -E 'Dev.*Tech' employee.txt
200 Jason Developer Technology $5,500
The following example will grep all the lines that contain both “Manager” and “Sales” in it (in any order).
$ grep -E 'Manager.*Sales|Sales.*
15) Grep NOT
grep -v 'pattern1' filename
$ grep -v Sales employee.txt
200 Jason Developer Technology $5,500
300 Raj Sysadmin Technology $7,000
400 Nisha Manager Marketing $9,500
$ egrep 'Manager|Developer' employee.txt | grep -v Sales
200 Jason Developer Technology $5,500
400 Nisha Manager Marketing $9,500
16) Highlighting the search using GREP_OPTIONS
$ export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8'
$ grep this demo_file
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.
17) Show only the matched string
#] grep -o
$ grep -o "is.*line" demo_file
is line is the 1st lower case line
is line
is is the last line
Subscribe to:
Posts (Atom)