Print first field in awk


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

Hi,

I have below text file

Code:
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

Code:
#!/bin/ksh
echo "enter week"
read week
while  read -r line
do
ffld=${line}|/usr/xpg4/bin/awk 'BEGIN { FS = "," } ;{print $1}'
echo $ffld
done < inputfile >> file

Want to print like below

Code:
output file
 
01.02.2014
12.03.2014
01.02.0214

but the ffld taking the complete row
Code:
12.03.2014,sdte,45gf,8iuj,qw343w

and o/p is not printing, the file is empty.
# 2  
Old 05-14-2014
Hello,

Could you please use the following code.

Code:
awk -F, '{print $1}' print_first_files_check12112

Output will be as follows.

Code:
01.02.2014
12.03.2014
01.02.0214


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 05-14-2014
I tried below code

Code:
#!/bin/ksh
echo "enter week"
read week
while  read -r line
do
sdate=${line}|/usr/xpg4/bin/awk -F, '{print $1}'
echo $sdate
done< inputfile >> file

still its printing empty file
# 4  
Old 05-14-2014
Hello,

Could you please let us know why you are getting week as input here ?


Thanks,
R. Singh
# 5  
Old 05-14-2014
try
Code:
#!/bin/ksh
echo "enter week"
read week
while  read -r line
do
sdate=`echo ${line}| nawk -F, '{print $1}'`
echo $sdate
done< inputfile >> file

Also no need to write while loop
Code:
nawk -F, '{print $1}' inputfile > file

# 6  
Old 05-14-2014
Use parameter substitution instead:
Code:
while read line
do
        echo "${line%%,*}"
done < inputfile > outputfile

# 7  
Old 05-14-2014
I'm a bit confused. From your input file, do you just want to grab the first column into another file?
Code:
cut -f1 -d, infile > outfile

...or do you want to process them in a loop:-
Code:
for val in `cut -f1 -d, infile`
do
   # whatever you like
done > outfile

.... and what do you do with the value read in as week anyway?




Regards,
Robin
 
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 using awk

I want to print line by line only the first field from txt file input file etr,t7tu,e45xdt,e45exgt,cdgfe aqw34aw,45edgf,45estd,sert34 a232e,4etedf,w345er,qw345rw, qw354,q34asf,tw45f,q3drsf required o/p file etr aqw34aw a232e qw354 (1 Reply)
Discussion started by: stew
1 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