2

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

3 Answers 3

2

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

echo 'asdf | this is something | something else | nothing | qwerty' | sed 's/|[^|]*|/|/' asdf | something else | nothing | qwerty 
Sign up to request clarification or add additional context in comments.

Comments

2

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 

Comments

1

check this:

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

with your example

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.