简体   繁体   中英

Extracting specific words using sed

I have a line like this

BR_MALLOC %p File - %s, Func - %s, Line - %u\n

This is actually a line from a C program which outputs the malloced address, the file where the custom malloc BR_MALLOC was called etc etc.

Now, I am running sed on this output to get just the malloced address (%p)

I tried this regular expression

$ echo "BR_MALLOC %p File - %s, Func - %s, Line - %u\n" | sed 's/BR_MALLOC\s+\(\S+\).*/\1'

Expected Output

%p

Output I get is

BR_MALLOC %p File - %s, Func - %s, Line - %un

Where am I going wrong?

You might need to escape the + ( docs ):

echo "BR_MALLOC %p File - %s, Func - %s, Line - %u\n" | sed 's/BR_MALLOC\s\+\(\S\+\).*/\1/'
%p

不了解sed,但使用perl,你的表达式几乎可以正常工作:

perl -pe 's/BR_MALLOC\s+(\S+).*/$1/'

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