2

I have a text file which contains some lines as the following:

ASDASD2W 3ASGDD12 SDADFDFDDFDD W11 ACC=PNO23 DFSAEFEA EAEDEWRESAD ASSDRE 
AERREEW2 3122312 SDADDSADADAD W12 ACC=HH34 23SAEFEA EAEDEWRESAD ASEEWEE 
A15ECCCW 3XCXXF12 SDSGTRERRECC W43 ACC=P11 XXFSAEFEA EAEDEWRESAD ASWWWW 
ASDASD2W 3122312 SDAFFFDEEEEE SD3 ACC=PNI22 ABCEFEA EAEDEWRESAD ASWEDSSAD 
...

I have to extract the substring between the '=' character and the following blank space for each line , i.e.

PNO23
HH34
P11
PNI22

I've been using the sed command but cannot figure out how to ignore all characters following the blank space.

Any help?

6 Answers 6

2

Use the right tool for the job.

$ awk -F '[= ]+' '{ print $6 }' input.txt
PNO23
HH34
P11
PNI22
Sign up to request clarification or add additional context in comments.

8 Comments

Doubtless awk is a powerful tool and your code will help me a lot but i'm just curious about achieving the same result with sed even if it were harder.
Using sed is certainly not harder in this case. "The right tool for the right job" is just wrong in this context. Sed is the right tool. Awk is better for tabular data and quick hacks and calculations. Sed makes regular expressions easy and is more declarative. See my answer.
@JoSo Sed grammar may make it easier to use regular expressions than awk in some situations, but this is not one of them.
@WilliamPursell: Please explain. I think s/.*=//; s/ .*// is dead simple.
@JoSo Although s/.*=//; s/ .*// is simple, [= ]+ is much simpler.
|
2

Sorry, but have to add another one because I feel the existing answers are just to complicated

sed 's/.*=//; s/ .*//;' inputfile

Comments

1

This might work for you:

sed -n 's/.*=\([^ ]*\).*/\1/p' file

or, if you prefer:

sed 's/.*=\([^ ]*\).*/\1/p;d' file

Comments

0

Put the string you want to capture in a backreference:

sed 's/.*=\([^ =]*\) .*/\1/'

or do the substitution piecemeal;

sed -e 's/.*=//' -e 's/ .*//'

Comments

0
sed 's/[^=]*=\([^ ]*\) .*/\1/' inputfile

Match all the non-equal-sign characters and an equal sign. Capture a sequence of non-space characters. Match a space and the rest of the line. Substitute the captured string.

Comments

0

A chain of grep can do the trick.

grep -o '[=][a-zA-Z0-9]*' file | grep -o '[a-zA-Z0-9]*'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.