jimmy_y
1
Hi Everyone,
I have a txt file: a.txt
a_b_ 3
c_d_e_ 55
(Assume this a.txt got 100 lines, here i just list 2 lines)
How should i get the 2nd field when 1st field eq c_d_e_. my way is:
`cat a.txt | grep c_d_e_ | cut -f2 -d' '`
Is there simple way, more efficient way in regular expression, perl? awk? and etc.
Thanks
Are you saying the fields are split on spaces?
If so, it's trivial with awk or Perl and even with just bash:
while read line
do
set $line
echo $2
done
frans
3
better (don't use cat with grep !)
grep c_d_e_ a.txt | cut f2 -d' '
Examples of awk and sed would be
$(awk '/c_d_e_/{print $2}' a.txt)
or
$(sed -n 's/^c_d_e_ *//p' a.txt)
Thanks Scrutinizer, frans, Tony.
to be even more specific since you are checking field 1
awk '$1=="c_d_e_"{print $2}' file