I have a input file FILE1.TXT as below.
11 id1
12
13 AGE = 20
14 NAME = NAME1
15
16 id2
17
18 AGE = 30
19 NAME = NAME2
.
.
.
110 idXYZ
111
112 AGE = AGEXYZ
113 NAME = NAMEXYZ
114
115 idZZZ
116
I want to search all the fields that belong to a particular Id and get the value for NAME
I managed to loop through each Id and formed the below command for each Id as required.
sed -n '/11/,/14/p' FILE1.TXT | grep NAME | awk -F "= " '{print $2}'
The problem here is, I get the output NAME1, in addition to that, I also get NAMEXYZ.
What should be changed so that I only get NAME1 but not NAMEXYZ?
As a workaround, the below commands work.
sed -n '/11/,/14/p' FILE1.TXT | grep NAME | awk -F "= " '{print $2}'|head -1
Is there any 'switch' or am I missing something?
NAME
between the lines 11 and 14. So, why issed
looking at111
and114
? How to make it not look in between111
and114
? – Vinay Mar 03 '14 at 17:11sed
expression. – devnull Mar 03 '14 at 17:12grep
with-w
flag ? isn't it ? – Rahul Patil Mar 03 '14 at 17:31-w
would be equivalent. For thesed
example,-w
is slightly different. – devnull Mar 03 '14 at 17:33awk
answer. – devnull Mar 03 '14 at 19:10