Print first field using awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Print first field using awk
# 1  
Old 05-16-2014
Print first field using awk

I want to print line by line only the first field from txt file

Code:
input file

etr,t7tu,e45xdt,e45exgt,cdgfe
aqw34aw,45edgf,45estd,sert34
a232e,4etedf,w345er,qw345rw,
qw354,q34asf,tw45f,q3drsf

Code:
required o/p file

etr
aqw34aw
a232e
qw354

but my code printing like below

Code:
etr
etr
etr
etr
aqw34aw
aqw34aw
aqw34aw
aqw34aw
a232e
a232e
a232e
a232e
qw354
qw354
qw354
qw354
qw354

my code

Code:
#!/bin/ksh
/usr/xpg4/bin/awk -F, '{for(i=1;i<=NF;i++) {print $1}}' inputfile >>outputfile

# 2  
Old 05-16-2014
Quote:
Originally Posted by stew
I want to print line by line only the first field from txt file

Code:
input file

etr,t7tu,e45xdt,e45exgt,cdgfe
aqw34aw,45edgf,45estd,sert34
a232e,4etedf,w345er,qw345rw,
qw354,q34asf,tw45f,q3drsf

Code:
required o/p file

etr
aqw34aw
a232e
qw354

but my code printing like below

Code:
etr
etr
etr
etr
aqw34aw
aqw34aw
aqw34aw
aqw34aw
a232e
a232e
a232e
a232e
qw354
qw354
qw354
qw354
qw354

my code

Code:
#!/bin/ksh
/usr/xpg4/bin/awk -F, '{for(i=1;i<=NF;i++) {print $1}}' inputfile >>outputfile

You didn't get the output you showed us above from the input and code shown above!
To get the output you said you want from the input shown, try:
Code:
#!/bin/ksh
/usr/xpg4/bin/awk -F, '{print $1}' inputfile > outputfile

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print unique text in field

I am trying to use awk to print the unique entries in $2 So in the example below there are 3 lines but 2 of the lines match in $2 so only one is used in the output. File.txt chr17:29667512-29667673 NF1:exon.1;NF1:exon.2;NF1:exon.38;NF1:exon.4;NF1:exon.46;NF1:exon.47 703.807... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. UNIX for Dummies Questions & Answers

Print first field in awk

Hi, I have below text file 01.02.2014,asdas,arse,aere,4tfsd 12.03.2014,sdte,45gf,8iuj,qw343w 01.02.0214,aetre,sdfgter,asfrwe I have writen below code to print only first field that is only date field from text file #!/bin/ksh echo "enter week" read week while read -r... (6 Replies)
Discussion started by: stew
6 Replies

3. UNIX for Dummies Questions & Answers

AWK print last field including SPACES

I have simple test.sh script, see below: bill_code=`echo $record | awk -F"|" '{print $1}'` Fullname=`echo $record | awk -F"|" '{print $3}'` email=`echo $record | awk -F\ '{print $4}'` The last field contains spaces: see csv below: A0222|Y|DELACRUZ|-cc dell@yahoo.com-cc support@yahoo.com ... (9 Replies)
Discussion started by: quay
9 Replies

4. Shell Programming and Scripting

Using awk, print all the lines where field 8 is equal to x

Using awk, print all the lines where field 8 is equal to x I really did try, but this awk thing is really hard to figure out. file1.txt"Georgia","Atlanta","2011-11-02","x","","","","" "California","Los Angeles","2011-11-03","x","","","",""... (2 Replies)
Discussion started by: charles33
2 Replies

5. Shell Programming and Scripting

awk - print all fields except for last field

How do I print all the fields of a record except for the $(NF) field? (4 Replies)
Discussion started by: locoroco
4 Replies

6. UNIX for Advanced & Expert Users

Awk command to print the field

894344202808090;11122;040320 075858 166;101;0;0;10u;0;NA;65;221890;2;101973;185059;568674;Y; PRE;0;0;NA;NA;0;NA;0;NA;textmsg;textmsg_snd1;telusmob;TEXTMSG1;0.15000000;126037;2010/03/04 12:58:57gmt;0;70532192; plz tell me any awk command which on the basis of the yellow field which is... (1 Reply)
Discussion started by: madfox
1 Replies

7. Shell Programming and Scripting

awk to print exact field number

Hello there. I want some help with awk I have this script that reads a file from awk and you can insert from keyboard any list from the fields that exist so to be printed on monitor echo "give a choice" read -a ans array=${ans} awk -F: -v k="$array" '{ ... (4 Replies)
Discussion started by: covis
4 Replies

8. Shell Programming and Scripting

awk to print field no 10 and complete event

hi, awk -F ";" '{if($10>500) then i++; } END{print i}' event_file_FR12_202_24-11-2009_* | less How to modify and add to this above to print the complete line in less ex : 4712714002021527;15298888;241129 095614... (1 Reply)
Discussion started by: madfox
1 Replies

9. UNIX for Dummies Questions & Answers

[awk] print from field n to field x

Hi, I'm trying to print every line from first field to the fourth from a file containing more. $ cat input a b c d e f g a b c d e f gI'm trying awk '{for (i=1; i <= NF-3; i++) print $i}' awkTest.datbut it printsa b c d a b c dSo, I easily guess I'm wrong. :) Of course, I want:a b... (5 Replies)
Discussion started by: daPeach
5 Replies

10. Shell Programming and Scripting

Print matching field using awk

Hi All, I have a string like below: str="Hold=True Map=False 'This will map the data' Run=Yes Modify=False" I want to print the field Run=Yes and retrive the value "Yes". I cannot use simple awk command because the position of the "Run" will be different at different times. Is there a way... (6 Replies)
Discussion started by: deepakgang
6 Replies
Login or Register to Ask a Question