Use the awk command to combine two lines of the same name in the following two files.
A file:
Han Hailin 21 years old
Hailin Korea 23 years old
Han Linhai 22 years old
Lin Hai Han 24 years old
B File:
Han Linhai Male
Hailin Han Nan
Han Hailin Male
Linhai Han Nan
Output effect:
Han Hailin 21-year-old male
_________________________________________________
#! /bin/bash
A= ' sed s/[[:space:]]/:/g A '
b= ' sed s/[[:space:]]/:/g B '
For I in $A
Do
Getname= ' echo $i | Awk-f ': ' {print '} '
If ["$getname" = = "Han Hailin"]
Then
Am= $getname
Am1= ' echo $i | Awk-f ': ' {print $} '
Fi
Done
For J in $B
Do
Getbname= ' echo $j | Awk-f ': ' {print '} '
If ["$getbname" = = "Han Hailin"]
Then
Bm= ' echo $j | Awk-f ': ' {print $} '
Fi
Done
echo $AM $AM 1 $BM
-----------------------------------------------------
#! /bin/bash
' Sort-n/tmp/a | awk ' {print '} ' | Uniq >/tmp/id.txt '
For ID in ' Cat/tmp/id.txt '
Do
echo "$id"
Awk-v id2= $id ' $1==id2 {print $} '/tmp/a/tmp/b #-v usage, and awk can directly process the contents of two files
Done
-----------------------------------------------------
Using external shell variables in awk
Such as:
A=44
echo "ABCD" | Awk-v get_a= $A ' {print get_a} '
Description: The-v option is used to define the parameter, which means that the value of variable A is given to get_a. How many variables need to be assigned, and how many-V options are required. Equivalent to:
To be applied to the script:
#! /bin/bash
sort-n filename |awk-f ': ' {print $} ' |uniq >id.txt
For ID in ' cat id.txt '; Do
echo "[$id]"
Awk-v id2= $id-F ': ' $1==id2 {print $} ' filename//Another way: awk-f ': ' $1== ' ' Id2 ' "{print $} ' filename
Done
Attachment:
Cat filename
1,111,111:13,443,253,456
2,222,222:13,211,222,122
1,111,111:13,643,543,544
3,333,333:12,341,243,123
2,222,222:12,123,123,123
After running the script, the result is:
[1111111]
13443253456
13643543544
[2222222]
13211222122
12123123123
[3333333]
12341243123
----------------------------------------------------------
NR, which represents the number of rows of data that awk reads after it starts executing the program.
FNR, similar to the NR function, the difference is that awk re-accumulates every new file that FNR opens from 0.
Let's look at two examples:
1, the output of NR and Fnr is the same for a single file:
# awk ' {print nr,$0} ' file1
1 A b c D
2 a B d C
3 A C b d
#awk ' {print fnr,$0} ' file1
1 A b c D
2 a B d C
3 A C b d
2, but for multiple files (NR operation on two files is constantly increasing)
# awk ' {print nr,$0} ' file1 file2
1 A b c D
2 a B d C
3 A C b d
4 AA BB cc DD
5 AA BB DD cc
6 AA cc BB DD
# awk ' {print fnr,$0} ' file1 file2 (FNR is the line number to reread per file)
1 A b c D
2 a B d C
3 A C b d
1 AA bb cc DD
2 AA BB DD cc
3 AA cc BB DD
Take a look at an example of typical applications for NR and FNR:
Case one, there are now two file formats as follows:
#cat Account
Zhang San |000001
John Doe |000002
#cat CDR
000001|10
000001|20
000002|30
000002|15
The result is to print the user name, account number and amount on the same line as follows:
Zhang San |000001|10
Zhang San |000001|20
John Doe |000002|30
John Doe |000002|15
Execute the following code
#awk-F \| ' Nr==fnr{a[$2]=$0;next}{print a[$1] ' | $ ' Account Cdr
Comments:
When the NR==FNR is true, it is judged that the first file account is currently being read, and then the {A[$2]=$0;next} loop is used to deposit each line of records in the file with array a, using the $2nd field as a subscript reference.
When NR==FNR is false, it is judged that the second file Cdr is currently read, and then skips {A[$2]=$0;next}, unconditionally executes {print a[$1] "|" On each line of the second file CDR. $ {$}, at which time the variable is the first field of the second file, and the second field in the first file is the same as when you read the first file. You can therefore use a[$1] to reference an array here.
Case two, I have the need to combine two files with the same row in the first column. For example, there are two files with the following contents:
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 result of the merge is:
1 AB AA
2 CD BB
3 AD EE
4 BD SS
5 de
The commands implemented are:
awk ' Nr==fnr{a[$1]=$2}nr>fnr{print $0,a[$1]} ' 1.txt 2.txt
-------------------------------------------------------------------------
Sed adds a number to a line at the end of a file
Sed ' s/\ (^a.*\)/\1 12/' test
#cat Test
Askdj
ASLKD aslkdjf3e
Skdjfsdfj
Sdkfjk
Fsdkfjksdjfkjsdf
12sdfesdf
ASLKDJFKASDJF ASDLFKJASKDFJ
#sed ' s/\ (^a.*\)/\1 12/' test
ASKDJ 12
ASLKD aslkdjf3e 12
Skdjfsdfj
Sdkfjk
Fsdkfjksdjfkjsdf
12sdfesdf
ASLKDJFKASDJF ASDLFKJASKDFJ 12
------------------------------------------------------------------------
In sed, uppercase is denoted with \u, \l is lowercase
1. Capitalize the first lowercase letter of each word:
Sed ' s/\b[a-z]/\u&/g ' filename
2. Capitalize all lowercase:
Sed ' s/[a-z]/\u&/g ' filename
3. Uppercase to lowercase:
Sed ' s/[a-z]/\l&/g ' filename
------------------------------------------------------------------------
There is a phone thin text file file.txt, formatted as follows
BILL 13800000000
TOM 13800000001
ABLE 13800000000
TOM 13800000003
Requirements: Names and phones can be duplicated (many-to-many), find the most occurrences of the phone number, and list the name of the person.
#! /bin/bash
Tel= ' Cat file.txt | awk ' {print $} ' | Sort-n | Uniq | Head-1 '
Echo $tel
Cat File.txt | awk ' {if ($2== "13800000000") print '} '
Shell Small Script