简体   繁体   中英

optimize sed command for better performance

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?

Seems to me like you just want to use cut:

cut -d '|' -f 2 file

Set the delimiter to | and print the second field.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM