Tim
Tim

Reputation: 113

How do I delete a line that contains a specific string at a specific location using sed or awk?

I want to find and delete all lines that have a specific string, with a specific length, at a specific location.

One line in my data set looks something like this:

STRING   1234567 1234567 7654321 6543217 5432176

Notes:

In this example, I want to find lines containing "1234567" located at column 17 and spanning to column 24 (i.e. the third field) and delete them. How can I do this with sed or awk?

I have used the following, but it deletes lines that I want to keep:

sed -i '/1234567/d' ./file_name.dat

Cheers!

Upvotes: 1

Views: 1084

Answers (2)

karakfa
karakfa

Reputation: 67507

with awk, print lines except the substring match.

$ awk 'substr($0,17,7)=="1234567"{next}1' file > output_file

or perhaps inverse logic is easier

$ awk 'substr($0,17,7)!="1234567"' file > output_file

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626845

You may use

sed -i '/^.\{17\}1234567/d' ./file_name.dat

Details

  • ^ - start of a line
  • .{17} - any 17 chars
  • 1234567 - a substring.

See the online sed demo:

s="STRING   1234567 1234567 7654321 6543217 5432176
STRING   1234567 5534567 7654321 6543217 5432176"
sed '/^.\{17\}1234567/d' <<< "$s"
# => STRING   1234567 5534567 7654321 6543217 5432176

Upvotes: 1

Related Questions