Reputation: 9893
I have following string
â â³ eGalax Inc. USB TouchController id=9 [slave pointer (2)]
â â³ eGalax Inc. USB TouchController id=10 [slave pointer (2)]
and would like to get the list of id ? How this can be done using sed or something else ?
Upvotes: 20
Views: 152297
Reputation: 1782
Assuming input of
{Anything}id={ID}{space}{Anything}
{Anything}id={ID}{space}{Anything}
--
#! /bin/sh
while read s; do
rhs=${s##*id=}
id=${rhs%% *}
echo $id # Do what you will with $id here
done <so.txt
Or if it's always the 7th field
#! /bin/sh
while read f1 f2 f3 f4 f5 f6 f7 rest
do
echo ${f7##id=}
done <so.txt
See Also
Upvotes: 1
Reputation: 360733
You can have awk
do it all without using cut
:
awk '{print substr($7,index($7,"=")+1)}' inputfile
You could use split()
instead of substr(index())
.
Upvotes: 2
Reputation: 8916
Use a regular expression to catch the id number and replace the whole line with the number. Something like this should do it (match everything up to "id=", then match any number of digits, then match the rest of the line):
sed -e 's/.*id=\([0-9]\+\).*/\1/g'
Do this for every line and you get the list of ids.
Upvotes: 3
Reputation: 74795
I pasted the contents of your example into a file named so.txt
.
$ cat so.txt | awk '{ print $7 }' | cut -f2 -d"="
9
10
Explanation:
cat so.txt
will print the contents of the file to stdout
. awk '{ print $7 }'
will print the seventh column, i.e. the one containing id=n
cut -f2 -d"="
will cut the output of step #2 using =
as the delimiter and get the second column (-f2
)If you'd rather get id=
also, then:
$ cat so.txt | awk '{ print $7 }'
id=9
id=10
Upvotes: 43