简体   繁体   English

AWK \\ SED在单个命令中同时替换字符串的^(开始)和$(end)

[英]AWK\SED Replace both ^(beginning) and $(end) of a string in a single command

I've been looking around but couldn't find a way to do it with both AWK and SED . 我一直在环顾四周,但是找不到同时使用AWKSED的方法 I was wondering if there's a way to replace a string's start and end in a single command. 我想知道是否有一种方法可以在单个命令中替换字符串的开头和结尾。

more specifically , there's a file with a lot of words in it, and I would like to add something before the word and after the word. 更具体地说 ,其中有一个包含很多单词的文件,我想在单词之前之后添加一些内容。

Thanks, Roy 谢谢罗伊

Since you said: more specifically, there's a file with a lot of words in it, and I would like to add something before the word and after the word. 如您所言: 更具体地说,其中有一个包含很多单词的文件,我想在单词之前和之后添加一些内容。

The only thing you need is $& that is match itself. 您唯一需要的就是$&本身就是匹配项。 So you simply can write anything that you want just before and end of this whildcard. 因此,您只需要此成绩单的开头和结尾都可以写任何您想要的东西。 that's it. 而已。

For example say you have this file: 例如,说您有以下文件:

this is line 1.
this is line 2.
this is line 3.

And I tested with perl: 我用perl进行了测试
perl -lne 'print "beginning->", $&, "<-end" if /.+/g' file
which the output is: 输出是:

beginning->this is line 1.<-end
beginning->this is line 2.<-end
beginning->this is line 3.<-end

May you would like to match only one word, so still this is a good solution such as: 可能您只想匹配一个单词,所以这仍然是一个很好的解决方案,例如:

perl -lne 'use English; print "$PREMATCH", "[$MATCH]","$POSTMATCH" if /line/g' file  

Here I matched line and put around that: [ then $& then ] 在这里,我匹配了line并放了起来: [然后$&然后]
the output 输出

this is [line] 1.
this is [line] 2.
this is [line] 3.  

NOTE 注意
As you can see the only things you need just are prematch and match and postmatch . 如您所见,您只需要预赛比赛赛后 比赛 I tested it with perl for you, and if you are interesting in Perl you can use it or may you want to use Sed or Awk . 我为您测试过perl ,如果您对Perl感兴趣的话,可以使用它,也可以使用SedAwk Since you have no specific examples I tested with Perl . 由于您没有特定的示例,因此我使用Perl进行了测试。

If you want to wrap a particular word with markers you can use & in the replacement string to achieve what you want. 如果您想用标记来包装特定的单词,可以在替换字符串中使用&来实现所需的功能。

For example to put square brackets around every occurrence of the word bird: 例如,将方括号括在每次出现“鸟”一词的周围:

$ echo "hello bird, are you really a bird?" | sed "s/\bbird\b/[&]/g"
hello [bird], are you really a [bird]?

to replace a string's start and end in a single command 用单个命令替换字符串的开头和结尾

Let's say we have a test file with line: 假设我们有一个包含以下内容的测试文件:

tag hello, world tag

To enclose each tag word with angle brackets < ... > we can apply: 要用尖括号< ... >将每个tag词括起来,我们可以应用:

awk approach with gsub() function: 使用gsub()函数的awk方法:

awk '{ gsub(/\<tag\>/, "<&>"); print}' test_file 

word boundaries \\< , \\> may differ depending on awk implementations 单词边界\\<\\>可能因awk实现而异


sed approach: sed方法:

sed 's/\btag\b/<&>/g' test_file 

The output(for both approaches): 输出(两种方法):

<tag> hello, world <tag>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM