i want extract first instance of string per line in linux. trying grep yields instances per line. below want strings (numbers , letters) after "tn="...but first set per line. actual characters combination of numbers or letters. , there space after them. there space before tn=
given following file:
hello name dog tn=12g3 fun 23k3 hello tn=1d3i9 cheese 234kd dks2 tn=6k4k ksk 1263 chairs tn=k38493kd cars run vroom it95958 tn=k22djd fair gold tn=293838 tounge
desired output:
12g3 k38493
here's 1 way can if have gnu grep, (mostly) supports perl compatible regular expressions -p
. also, non-standard switch -o
used print part matching pattern, rather whole line:
grep -po '^.*?tn=\k\s+' file
the pattern matches start of line ^
, followed characters .*?
, ?
makes match non-greedy. after first match of tn=
, \k
"kills" previous part you're left bit you're interested in: 1 or more non-space characters \s+
.
as in ed's answer, may wish add space before tn
avoid accidentally matching footn=...
. might prefer use \w
match "word" characters (equivalent [[:alnum:]_]
).
Comments
Post a Comment