text:
Hello Chicago!
Hello New York!
Hello Los Angeles!
script_1:
#! /bin/bash
awk '{ C+= length($0) + 1; W+=NF;} END { print NR,W,C;}' text
#output:
#3 8 50
#For each record, it will execute the given action, which
#track the number of character: "C", number of words(fields): "W"
#In the end, we output the record number which is also the number
#of lines. We also output W and C, which is number of words and
#characters.
2. Sink the data
text is same as above
script_1:
#! /bin/bash
cat text >/dev/null
#We use cat to "sink" the data.
awk '' text
#same effect with awk, since for each record, we let
#awk do nothing
3. Output the content of given file
text is same as above
terminal:
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ awk '1' text
Hello Chicago!
Hello New York!
Hello Los Angeles!
4. Output Calculation result of specific column
text2:
Hello 5
world 8
Hello 12
Chicago 15
terminal:
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ awk '{ print $2, log($2); }' text2
5 1.60944
8 2.07944
12 2.48491
15 2.70805
5. output some percentage of lines of records
text2:
1 Hello
2 World
3 Hello
4 Chicago!
5 Hello
6 New York!
7 Hello
8 World
9 Hello
10 Chicago!
11 Hello
12 New York!
13 Hello
14 World
15 Hello
16 Chicago!
17 Hello
18 New York!
terminal:
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ awk 'rand()<0.3' text2
9 Hello
12 New York!
15 Hello
We are outputting about 30% of lines of records from text2
6. Output sum of specified column records
text2:
1 Hello
2 World
3 Hello
4 Chicago!
5 Hello
terminal:
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ awk -v col=1 '{ sum+=$col;} END { print sum;}' text2
15
No comments:
Post a Comment