简体   繁体   English

Bash - 使用新行替换字符串的Sed / Awk

[英]Bash - Sed / Awk replacing string with new line

I have a file with strings like this below and want replace everthing between the "steps:" and "-" with a new line: 我有一个带有如下所示字符串的文件,并希望在“steps:”和“ - ”之间用新行代替everthing:

- name: Change It uid: Change It_TRANS_App - Web type: TRANSACTION steps: 02 - name: Change It 1 type: BSTEP uid: Step 1_STEP_Change It_TRANS_App -strong text Web rules:

I've tried using awk like: 我尝试过使用awk:

echo $string | awk {'gsub(/steps:.*?-/ , 'steps:\n-' )'; print $0}

However, I get the error below: 但是,我收到以下错误:

awk: {gsub(/steps:.*?-/ , steps:n- )
awk:                          ^ syntax error
awk: fatal: 0 is invalid as number of arguments for gsub

I've tried as well with sed: 我和sed一起尝试过:

sed -r 's:/\bsteps:\b.*?-/\n/' stringfile.txt > output.txt

and removing the ":": 并删除“:”:

sed -r 's/\bsteps.*?-/\n/' stringfile.txt > output.txt

In the first case, I get the error: 在第一种情况下,我得到错误:

sed: -e expression #1, char 28: unterminated `s' command

And with the second sed I removes a lot of other things that it shouldn't. 随着第二个sed,我删除了很多其他不应该的东西。

[Edit] As you guys told me, I forgot to put the expected output, I've been thinking that it would be simple, however I'm not good. [编辑]正如你们告诉我的那样,我忘了把预期的输出,我一直以为这很简单,但是我不好。 I've want to "break" as a new line cuting out just the word right after "step", in that case, would be "02". 我想要“打破”作为一条新线,在“步骤”之后立即切出这个词,在这种情况下,将是“02”。 It would be to the first occurence of "-": 这将是第一次出现“ - ”:

- name: Change It uid: Change It_TRANS_App - Web type: TRANSACTION steps:
- name: Change It 1 type: BSTEP uid: Step 1_STEP_Change It_TRANS_App -strong text Web rules:

You were close: 你很亲密:

awk '{gsub(/steps:.*-/,"steps:\n-");print $0}' <<< "$sting"
- name: Change It uid: Change It_TRANS_App - Web type: TRANSACTION steps:
-strong text Web rules:

Can be shorted to: 可以缩写为:

awk '{gsub(/steps:.*-/,"steps:\n-")}1' <<< "$sting"

Ref Eds comments: Ref Eds评论:

awk '{gsub(/steps:[^-]*-/,"steps:\n-")}1' t
- name: Change It uid: Change It_TRANS_App - Web type: TRANSACTION steps:
- name: Change It 1 type: BSTEP uid: Step 1_STEP_Change It_TRANS_App -strong text Web rules:

Since sed won't support non-greedy patterns *? 既然sed不支持非贪婪模式*? , you could use a negated class character class. ,你可以使用一个否定的类字符类。

sed -r 's/\bsteps[^-]*-/\n-/' file

Example: 例:

$ sed -r 's/\bsteps[^-]*/\n/' file
- name: Change It uid: Change It_TRANS_App - Web type: TRANSACTION 
- name: Change It 1 type: BSTEP uid: Step 1_STEP_Change It_TRANS_App -strong text Web rules:

While awk and sed gurus are providing the solution, may I suggest to consider using Perl. 虽然awksed专家正在提供解决方案,但我建议考虑使用Perl。

To substitute for the new line everything until the first occurrence of - in the string: 要替换新行,直到第一次出现-在字符串中:

echo $string | perl -pe 's/(.*steps:).*?(-.*)/$1\n$2/'
- name: Change It uid: Change It_TRANS_App - Web type: TRANSACTION steps:
- name: Change It 1 type: BSTEP uid: Step 1_STEP_Change It_TRANS_App -strong text Web rules:

To substitute for the new line everything until the last occurrence of - in the string: 要替换新行,直到最后一次出现-在字符串中:

echo $string | perl -pe 's/(.*steps:).*(-.*)/$1\n$2/'
- name: Change It uid: Change It_TRANS_App - Web type: TRANSACTION steps:
-strong text Web rules:

Two expressions inside parenthesis are refereed to as $1 and $2 in the second half of the substitution expression. 括号内的两个表达式在替换表达式的后半部分被称为$ 1和$ 2。 And then ? 然后? in the middle part regulates whether .* works greedy or not greedy. 在中间部分规定.*是贪婪还是不贪婪。

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

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