Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extracting first line from file using awk command

Tags:

unix

awk

I've been going through an online UNIX course and have come across this question which I'm stuck on. Would appreciate any help!

You are provided with a set of files each one of which contains personal details about an individual. Each file is laid out in the following format, with one file per individual:

name:Niko Tanaka 
age:41 
occupation:Doctor

I know the answer has to be in the form:

n=$(awk -F: '   /   /{print }'  filename)
like image 805
methuselah Avatar asked Jan 29 '12 22:01

methuselah


1 Answers

n=$(awk -F: '/name/{print $2}' infile)

Whatever is inside of / / are regular expressions. In this case you just want to match on the line that contains 'name'.

like image 176
SiegeX Avatar answered Oct 15 '22 07:10

SiegeX