awk:如果行以“做某事”开始,则以其他方式打印

awk:如果行以“做某事”开始,则以其他方式打印,awk,Awk,假设文件test.txt的内容为: >this_should_be_changed blabla >this_should_be_changed foofoo >this_should_be_changed barbar 我正在尝试获得以下输出: >this_changed blabla >this_changed foofoo >this_changed barbar 我想为此使用awk。我试过: awk -F "_" '/>/

假设文件
test.txt
的内容为:

>this_should_be_changed
blabla
>this_should_be_changed
foofoo
>this_should_be_changed
barbar
我正在尝试获得以下输出:

>this_changed
blabla
>this_changed
foofoo
>this_changed
barbar
我想为此使用
awk
。我试过:

awk -F "_" '/>/ {print $1"_"$4}' test.txt 
哪些产出:

>this_changed
>this_changed
>this_changed
我如何告诉
awk
它还应该打印与搜索模式不匹配的行

编辑

根据公认的答案,我找到了解决方案:

awk -F "_" '/>/{print $1"_"$4; next}1' test.txt 
编辑:添加1个带替换的解决方案,以防以后在代码中使用当前行

awk '
/^>/{
  match($0,/_.*_/)
  print substr($0,1,RSTART) substr($0,RSTART+RLENGTH)
  next
}
1
' Input_file


仅在展示样品的情况下,请尝试以下内容。使用GNU
awk
编写和测试

awk '/^>/{sub(/_.*_/,"_")} 1' Input_file
说明:添加上述内容的详细说明

awk '              ##Starting awk program from here.
/^>/{              ##Checking condition if line starts with > then do following.
  sub(/_.*_/,"_")  ##Substituting everything from _ to till _ with _ here.
}
1                  ##Printing current line here.
' Input_file       ##Mentioning Input_file name here.
注意:对于OP的代码修复,它将是
awk-F“\u”/>/{print$1”\u4;next}1”Input\u文件
编辑:添加1个带有替换的解决方案,以防以后在代码中使用当前行

awk '
/^>/{
  match($0,/_.*_/)
  print substr($0,1,RSTART) substr($0,RSTART+RLENGTH)
  next
}
1
' Input_file


仅在展示样品的情况下,请尝试以下内容。使用GNU
awk
编写和测试

awk '/^>/{sub(/_.*_/,"_")} 1' Input_file
说明:添加上述内容的详细说明

awk '              ##Starting awk program from here.
/^>/{              ##Checking condition if line starts with > then do following.
  sub(/_.*_/,"_")  ##Substituting everything from _ to till _ with _ here.
}
1                  ##Printing current line here.
' Input_file       ##Mentioning Input_file name here.

注意:使用OP的代码修复,它将是
awk-F“\u”/>{print$1”\u4;next}1'输入文件
一个简单的
sed
一行程序:

sed'/^>/s/.*.\u/\ u/'test.txt
>这一切都改变了
布拉布拉
>这一切都改变了
福福
>这一切都改变了
芭芭拉

简单的
sed
一行:

sed'/^>/s/.*.\u/\ u/'test.txt
>这一切都改变了
布拉布拉
>这一切都改变了
福福
>这一切都改变了
芭芭拉
另一个awk:

$ awk '/this_should_be_changed/{sub(/should_be_/,"")}1' file
输出:

>this_changed
blabla
>this_changed
foofoo
>this_changed
barbar
解释:

$ awk '
/this_should_be_changed/ {   # identify the record to change
    sub(/should_be_/,"")     # sub() changes the record
}1' file                     # output changed and unchanged records
另一个awk:

$ awk '/this_should_be_changed/{sub(/should_be_/,"")}1' file
输出:

>this_changed
blabla
>this_changed
foofoo
>this_changed
barbar
解释:

$ awk '
/this_should_be_changed/ {   # identify the record to change
    sub(/should_be_/,"")     # sub() changes the record
}1' file                     # output changed and unchanged records

谢谢你的解决方案。但是,我需要访问字段。现实世界的例子更为复杂。对不起,我应该提到这一点。@MKR,你能不能让我更清楚地知道什么地方不起作用?由于我看不到您的实际样品,请在上面添加更多信息。@MKR,好的,请您现在尝试我的第一个解决方案(编辑一个)并告诉我好吗?它使用正则表达式,不执行替换,所以以后您也可以使用line,请告诉我您的情况。谢谢@RavinderSingth13。解决方案是使用
next
1
。感谢您的解决方案。但是,我需要访问字段。现实世界的例子更为复杂。对不起,我应该提到这一点。@MKR,你能不能让我更清楚地知道什么地方不起作用?由于我看不到您的实际样品,请在上面添加更多信息。@MKR,好的,请您现在尝试我的第一个解决方案(编辑一个)并告诉我好吗?它使用正则表达式,不执行替换,所以以后您也可以使用line,请告诉我您的情况。谢谢@RavinderSingth13。解决方案是使用
next
1