gandalf3
gandalf3

Reputation: 1666

Sed remove everything between first and second instance of a character?

I'm not very experienced with sed, but If I have a string like this:

asdf | this is something | something else | nothing | qwerty

can I remove everything between the first and second instances of |, plus one of them?

The ideal output would be:

asdf | something else | nothing | qwerty

I tried sed 's/|*|//2', but this only removes the second pipe.

Thanks

Upvotes: 2

Views: 1541

Answers (3)

anubhava
anubhava

Reputation: 784908

Can be done using awk also:

awk -F '|' -v OFS='|' '{sub($2 " *\\|", "")}1' <<< "$str"
asdf | something else | nothing | qwerty

Using pure BASH:

echo "${str%%|*}|${str#*|*|}"
asdf | something else | nothing | qwerty

Upvotes: 2

Kent
Kent

Reputation: 195029

check this:

sed 's/|[^|]*//'

with your example

kent$ sed 's/|[^|]*//' <<<"asdf | this is something | something else | nothing | qwerty"                                                                      
asdf | something else | nothing | qwerty

Upvotes: 1

iruvar
iruvar

Reputation: 23374

s/|[^|]*|/|/ should do the job

echo 'asdf | this is something | something else | nothing | qwerty' | 
sed 's/|[^|]*|/|/'
asdf | something else | nothing | qwerty

Upvotes: 2

Related Questions