Linux awk one liner 根据列的值仅选择行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13364153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:47:04  来源:igfitidea点击:

awk one liner select only rows based on value of a column

linuxunixawk

提问by Dnaiel

I'd like to read filein.txt(tab delimited) and output a fileout.txtwith only rows that match the value of a given column, and eliminate the column being queried. i.e.,

我想读取filein.txt(制表符分隔)并输出一个fileout.txt,其中仅包含与给定列的值匹配的行,并消除被查询的列。IE,

filein.txt
#name\thouse\taddress
roger\tvictorian\t223 dolan st.
maggie\tfrench\t12 alameda ave.
kingston\tvictorian\t224 house st.
robert\tamerican\t22 dolan st.

Let us say I'd like to select only the rows where the houses are of victorianstyle, then my fileout.txtshould look like:

假设我只想选择房屋victorian风格的行,那么我的fileout.txt应该如下所示:

fileout.txt
#name\taddress
roger\t223 dolan st.
kingston\t224 house st.

采纳答案by Kevin

awk -F"\t" ' == "victorian" { print "\t" }' file.in

回答by sampson-chen

You can do it with the following awkscript:

您可以使用以下awk脚本执行此操作:

#!/bin/bash

style="victorian"
awk -v s_style=$style 'BEGIN{FS=OFS="\t"}
    ==s_style {=""; sub("\t\t","\t"); print}'

Explanation:

解释:

  • style="victorian": assign the house style that you want to select outside of the awkscript so it's easier to maintain
  • awk: invoke awk
  • -v s_style=$style: the -voption passes an external variable into awk. Need to specify this for each variable you pass in. In this case it assigns the external variable $styleto the awk variable s_style.
  • BEGIN{FS=OFS="\t"}: tells awk that the field separators in the output should be tabs, not spaces by default.
  • {$2==s_style {$2=""; sub("\t\t","\t"); print}}': If the 2nd field is the house type specified in s_style(in this case, victorian), then remove it and print the line.
  • style="victorian":在awk脚本之外指定要选择的房屋样式,以便更易于维护
  • awk: 调用awk
  • -v s_style=$style: 该-v选项将外部变量传递给 awk。需要为您传入的每个变量指定此项。在这种情况下,它将外部变量分配$style给 awk 变量s_style
  • BEGIN{FS=OFS="\t"}: 告诉 awk 输出中的字段分隔符应该是制表符,而不是默认的空格。
  • {$2==s_style {$2=""; sub("\t\t","\t"); print}}':如果第二个字段是s_style(在本例中为victorian)中指定的房屋类型,则将其删除并打印该行。

Alternatively, you could do:

或者,你可以这样做:

#!/bin/bash

style="victorian"
awk -v s_style=$style 'BEGIN{FS=OFS="\t"}
    ==s_style {print , }'

but this assumes that your input files will not have additional fields separated by tabs in the future.

但这假设您的输入文件将来不会有由制表符分隔的其他字段。

回答by mlegge

Using the OFS (Output Field Separator) variable, you can avoid hard coding between rows:

使用 OFS(输出字段分隔符)变量,您可以避免行之间的硬编码:

awk -F"\t" -v OFS="\t" ' == "victorian" { print , }' file.in