AWK
awk 'BEGIN {statements} {statements} END {statements}'
bash-3.00# awk 'BEGIN {i=1} {i++} END {print i}' a.txt
15
bash-3.00# wc -l a.txt
14 a.txt
bash-3.00# who
root console Apr 10 15:17 (:0)
root pts/3 Apr 10 15:20 (:0.0)
bash-3.00# who >> a.txt
bash-3.00# wc -l a.txt
16 a.txt
bash-3.00# awk 'BEGIN {i=1} {i++} END {print i}' a.txt
17
------------------
-------------------
bash-3.00# cat ttt
111 fdfdfdfddf
222 fdfdfdfdss
333 2dsdssssss
3331 rrrrrrrrrr
444 rttttttttt
5552 tewwwwwwww
bash-3.00# awk '{ if (length($1) >3) print(NR,$0) }' ttt
4 3331 rrrrrrrrrr
6 5552 tewwwwwwww
-----------------------
Syntax of running an awk program
Awk ‘program’ input file(s)
Number of columns in each row of a file
awk '{print NF}' ttt
Swap first two columns of a file and print
bash-3.00# cat ttt
111 fdfdfdfddf
222 fdfdfdfdss
333 2dsdssssss
333 rrrrrrrrrr
444 rttttttttt
555 tewwwwwwww
bash-3.00# awk '{tem=$1; $1=$2; $2=tem; print}' ttt
fdfdfdfddf 111
fdfdfdfdss 222
2dsdssssss 333
rrrrrrrrrr 333
rttttttttt 444
tewwwwwwww 555
Remove 1 column
bash-3.00# awk '{$1="";print}' ttt
fdfdfdfddf
fdfdfdfdss
2dsdssssss
rrrrrrrrrr
rttttttttt
tewwwwwwww
---------------------
$cat employee.txt
100 Thomas Manager Sales $5,000
200 Jason Developer Technology $5,500
300 Sanjay Sysadmin Technology $7,000
400 Nisha Manager Marketing $9,500
500 Randy DBA Technology $6,000
$ awk '$1 >200' employee.txt
300 Sanjay Sysadmin Technology $7,000
400 Nisha Manager Marketing $9,500
500 Randy DBA Technology $6,000
$ awk '$4 ~/Technology/' employee.txt
200 Jason Developer Technology $5,500
300 Sanjay Sysadmin Technology $7,000
500 Randy DBA Technology $6,000
------------------------
bash-3.00# awk 'BEGIN {i=1} {i++} END {print i}' a.txt
15
bash-3.00# wc -l a.txt
14 a.txt
bash-3.00# who
root console Apr 10 15:17 (:0)
root pts/3 Apr 10 15:20 (:0.0)
bash-3.00# who >> a.txt
bash-3.00# wc -l a.txt
16 a.txt
bash-3.00# awk 'BEGIN {i=1} {i++} END {print i}' a.txt
17
------------------
Skip First Two Fields and Print the Rest of Line
echo 'This is a test' | awk '{print substr($0, index($0,$3))}'
a test
Select columns 1 and 5:
cat a | awk '{print $1" "$5}'
select line 3
cat a | awk 'FNR==3{print}'
Print column with awk
ps aeu | awk '{print $2}'
-----------------------
bash-3.00# echo -e 'test1\ntest2' | awk 'BEGIN{print"START"}{print} END{print"End"}'
START
test1
test2
End
----------------
-----------------------
bash-3.00# echo -e 'test1\ntest2' | awk 'BEGIN{print"START"}{print} END{print"End"}'
START
test1
test2
End
----------------
Select 2 lines
awk '(NR==3)||(NR==5){print}' fil
select lines from 3 to 7
awk '(NR>=3)&&(NR<=7){print}' fil
----------------bash-3.00# cat ttt
111 fdfdfdfddf
222 fdfdfdfdss
333 2dsdssssss
3331 rrrrrrrrrr
444 rttttttttt
5552 tewwwwwwww
bash-3.00# awk '{ if (length($1) >3) print(NR,$0) }' ttt
4 3331 rrrrrrrrrr
6 5552 tewwwwwwww
-----------------------
Syntax of running an awk program
Awk ‘program’ input file(s)
Number of columns in each row of a file
awk '{print NF}' ttt
Swap first two columns of a file and print
bash-3.00# cat ttt
111 fdfdfdfddf
222 fdfdfdfdss
333 2dsdssssss
333 rrrrrrrrrr
444 rttttttttt
555 tewwwwwwww
bash-3.00# awk '{tem=$1; $1=$2; $2=tem; print}' ttt
fdfdfdfddf 111
fdfdfdfdss 222
2dsdssssss 333
rrrrrrrrrr 333
rttttttttt 444
tewwwwwwww 555
Remove 1 column
bash-3.00# awk '{$1="";print}' ttt
fdfdfdfddf
fdfdfdfdss
2dsdssssss
rrrrrrrrrr
rttttttttt
tewwwwwwww
---------------------
$cat employee.txt
100 Thomas Manager Sales $5,000
200 Jason Developer Technology $5,500
300 Sanjay Sysadmin Technology $7,000
400 Nisha Manager Marketing $9,500
500 Randy DBA Technology $6,000
$ awk '$1 >200' employee.txt
300 Sanjay Sysadmin Technology $7,000
400 Nisha Manager Marketing $9,500
500 Randy DBA Technology $6,000
$ awk '$4 ~/Technology/' employee.txt
200 Jason Developer Technology $5,500
300 Sanjay Sysadmin Technology $7,000
500 Randy DBA Technology $6,000
------------------------
Select columns 1 and 5:
cat a | awk '{print $1" "$5}'
select line 3
cat a | awk 'FNR==3{print}'
Print column with awk
ps aeu | awk '{print $2}'
------------------------
FNR: The ordinal number of the current record in the current file.
NR: The ordinal number of the current record from the start of input.
f1:
------------------------
FNR: The ordinal number of the current record in the current file.
NR: The ordinal number of the current record from the start of input.
f1:
a b c d
f2:
e f g
awk '{printf("file->[%s] NR->[%d] FNR->[%d] str->[%s]\n", FILENAME, NR, FNR, $0)}' f1 f2
output:
Code:
file->[f1] NR->[1] FNR->[1] str->[a] file->[f1] NR->[2] FNR->[2] str->[b] file->[f1] NR->[3] FNR->[3] str->[c] file->[f1] NR->[4] FNR->[4] str->[d] file->[f2] NR->[5] FNR->[1] str->[e] file->[f2] NR->[6] FNR->[2] str->[f] file->[f2] NR->[7] FNR->[3] str->[g]
-------------------------------
http://www.linuxnix.com/awk-scripting-learn-awk-built-in-variables-with-examples/AWK built-in variables:
- NR: Current count of the number of input records.
- NF: Keeps a count of the number of fields
- FILENAME: The name of the current input-file.
- FNR: No of records in current filename
- FS: Contains the “field separator” character
- RS: Stores the current “record separator” or Row Separator.
- OFS: Stores the “output field separator”.
- ORS: Stores the “output record separator” or Output RS.
Name: Jim Bean
Vice: Dice
ID: AFDSDFDSFDSFASFA
LoginTime: 12343314
Name: Bob Dylon
Vice: Trumpets
ID: AFD232SFDSFASFA
LoginTime: 12343314
Name: Mary Jane
Vice: Gambling
ID: EWDSFDSFASFA
LoginTime: 12343314
$ awk '{ if($2) printf("%s ", $2); else print ""; }' < dataset
Jim Dice AFDSDFDSFDSFASFA 12343314
Bob Trumpets AFD232SFDSFASFA 12343314
Mary Gambling EWDSFDSFASFA 12343314
Comments
Post a Comment