首页 技术 正文
技术 2022年11月15日
0 收藏 651 点赞 3,510 浏览 2077 个字

1、当awk读取的文件只有两个的时候,比较常用的有三种方法
(1)awk ‘NR==FNR{…}NR>FNR{…}’ file1 file2

(2)awk ‘NR==FNR{…}NR!=FNR{…}’ file1 file2
(3)awk ‘NR==FNR{…;next}{…}’ file1 file2

next表示下一个命令不被执行

2、当awk处理的文件超过两个时,显然上面那种方法就不适用了。因为读第3个文件或以上时,也满足NR>FNR (NR!=FNR),显然无法区分开来。
所以就要用到更通用的方法了:
(1)ARGIND 当前被处理参数标志: awk ‘ARGIND==1{…}ARGIND==2{…}ARGIND==3{…}… ‘ file1 file2 file3 …
(2)ARGV 命令行参数数组: awk ‘FILENAME==ARGV[1]{…}FILENAME==ARGV[2]{…}FILENAME==ARGV[3]{…}…’ file1 file2 file3 …
(3)把文件名直接加入判断: awk ‘FILENAME==”file1″{…}FILENAME==”file2″{…}FILENAME==”file3″{…}…’ file1 file2 file3 …

举例说明,这个例子在面试的时候问过。

a.txt中有一列:

A
B
C
D
E
b.txt中有两列:

A hello world
A good morning
B what
C sad
D interview someone
D feeling bad
E not so good
E want to be happy
F fail to pass the exam
G good result
则打印出b.txt中的行,满足b.txt中第一列等于a.txt的第一列。

#! /usr/bin/kshprint "Method1:########"
awk 'ARGIND==1{test[NR]=$1}
ARGIND==2{for(i in test){if(test[i]==$1){print $0}}}' a.txt b.txtprint "\nMethod2:########"
#the '{" must at the same line of the NR!=FNR
gawk 'NR==FNR{test[NR]=$1}
NR!=FNR{
for( i in test)
{
if(test[i]==$1)
print $0
}
}' a.txt b.txtprint "\nMethod3:########"
gawk 'NR==FNR{test[NR]=$1}
NR>FNR{
for( i in test)
{
if(test[i]==$1)
print $0
}
}' a.txt b.txtprint "\nMethod4:########"
gawk 'NR==FNR{test[NR]=$1;next}
{
for( i in test)
{
if(test[i]==$1)
print $0
}
}' a.txt b.txtprint "\nMethod5:########"
gawk 'FILENAME==ARGV[1]{test[NR]=$1}
FILENAME==ARGV[2]{
for( i in test)
{
if(test[i]==$1)
print $0
}
}' a.txt b.txtprint "\nMethod6:########"
gawk 'FILENAME=="a.txt"{test[NR]=$1}
FILENAME=="b.txt"{
for( i in test)
{
if(test[i]==$1)
print $0
}
}' a.txt b.txt

输出结果为:

Method1:########
A hello world
A good morning
B what
C sad
D interview someone
D feeling bad
E not so good
E want to be happyMethod2:########
A hello world
A good morning
B what
C sad
D interview someone
D feeling bad
E not so good
E want to be happyMethod3:########
A hello world
A good morning
B what
C sad
D interview someone
D feeling bad
E not so good
E want to be happyMethod4:########
A hello world
A good morning
B what
C sad
D interview someone
D feeling bad
E not so good
E want to be happyMethod5:########
A hello world
A good morning
B what
C sad
D interview someone
D feeling bad
E not so good
E want to be happyMethod6:########
A hello world
A good morning
B what
C sad
D interview someone
D feeling bad
E not so good
E want to be happy
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289