LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   printing hh in hh:mm using "awk '{FS=":";print $1}'" misses first line of output!! (https://www.linuxquestions.org/questions/linux-newbie-8/printing-hh-in-hh-mm-using-awk-%7Bfs%3D-%3Bprint-%241%7D-misses-first-line-of-output-773133/)

mayankmehta83 12-03-2009 02:48 AM

printing hh in hh:mm using "awk '{FS=":";print $1}'" misses first line of output!!
 
input -->
% cat file
11:30
12:20
13:10

unexpected output :( -->
% awk '{FS=":";print $1}' file
11:30
12
13

expected output :) -->
% awk '??' file
11
12
13

Please suggest what's wrong with the command. Thanks in advance.

Also consider,

% awk '{ if(NR>1) {FS=":";print $1} }' file
12:20
13

It is as if awk has decided not to work on 1st row of input supplied !!

Tinkster 12-03-2009 02:54 AM

Hi, welcome to LQ!

2 options.

Put
Code:

awk -F: '{....'
or
awk 'BEGIN{FS=":"}{ ... '


druuna 12-03-2009 02:55 AM

Hi,

awk -F: '{ print $1 }' infile

or

awk 'BEGIN { FS=":" } { print $1 }' infile

Hope this helps.


All times are GMT -5. The time now is 12:08 PM.