rehman
rehman

Reputation: 101

Delete only the lines of files, which have certain numerical character strings

I have so many files with name *DSP

The file contains the following information:

SURF96 R U X 0 160 3.92112 0.99791
SURF96 R U X 0 150 3.75218 0.85666
SURF96 R U X 0 140 3.71209 0.78256
SURF96 R U X 0 130 3.73428 0.73537
SURF96 R U X 0 120 3.79718 0.70187
SURF96 R U X 0 110 3.89562 0.67717
SURF96 R U X 0 100 3.91107 0.62050
SURF96 R U X 0 95 3.91176 0.58968
SURF96 R U X 0 90 3.91777 0.56036
SURF96 R U X 0 85 3.93612 0.53420
SURF96 R U X 0 80 3.95969 0.50882

I want to delete the line that contains 110, 120,130,140,150,160 and save an existing file.

I have tried this code but it's not working:

for file in *DSP; do
sed '/110/d' $file
sed '/120/d' $file
sed '/130/d' $file
sed '/140/d' $file
sed '/150/d' $file
sed '/160/d' $file

done

Upvotes: 0

Views: 39

Answers (1)

James Brown
James Brown

Reputation: 37404

Single file with sed -i:

$ sed -i '/1[1-6]0/d' file
$ cat file
SURF96 R U X 0 95 3.91176 0.58968
SURF96 R U X 0 90 3.91777 0.56036
SURF96 R U X 0 85 3.93612 0.53420
SURF96 R U X 0 80 3.95969 0.50882

man sed:

-i[SUFFIX], --in-place[=SUFFIX]

       edit files in place (makes backup if SUFFIX supplied)

The regex is a bit problematic as at least this line was lost:

SURF96 R U X 0 100 3.91107 0.62050
                      ^^^

You should at least add space before and after the value, ie. / 1[1-6]0 /d.

Using recent enough GNU awk you could:

$ awk -i inplace '$6!~/1[1-6]0/' file

which examines the 6th space-separated field for the regex.

Upvotes: 2

Related Questions