Linux的awk/cut命令学习


=Start=

Linux的cut命令学习
参考链接:
参考解答:
cut命令的 --complement 选项

if we use --complement as an option, cut command will display all the fields, but the specified field. {使用 --complement选项 打印除了指定列外的所有列}{complement  n.补充; 补足语; 补充物; 补集(数);}

==

$ echo a b c
a b c
$ echo a b c | cut -f 1 -d ' '
a
$ echo a b c | cut -f 1,2 -d ' '
a b
$ echo a b c | cut -f 1 -d ' ' --complement
b c
$ echo a b c | cut -d ' ' -f 2-
b c
用awk打印除了第一列/最后一列的其他列
搜索关键字:
  • Linux awk print except first column
参考链接:
参考解答:
##打印除了第一列外的所有列##
awk '{$1=""; print $0}' input.txt	# Will leave a leading space

awk '{for(i=2; i<=NF; i++) print $i}' input.txt

cut -d ' ' -f 1 --complement input.txt

cut -d ' ' -f 2- input.txt

##打印除了最后一列外的所有列##
awk '{$NF=""; print $0}' input.txt

awk '{for(i=1; i<NF; i++) print $i}' input.txt

rev input.txt | cut -f2- | rev

=EOF=

, , ,

《“Linux的awk/cut命令学习”》 有 5 条评论

  1. linux awk 内置函数详细介绍(实例)
    http://www.cnblogs.com/chengmo/archive/2010/10/08/1845913.html
    awk内置字符串函数详解
    https://www.linuxsong.org/2010/09/awk-string-function/
    https://stackoverflow.com/questions/8009664/how-to-split-a-delimited-string-into-an-array-in-awk
    `
    #需要把一些无用的字符「双引号、单引号、空格等」去掉
    $ head input.csv | awk -F’,’ ‘NR>7 {print $8, $10, $11}’
    ‘9RCED3 “张三 zhangsan 公司A”
    ’98YTC “李四 lisi”
    ‘ixyzero “王五 zero.wang”

    $ head input.csv | awk -F’,’ ‘NR>7 {print NF”\t”$11}’
    $ head input.csv | awk -F’,’ ‘NR>7 {split($11,mis,” “); print substr($8,2,15), substr($10,2,15), mis[0]}’
    $ head input.csv | awk -F’,’ ‘NR>7 {split($11,mis,” “);sub(“\””, “”, mis[1]); print substr($8,2,15), substr($10,2,15), mis[1]}’
    9RCED3 张三 zhangsan
    98YTC 李四 lisi
    ixyzero 王五 zero.wang

    awk的函数要放在前面,不能直接放在 print 语句里面,否则打印的是函数的返回值(0或1):
    split()
    substr()
    `

  2. awk中如何判断某列是否包含关键字?
    https://stackoverflow.com/questions/17001849/awk-partly-string-match-if-column-word-partly-matches
    https://www.unix.com/unix-for-advanced-and-expert-users/156232-awk-line-contains-someting-field.html
    `
    # $4 包含 John
    awk ‘$4 ~ “John” {print}’ myFile
    awk ‘$4 ~ /John/ {print}’ myFile

    # 使用 index 函数
    awk ‘index($4, “John”)’ myFile

    # $4 等于 John
    awk ‘$4 == “John” {print}’ myFile
    `

回复 hi 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注