2

I have a file .txt with inside

ID       VALUE
test     value_1
testing  value_2
first    value_3
second   value_4

I'm trying to write a shell script which will delete a line corresponding to a given ID. For exemple, I would like to delete the line with the ID "test" so I found on the forum that could use the "sed" function by the following way:

sed -i '/test/d' file.txt

However, this function will also delete the line with the ID "testing" because this word is composite with the word "test". Furthermore the function will check in every line, so if there is a value with the "test" word, it'll be delete too.

So,

  • How can I check and delete every line with the word "test" ONLY in the ID column, given the ID is at the beginning of everyline, I can also ask how can I check and delete every line START with the word "test"?
  • How can I delete only when the ID is "test" and not composite with the word "test" (In this case, "testing" is delete )
  • Is the function "sed" is really adequate ?

Thanks

2 Answers 2

10

Use \b or \s and ^

\b to match word boundaries, and ^ to match the beginning of a line:

sed -i '/^test\b/d' file.txt

Instead of \b, consider \s to match a space character if your columns are whitespace-delimited.

Is sed adequate?

Yes.


What follows is a brief mention of (most of) the other "usual suspects" in command-line text processing:

Alternatively, sometimes I use grep for similar purposes, especially if I simply want to work on a copy of the file, redirect the output, or stream the output to another utility:

grep -v '^test\b' file.txt 

or using the -w word match:

grep -vw '^test' file.txt

Both output:

ID       VALUE
testing  value_2
first    value_3
second   value_4

There are many other more powerful options available once you get into the higher-level scripting languages. awk is a good in-between choice, and is good at handling tabular input such as this:

awk 'NR > 1 && $1 != "test" { print $1,":",$2 }' test.txt

NR > 1 skips the first (header) line, and $1 != "test" uses a simple string comparison instead of a regex (although you could equivalently use !/^test / instead). The { print ... } block prints out the first and second columns separated by a colon (arbitrary, just to show you a bit of what's possible):

testing : value_2
first : value_3
second : value_4

so awk is a good tool to learn. After that, you are in the realm of full programming languages like Perl, Python, etc. They can be used for one-liners, or gigantic software suites.

1

@type_outcast

Thank for you answer. I found a solution that's why I choose the answer to my own question: I found on the forum how to delete everyone beginning with a given word with

sed '/^test/ d'

So I use "join" it with the following line

sed -i '/^test\b/d' file.txt

which look finally like

sed /^\btest\b/ d

The last command line will allow you to delete every line beginning with the word boundaries "test"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .