Reputation: 69
I'm using a grep
I want to output only offset-point.
But now my commands are being printed out to offsets and macthing-keywords.
My command is
grep -Pbzo 'macthing-keywords' test.txt
output is..
15 : macthing-keywords
I want '15' to be printed out, representing offset (not macthing-keywords is printed)
Can you tell me how?
Upvotes: 1
Views: 24
Reputation: 626747
You may simply remove any <space>:<space><anything that remains here>
with a sed
command like sed 's/ : .*//'
or you may delete all after first :
with cut -d: -f1
(as suggested by @bigdataolddriver):
grep -Pbzo 'macthing-keywords' test.txt | sed 's/ : .*//'
Or
grep -Pbzo 'macthing-keywords' test.txt | cut -d: -f1
To output to a file:
grep -Pbzo 'macthing-keywords' test.txt | sed 's/ : .*//' > outputfile.txt
If you have multiple matches per line, you may need to separate them before running sed
or cut
:
xargs -0 | grep -Pbzo 'macthing-keywords' test.txt | \
xargs -0 -n1 | cut -d: -f1 > outputfile.txt
Or, which seems safer as it will remove :...
even if the matches contain linebreaks and those match continuations might also have :
in them (but this solution requires sed
that supports \xXX
notation):
xargs -0 | grep -Pbzo 'macthing-keywords' test.txt | \
sed 's/ *:[^\x00]*//g' | xargs -0 -n1 > outputfile.txt
The xargs -0
/ xargs -0 -n1
pair will handle the match breaks turning NULs to newlines.
Upvotes: 1