Bash script reading line for '+', if not p

2019-07-29 17:01发布

问题:

I am writing a bash script and trying to change lines in my file. I currently have:

 if [[ ! $line == *[+]* ]]
   then
     [command to change line]

I merely want to change the line by adding on to the text already there. Any suggestions? I have tried:

sed -i 'Ns/.*/replacement-line/' file.txt

and

sed -i '/Text_to_be_replaced/c\This is the new line.' file.txt

among some others found online to no avail.

My full script is:

#!bin/bash
filename="227.dat"
while ((i++)); read -r line; do
sed -i 's/(/  /g' $filename
sed -i 's/)//g' $filename
sed -i 's/,/    /g' $filename
sed -i 's/-x/-1     0     0/g' $filename
sed -i 's/x/ 1     0     0/g' $filename
sed -i 's/-y/ 0    -1     0/g' $filename
sed -i 's/y/ 0     1     0/g' $filename
sed -i 's/-z/ 0     0    -1/g' $filename
sed -i 's/z/ 0     0     1/g' $filename
[*command to add to line*]
done < "$filename"

回答1:

You can put the test in the sed command:

sed -i '/+/! s/.*/replacement-line/' file.txt

The ! means to do the replacement only on lines that don't match the regular expression.



标签: bash shell sed