$ ruby -ne 'puts $_.scan(/id=(\d+)/)' file
9
10
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())
.
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
A perl-solution:
perl -nE 'say $1 if /id=(\d+)/' filename
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.
Source: Stackoverflow.com