Help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help
# 1  
Old 02-06-2004
Help

I have a csv file, which is comma delimited. It has 300 fields, but not all are populated with data.

What I need to do is run a script that would look at each field and if it is populated then print the contents along with the field number, i.e. Joe Bloggs - 2 (Joe Bloggs is the data and 2 is the field it was in).

I've tried loads of things, but I can't get it right? So a fresh start is probably needed.

Thanks in advance
# 2  
Old 02-06-2004
This should work. It counts the number of fields and iterates through each one, only printing it if it's not empty. Smilie
Code:
awk -F, '{for (i=1; i<=NF; i+=1) if ($i != "") print "Field "i" - value is " $i}' your_file

Actually, to get the exact format you wanted:
Code:
awk -F, '{for (i=1; i<=NF; i+=1) if ($i != "") print $i " - " i}' your_file

# 3  
Old 02-06-2004
Thanks for this.

Is it possible to print all the populated fields found on each line in one continuous line. Rather then on a new line when one is found, if this makes sense.
# 4  
Old 02-06-2004
Code:
awk -F, '{for (i=1; i<=NF; i+=1) if ($i != "") printf $i " " } END {print ""}' your_file

# 5  
Old 02-06-2004
Brilliant thanks very much
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question