1 Remove repeated lines
Sometimes we want to remove these repeated lines according to some column(s).
1
|
awk '!a[$2,$3]++' pushLog.log
|
Above command will print the unique lines after removing repeated in columns
2
and 3
.
For instance, there are some SAC files with same header gcarc
, and can use
the following command to achieve that.
1
|
saclst gcarc f *.SAC | sort -k2 -n -r | awk '!a[$2]++'
|
It will show the lines after removing repeated lines.
2 Print single and double quation marks: "
and '
Add back slash before double quotation marks.
1
|
awk 'BEGIN{for(i=1;i<=10;i++) {print "\""}}'
|
However, we need two single quotation marks to close \'
.
1
|
awk 'BEGIN{for(i=1;i<=10;i++) {print "'\''"}}'
|
3 Arrays
3.1 1D array
1
2
3
4
5
6
7
8
|
awk 'BEGIN{
for(i=1;i<=5;i++){
array[i] = i*2 - 1
}
for ( i in array ){
print i" = " array[i]
}
}'
|
We get output
1
2
3
4
5
|
4 = 7
5 = 9
1 = 1
2 = 3
3 = 5
|
3.2 2D array
1
2
3
4
5
6
7
8
9
10
11
12
13
|
awk 'BEGIN{
for(i=1;i<=5;i++){
for(j=1;j<=5;j++){
array[i,j] = i + j
print i" + "j" = "array[i,j]
}
}
print
for(i in array){
split(i,array2,SUBSEP)
print array2[1]" + "array2[2]" = "array[i]
}
}'
|
output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
1 + 1 = 2
1 + 2 = 3
1 + 3 = 4
2 + 1 = 3
2 + 2 = 4
2 + 3 = 5
3 + 1 = 4
3 + 2 = 5
3 + 3 = 6
3 + 3 = 6
1 + 1 = 2
2 + 1 = 3
1 + 2 = 3
3 + 1 = 4
2 + 2 = 4
1 + 3 = 4
3 + 2 = 5
2 + 3 = 5
|
Attention must be paid to the output, and the lines are unordered.
4 Use length(array)
to get length of array
1
2
3
4
5
6
7
8
9
|
awk 'BEGIN{
info="it is a test"
len=split(info,array," ")
print len, length(array)
print
for(i in array){
print i" = "array[i]
}
}'
|
output
1
2
3
4
5
6
|
4 4
4 = test
1 = it
2 = is
3 = a
|
5 Sort elements of a array
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
awk 'BEGIN{
info="it is a test"
len=split(info,array," ")
print len, length(array)
print "----not sorted----"
for(i=1;i<=len;i++){
print i" = " array[i]
}
print
for(i in array){
print i" = " array[i]
}
print "------sort-----"
for(i in array){
print i" = " array[i]
}
print "asort(array) = ",asort(array)
for(i=1;i<=len;i++){
print i" = "array[i]
}
}'
|
output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
4 4
----not sorted----
1 = it
2 = is
3 = a
4 = test
4 = test
1 = it
2 = is
3 = a
------sort-----
4 = test
1 = it
2 = is
3 = a
asort(array) = 4
1 = a
2 = is
3 = it
4 = test
|
6 Index of array
1
2
3
4
5
6
7
8
9
10
11
12
|
awk 'BEGIN{
array[1]="a"
array[2]="b"
array[3]="c"
array[4]="d"
array["first"]=1
array["second"]=2
array["three"]=3
array["four"]=4
print array[4],array["four"]
print array[2],array["second"]
}'
|
output
It’s like the direcionary in python, is it?