Linux20180503 Six-week fifth lesson (May 3) awk extension

 The fifth lesson in six weeks (May 3)
reviews the use of external shell variables in
extended awk http://ask.apelearn.com/question/199 

Relatively speaking, awk is more like a simple programming language 


A=44echo "ABCD" | awk -v GET_A=$A '{print GET_A}'
Description: The -v option is used to define parameters, which means that the value of variable A is assigned to GET_A.
As many variables need to be assigned, as many -v options are required. Equivalent to: Applied to a script:


image.png

image.png


Awk merges a file  http://ask.apelearn.com/question/493 
I have such a requirement, I need to merge the same lines in the first column of two files into the same line. For example, there are two files with the following content:
cat 1.txt
1 aa
2 bb
3 ee
4 ss

cat 2.txt
1 ab
2 cd
3 ad
4 bd
5 de

The combined result is:

1 ab aa
2 cd bb
3 The command implemented by ad ee
4 bd ss
5 de

is:
awk 'NR==FNR{a[$1]=$2}NR>FNR{print $0,a[$1]}' 1.txt 2.txt
image.png

This involves

Explanation: NR represents the number of lines read, and FNR represents the current number of lines read,
so in fact, NR==FNR means when 2.txt is read. Similarly, NR>FNR means that when reading 1.txt, the
array a is actually equivalent to a map connecting multiple lines of a file into one line  http://ask.apelearn.com/question/266 

The use of the gsub function in awk  http: //ask.apelearn.com/question/200 

awk intercepts multiple specified domains into one line  http://ask.apelearn.com/question/224 

Filters two or more keywords  http://ask.apelearn.com/ question/198 

Generate the following structured file with awk  http://ask.apelearn.com/question/5494 

awk print single quotes with print  http://ask.apelearn.com/question/1738 

Merge two files  http://ask .apelearn.com/question/945 

Reference tutorial for awk  http://www.cnblogs.com/emanlee/p/3327576.html 


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325280831&siteId=291194637