Home > front end >  Can anyone please let me know what these lines are for the sed command
Can anyone please let me know what these lines are for the sed command

Time:12-17

Can anyone please explain what does meaning of the below sed command:

sed -i '155 s|go test -v|go test -v -count=1|' ./hack/build.sh
sed -i '163 s|rm|cp $test_output ./client-unittest.txt \&\& rm|' ./hack/build.sh

One thing I found is that -i will edit the file in-place(same file is changed and saved again), 155 and 163 specify line numbers from the ./hack/build.sh to which substitution will take place. But I am unable to understand s|go test -v| go...

Please correct me if my understanding is wrong.

CodePudding user response:

  • s|go test -v|go test -v -count=1| replaces the go test -v with go test -v -count=1
  • s|rm|cp $test_output ./client-unittest.txt \&\& rm| replaces the rm with cp $test_output ./client-unittest.txt && rm (Here, the & is escaped by a \ because an unescaped & has a special meaning in the replacement string).

The s command substitutes the replacement string for instances of the regular expression in the pattern space. Its syntax is s/regexp/replacement/flags. Any character other than backslash (\) or newline can be used instead of a slash (/) to delimit the regex and the replacement; in the examples the | is used instead of a /.

CodePudding user response:

Correct me if I'm wrong but should this be:

sed -i '155 s/go test -v/go test -v -count=1/g' ./hack/build.sh
sed -i '163 s/rm/cp \$test_output \.\/client-unittest.txt \&\& rm/g' ./hack/build.sh

sed uses / not | in its syntax. You also need to escape special characters for them to be taken literally.

sed syntax should look like this: sed '$line s/$pattern/$substitution/g' $file

  • Related