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

2019-07-29 16:26发布

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"

标签: bash shell sed
1条回答
叼着烟拽天下
2楼-- · 2019-07-29 17:06

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.

查看更多
登录 后发表回答