开发者

how to delete a line containg string from file in unix or solaris

开发者 https://www.devze.com 2023-02-16 20:23 出处:网络
I have myfile a开发者_开发技巧s follows test1 test2 test3 test4 test5 I want to delete line containing test2, so I used command as follows, but instead of removing only \'test2\' line its removed w

I have myfile a开发者_开发技巧s follows

test1    
test2    
test3    
test4    
test5

I want to delete line containing test2, so I used command as follows, but instead of removing only 'test2' line its removed whole file and fize-size became zero.

bash# cat /aaa/bbb/ccc/myfile | sed -e '/test2/ d'  > /aaa/bbb/ccc/myfile     
bash# ls -l total 0    
-rw-r--r--   1 root     root           0  3月 11 17:41 myfile

can anybody suggest , whats wrong in command?


unless you have GNU sed (with the "-i" option), and you're on Solaris so you probably don't, you have no choice but to write to temp file:

sed -e '.....' infile > infile.tmp
mv infile.tmp infile

update: edit the file in-place with ed

printf "%s\n" 'g/test2/d' w q | ed infile


I don't know if perl is standard on Solaris. If this is the case, you can use:

perl -ni -e 'if(!/test2/){print;}' myfile


Unfortunately the output redirection immediately empties the output file. Therefore you have to use a different file as output file, eg:

sed '/test2/d' /aaa/bbb/ccc/myfile > /aaa/bbb/ccc/myfile2

Or you could do e.g. something like that:

sed '/test2/d' /aaa/bbb/ccc/myfile | tee /aaa/bbb/ccc/myfile

But due to buffering this is not very reliable. If the output program (tee) writes to the file before sed has finished reading, this will lead to corrupt data. Maybe you could also experiment with the programs buffer or mbuffer as substitute for tee there you can specify buffer sizes. But I didn't have reliable success on a fast trial.


Try

grep -v test2 test.txt  > temp
mv temp test.txt 


You can simply use the good old ed:

echo "/test2
d
w
q" | ed /aaa/bbb/ccc/myfile


You can also use grep:

> grep -v test2 test.txt 
test1    
test3    
test4    
test5

Be aware that as with sed, you shouldn't overwrite the file you are reading from, so you can invoke it like this:

grep -v test2 test.txt > test.out


Solaris, assuming you are not on an archaic version, should already come with at least bash 3. So using just bash

while read -r line
do
  case "$line" in
     *"test2"*) continue;;
     *) echo "$line";;   
  esac
done < file > temp && mv temp file


the following code is workign fine for me...

touch myfile2
cp /aaa/bbb/ccc/myfile myfile2
sed '/test2/d' myfile2 > /aaa/bbb/ccc/myfile


You can read the whole file into an array in AWK:

awk '!/test2/ {a[++c] = $0} END {for (i=1; i<=c; i++) print a[i] > FILENAME}' /aaa/bbb/ccc/myfile

Since redirection is not done by the shell, early truncation is not performed.


$touch myfile

$printf "test1\ntest2\ntest3\ntest4\ntest5\n">myfile

$cat myfile 

test1
test2
test3
test4
test5

$sed '2d' myfile

test1
test3
test4
test5

So use:

$sed '2d' myfile
0

精彩评论

暂无评论...
验证码 换一张
取 消