0

I have got the following lines in a file:

1231231213123123123|W3A|{ (ABCDE)="8=3AF.R.Y2=133AA=9WW=334MNFN=20120925-22:23:59.998
1231231213123123123|4GM|{ (ABCDE)="8=3AF.R.Y2=123AA=9WW=4AF013DCV=EXAMPLE=ABC
1231231213123123123|KYC|{ (ABCDE)="8=3AF.R.Y2=112AA=9WW=0002DDS=20120921-14:55:21

In order to get the value between '|' characters I am using:

sed -e 's/\(^.*|\)\(.*\)\(|.*$\)/\2/'

And output is:

W3A
4GM
KYC

Which is expected. But as file has thousands of records, sed command is taking a lot of time. Is there any way to improve the performance of this command?

1
  • You could try awk, with awk -F'|' '{print $2}' yourFile Commented Feb 4, 2016 at 18:52

2 Answers 2

4

Seems to me like you just want to use cut:

cut -d '|' -f 2 file

Set the delimiter to | and print the second field.

Sign up to request clarification or add additional context in comments.

Comments

1

Since you're only keeping the 2nd parenthesized groups, you're making sed do unnecessary work by remembering the other stuff. Try

sed -e 's/^[^|]*|\([^|]*\)|.*/\1/'

Tom Fenech's answer should be a lot faster though.

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.