Bash:根据匹配的子字符串将整个字符串替换为一列 [英] Bash: replace entire string in one column based on matching substring

查看:62
本文介绍了Bash:根据匹配的子字符串将整个字符串替换为一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的文件,其中包含许多列和行.我想根据我要替换的所有字符串共有的子字符串替换第一列中的整个字符串.这是我所拥有的示例:

I have a large file with many columns and rows. I would like to replace an entire string in the first column based on a substring that's common to all strings I want to replace. Here's an example of what I have:

AAA_1765 866 HTG
AAA_1873 987 IGA
AAA_1922 413 BOK

我希望第一列中包含子字符串AAA_1的所有字符串都完全替换为另一个字符串,以使它看起来像这样:

I would like all strings in the first column that contain the substring AAA_1 be entirely replaced with another string, so that it looks like this:

BBB_2 866 HTG
BBB_2 987 IGA
BBB_2 413 BOK

我一直在与sed一起进行搜索/替换:

I've been working with sed to do a search/replace:

sed 's/^AAA_1*/BBB_2/' infile.txt >outfile.txt
sed 's/^AAA_1.*/BBB_2/' infile.txt >outfile.txt

但是第一次使用仅将子字符串AAA_1替换为BBB_2,并保留其余字符串(我希望将整个字符串替换为BBB_2),第二次使用将整个行替换为BBB_2(我只希望字符串在第一列中替换).

But the first use replaces only the substring AAA_1 with BBB_2 and retains the rest of the string (I want the full string to be replaced with BBB_2), and the second use replaces the entire line with BBB_2 (I only want the string in column one replaced).

也许我需要awk?任何建议都会有所帮助.

Maybe awk is what I need? Any suggestions will be helpful.

推荐答案

您可以使用

sed 's/^AAA_1[0-9]*/BBB_2/' infile.txt > outfile.txt

请参见在线 sed 演示.

此正则表达式匹配

  • ^ -行首- AAA_1 -文字子字符串
  • [0-9] * -零个或多个数字(如果要表示非空格,则可以将其替换为 [^] * )
  • ^ - start of a line -AAA_1 - a literal substring
  • [0-9]* - zero or more digits (if any non-space is meant, you may replace it with [^ ]*)

这篇关于Bash:根据匹配的子字符串将整个字符串替换为一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆