I have a file with strings like this below and want replace everthing between the "steps:" and "-" with a new line:
- 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:
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 -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.
[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". 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:
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 *?
, 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.
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. And then ?
in the middle part regulates whether .*
works greedy or not greedy.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.