0

How can I delete all words in a file starting with a specific string (in this case the string "end") so that:

<region> sample=PDL UP IN-A-1.flac lokey=21 hikey=21 lovel=0 hivel=21 end=423471 pitch_keycenter=21  
<region> sample=PDL UP IN-A-1_0001.flac lokey=21 hikey=21 lovel=22 hivel=42 end=370196 pitch_keycenter=21  
<region> sample=PDL UP IN-A-1_0002.flac lokey=21 hikey=21 lovel=43 hivel=63 end=362268 pitch_keycenter=21

Becomes:

<region> sample=PDL UP IN-A-1.flac lokey=21 hikey=21 lovel=0 hivel=21 pitch_keycenter=21  
<region> sample=PDL UP IN-A-1_0001.flac lokey=21 hikey=21 lovel=22 hivel=42 pitch_keycenter=21  
<region> sample=PDL UP IN-A-1_0002.flac lokey=21 hikey=21 lovel=43 hivel=63 pitch_keycenter=21 

using the command line

I tried:

sed 's/\S*\(end\|END\)\S*//g' file.txt

But it didn't work.

Thanks.

3 Answers 3

3

You were pretty close. The biggest thing you were missing was enabling extended regex.

Second, you have "\S*" at the beginning of the match. This doesn't actually do anything since it allows "one or zero" matches on anything but a space. Since "end" always starts on a space, this will never be used. I believe you meant to say "end" was the start of the word, which is a "<" character.

Here's a working version:

sed -E 's/\<(end|END)\S*//g' file.txt

But it can be improved a little. Here I've made the match case insensitive to remove the "or" on the word, and thus removes the need for extended regex. I also added a trailing"\s" to remove the trailing space (prevent a double space in the gap).

sed 's/\<END\S*\s//gi' file.txt

Here's a guide on sed from GNU for reference.

Update

Based on your comment below, it seems we all misunderstood what you wanted. It sounds like you wanted to not only remove those text fields, but to do so in the file itself. While I apologize for missing that point, this is a good lesson on why it is so important to phrase your question to ask for exactly what you want.

Anyway, your solution (to add > file.txt) effectively overwrites the old file with the new text. Specifically, sed will output the result to stdout (your terminal). The ">" symbol is called a redirection. It will redirect the stdout before it into whatever is after it. This will actually remove the existing file and replace it with a new one.

While this works, it isn't the best. If you want to replace the string in the file in-place, sed can do that for you as well by adding a "-i" option:

sed -i 's/\<END\S*\s//gi' file.txt

Here's a useful reference to help phrase questions effectively. The more precise you can be in conveying what you want, the better the answers will be!

Hope this helps!

1
  • A good first answer Jul 27, 2023 at 0:31
0

in this case, since the input is so well formed, it might be easier to use cut

cut -d" " -f-8,10- file.txt

explanation

-d" " sets field separator to space. default is tab.

-f-8,10- take fields first to eight, then tenth to last. skipping the ninth field which is the one starting with "end".

0
sed -E 's/ (end|END)[^ ]+//g' file.txt

Matches and removes a space, followed by end or END, followed as many non spaces as possible.

You must log in to answer this question.

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