awk count fields not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk count fields not working
# 1  
Old 02-02-2013
awk count fields not working

Hi,

i am trying to count the fields in a file.
Input:
Code:
100,1000,,2000,3000,10/26/2012 12:12:30
200,3000,,1000,01/28/2012 17:12:30
300,5000,,5000,7000,09/06/2012 16:12:30

output:
Cout of the fileds for each row
Code:
6
5
6

Code:
awk -F"," '{print $NF}' file1.txt

When i try with above awk command i am getting the below output and it is not working as expected.
Code:
12:12:30
17:12:30
16:12:30

Any suggestion using awk command for couting the fields.

Last edited by Scott; 02-02-2013 at 03:54 AM.. Reason: More code tags
# 2  
Old 02-02-2013
Use NF instead of $NF.

But your output with $NF is strange, given the field separator you've used.
# 3  
Old 02-02-2013
thanks for swift responce.
i can find the total fileds in the row which is not equal to 6
Code:
awk -F"," '{print NF","$0}' |grep -v "^6"

Input
Code:
100,1000,,2000,3000,10/26/2012 12:12:30
200,3000,,1000,01/28/2012 17:12:30
300,5000,,5000,7000,09/06/2012 16:12:30

Output
Code:
5,200,3000,,1000,01/28/2012 17:12:30

i unable to print the only values not include the count
Code:
required output : fileds not having 6 and print only values and not include 
200,3000,,1000,01/28/2012 17:12:30

i want to print only data not count in the output.

Last edited by Scott; 02-02-2013 at 01:34 PM.. Reason: As RudiC suggest, please use code tags for code and data
# 4  
Old 02-02-2013
Please use code tags for data as well!
Try
Code:
awk -F"," 'NF!=6'
200,3000,,1000,01/28/2012 17:12:30

No need for grep. awk will check the field count, and, if eq. 6, print the line, otherwise, not.
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 remove lines where field count is greather than 1 in two fields

I am trying to remove all the lines and spaces where the count in $4 or $5 is greater than 1 (more than 1 letter). The file and the output are tab-delimited. Thank you :). file X 5811530 . G C NLGN4X 17 10544696 . GA G MYH3 9 96439004 . C ... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. Shell Programming and Scripting

awk to output match and mismatch with count using specific fields

In the below awk I am trying output to one file those lines that match between $2,$3,$4 of file1 and file2 with the count in (). I am also trying to output those lines that are missing between $2,$3,$4 of file1 and file2 with the count of in () each. Both input files are tab-delimited, but the... (7 Replies)
Discussion started by: cmccabe
7 Replies

3. Shell Programming and Scripting

awk to count and rename based on fields

In the below awk using the tab-delimited input, I am trying count the - symbol in $5 and output the count as well as the renamed condition ins. I am also count the - symbol in $6 and output the count as well as the renamed condition del. I am also count the tomes that in $5 and $6 there are... (6 Replies)
Discussion started by: cmccabe
6 Replies

4. Shell Programming and Scripting

awk sort based on difference of fields and print all fields

Hi I have a file as below <field1> <field2> <field3> ... <field_num1> <field_num2> Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields. I tried this and it doesn't sort on the difference field .. Appreciate your help. cat... (9 Replies)
Discussion started by: newstart
9 Replies

5. Shell Programming and Scripting

Speed : awk command to count the occurrences of fields from one file present in the other file

Hi, file1.txt AAA BBB CCC DDD file2.txt abc|AAA|AAAabcbcs|fnwufnq bca|nwruqf|AAA|fwfwwefwef fmimwe|BBB|fnqwufw|wufbqw wcdbi|CCC|wefnwin|wfwwf DDD|wabvfav|wqef|fwbwqfwfe i need the count of rows of file1.txt present in the file2.txt required output: AAA 2 (10 Replies)
Discussion started by: mdkm
10 Replies

6. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

7. Shell Programming and Scripting

awk - count character count of fields

Hello All, I got a requirement when I was working with a file. Say the file has unloads of data from a table in the form 1|121|asda|434|thesi|2012|05|24| 1|343|unit|09|best|2012|11|5| I was put into a scenario where I need the field count in all the lines in that file. It was simply... (6 Replies)
Discussion started by: PikK45
6 Replies

8. UNIX for Dummies Questions & Answers

count number of fields not using SED or AWK

hi forums i need help with a little problem i am having. i need to count the number of fields that are in a saved variable so i can use that number to make a different function work properly. is there a way of doing this without using SED/AWK? anything would be greatly appreciated (4 Replies)
Discussion started by: strasner
4 Replies

9. Shell Programming and Scripting

awk sed cut? to rearrange random number of fields into 3 fields

I'm working on formatting some attendance data to meet a vendors requirements to upload to their system. With some help on the forums here, I have the data close. But they've since changed what they want. The vendor wants me to submit three fields to them. Field 1 is the studentid field,... (4 Replies)
Discussion started by: axo959
4 Replies

10. UNIX for Dummies Questions & Answers

count fields

Is there a way to count the no. of fields (columns) in a file? Actually I need to cut some fields starting from the middle to the end. How can I specify to cut till last field? thanks in advance :) (4 Replies)
Discussion started by: sskb
4 Replies
Login or Register to Ask a Question