I'd like to read filein.txt (tab delimited) and output a fileout.txt with only rows that match the value of a given column, and eliminate the column being queried. i.e.,
我想读文件。txt(制表符分隔符)并输出文件。txt,只包含与给定列的值匹配的行,并消除要查询的列。也就是说,
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 victorian
style, then my fileout.txt should look like:
让我们说,我想只选择房子的行是维多利亚风格的,然后我的文件。txt应该看起来像:
fileout.txt
#name\taddress
roger\t223 dolan st.
kingston\t224 house st.
3 个解决方案
#1
24
awk -F"\t" '$2 == "victorian" { print $1"\t"$3 }' file.in
#2
8
You can do it with the following awk
script:
您可以使用以下awk脚本:
#!/bin/bash
style="victorian"
awk -v s_style=$style 'BEGIN{FS=OFS="\t"}
$2==s_style {$2=""; sub("\t\t","\t"); print}'
Explanation:
解释:
-
style="victorian"
: assign the house style that you want to select outside of theawk
script so it's easier to maintain - style="victorian":指定要在awk脚本之外选择的house样式,以便更容易维护
-
awk
: invoke awk - awk:调用awk
-
-v s_style=$style
: the-v
option passes an external variable into awk. Need to specify this for each variable you pass in. In this case it assigns the external variable$style
to the awk variables_style
. - -v s_style=$style: -v选项将外部变量传递到awk。需要为传入的每个变量指定此值。在这种情况下,它将外部变量$style分配给awk变量s_style。
-
BEGIN{FS=OFS="\t"}
: tells awk that the field separators in the output should be tabs, not spaces by default. - BEGIN{FS=OFS="\t"}:告诉awk输出中的字段分隔符应该是制表符,而不是默认的空格。
-
{$2==s_style {$2=""; sub("\t\t","\t"); print}}'
: If the 2nd field is the house type specified ins_style
(in this case,victorian
), then remove it and print the line. - { $ 2 = = s_style { 2美元= " ";子(“\ t \”,\“t”);打印}':如果第二个字段是s_style中指定的house类型(在本例中为维多利亚),那么删除它并打印行。
Alternatively, you could do:
或者,你可以做的:
#!/bin/bash
style="victorian"
awk -v s_style=$style 'BEGIN{FS=OFS="\t"}
$2==s_style {print $1, $3}'
but this assumes that your input files will not have additional fields separated by tabs in the future.
但是,这假定您的输入文件将来不会有由制表符分隔的其他字段。
#3
2
Using the OFS (Output Field Separator) variable, you can avoid hard coding between rows:
使用OFS(输出字段分隔符)变量,可以避免在行之间进行硬编码:
awk -F"\t" -v OFS="\t" '$2 == "victorian" { print $1,$3 }' file.in
#1
24
awk -F"\t" '$2 == "victorian" { print $1"\t"$3 }' file.in
#2
8
You can do it with the following awk
script:
您可以使用以下awk脚本:
#!/bin/bash
style="victorian"
awk -v s_style=$style 'BEGIN{FS=OFS="\t"}
$2==s_style {$2=""; sub("\t\t","\t"); print}'
Explanation:
解释:
-
style="victorian"
: assign the house style that you want to select outside of theawk
script so it's easier to maintain - style="victorian":指定要在awk脚本之外选择的house样式,以便更容易维护
-
awk
: invoke awk - awk:调用awk
-
-v s_style=$style
: the-v
option passes an external variable into awk. Need to specify this for each variable you pass in. In this case it assigns the external variable$style
to the awk variables_style
. - -v s_style=$style: -v选项将外部变量传递到awk。需要为传入的每个变量指定此值。在这种情况下,它将外部变量$style分配给awk变量s_style。
-
BEGIN{FS=OFS="\t"}
: tells awk that the field separators in the output should be tabs, not spaces by default. - BEGIN{FS=OFS="\t"}:告诉awk输出中的字段分隔符应该是制表符,而不是默认的空格。
-
{$2==s_style {$2=""; sub("\t\t","\t"); print}}'
: If the 2nd field is the house type specified ins_style
(in this case,victorian
), then remove it and print the line. - { $ 2 = = s_style { 2美元= " ";子(“\ t \”,\“t”);打印}':如果第二个字段是s_style中指定的house类型(在本例中为维多利亚),那么删除它并打印行。
Alternatively, you could do:
或者,你可以做的:
#!/bin/bash
style="victorian"
awk -v s_style=$style 'BEGIN{FS=OFS="\t"}
$2==s_style {print $1, $3}'
but this assumes that your input files will not have additional fields separated by tabs in the future.
但是,这假定您的输入文件将来不会有由制表符分隔的其他字段。
#3
2
Using the OFS (Output Field Separator) variable, you can avoid hard coding between rows:
使用OFS(输出字段分隔符)变量,可以避免在行之间进行硬编码:
awk -F"\t" -v OFS="\t" '$2 == "victorian" { print $1,$3 }' file.in