simple awk question to count columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting simple awk question to count columns
# 1  
Old 01-22-2009
simple awk question to count columns

hi all here is a very simple question.. i want to count the number of columns using awk..my file looks like this:

Code:
1,2
1,2
1,2
1,2
1,2
1,2
1,2 
1,2

i want to count number of columns and i so far i have:

Code:
awk 'BEGIN {IFS=","} END {print NF}' data  > data1

i am getting 1 as my result.. what am i missing here? thanks
# 2  
Old 01-22-2009
ok i got it..

Code:
awk 'BEGIN {FS=","} ; END{print NF}' data  > data1

got confused with nawk..
# 3  
Old 01-22-2009
Code:
awk 'BEGIN {FS=","} END {print NF}' datain  >dataout

I hope it works. At least in Linux it does.
Regards.

Edit: Upss, too late Smilie
# 4  
Old 01-22-2009
Quote:
Originally Posted by npatwardhan
ok i got it..

Code:
awk 'BEGIN {FS=","} ; END{print NF}' data  > data1

got confused with nawk..
Warning: for some awk-s, the value of 'NF' is not visible in the 'END' block.

Code:
nawk -F',' '{print NF;exit}' data  > data1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Simple awk question

Here is an awk line I have in a bigger script that checks to see if nimsh process is running and does couple other things based on the output and runs on all servers. ps -ef|grep -i nimsh|awk '{print $9}' and I am expecting output to be "/usr/sbin/nimsh" I find that on some servers... (4 Replies)
Discussion started by: kvosu
4 Replies

2. UNIX for Beginners Questions & Answers

Simple question about charecter count

Hi, I have collection of letters in a column such as: AA5678 AA9873434 .. .. I am trying to find the number of charecters in each. "echo "AA5678"|wc -c 7----------------> why does it give 7 instead of 6? (6 Replies)
Discussion started by: kvosu
6 Replies

3. Programming

awk to count occurrence of strings and loop for multiple columns

Hi all, If i would like to process a file input as below: col1 col2 col3 ...col100 1 A C E A ... 3 D E G A 5 T T A A 6 D C A G how can i perform a for loop to count the occurences of letters in each column? (just like uniq -c ) in every column. on top of that, i would also like... (8 Replies)
Discussion started by: iling14
8 Replies

4. Shell Programming and Scripting

awk help - Simple question

I have what a think is a simple question but I'm just a beginner in scripting. I'm my unix command line I run a date command that returns the following: Wed Apr 3 10:39:30 EDT 2013 How do I awk out the "10" only in awk? Or is awk the way to do it or is there a better way? (7 Replies)
Discussion started by: scj2012
7 Replies

5. Shell Programming and Scripting

Simple awk question

Hi All, A very silly question: How to just print the integer attached a particular string in a line by awk? Ex: Happy_world_foo123...So i just want the value 123 to be printed on the line where we have string "foo". Thanks (16 Replies)
Discussion started by: Indra2011
16 Replies

6. Shell Programming and Scripting

count the number of instances in 2 columns using awk

Input A.1 Q.1 A.1 Q.2 A.1 Q.3 A.2 Q.4 Explanation: Final Output A.1 Q.1 s1 t1 A.1 Q.2 s1 t2 A.1 Q.3 s1 t3 A.2 Q.4 s5 t5 ---------- Post updated 09-28-12 at 03:38 AM ---------- Previous update was 09-27-12 at 09:10 AM ---------- Hi Guys, I was able to do until... (11 Replies)
Discussion started by: quincyjones
11 Replies

7. UNIX for Dummies Questions & Answers

awk simple question

Can anyone tell me please what the "+" is doing in this awk command? find / -user smith -type f -ls | awk '{ sum += $7 } END {print sum }' Thanks, George (2 Replies)
Discussion started by: george_vandelet
2 Replies

8. UNIX for Dummies Questions & Answers

Simple AWK question

Hi, let's assume i have an output below: orgauser 23826 :E:Validity senerse 2096 senerse 2111 senerse 21585 senerse 21596 root 12653 -bash root 17262 root 17278 Some lines have not any string in their third column. I don't want to see those lines. i just want to see the lines... (3 Replies)
Discussion started by: oduth
3 Replies

9. UNIX for Dummies Questions & Answers

simple awk question

Hi , I have a simple question in awk, i have long string which i am getting for a grep command. the output contains 50 fields. I need to display like first 5 fileds in a line and rest of all fields in the next line. { for(i=5;i<NF;++i) s= $i; print $1,$2,$3,$4,$5,"\n",$s} Is the above... (1 Reply)
Discussion started by: senthilkumar_ak
1 Replies

10. UNIX for Dummies Questions & Answers

simple awk question

Hello, I'm trying to use awk to print lines that match a regular expression. I am using awk to print a record only if it contains N/A. awk '/N/A/ {print $1}' When executed the script returns "awk syntax error near line 1". If I use /N//A/ it prints all records containing a "/", not... (2 Replies)
Discussion started by: orahi001
2 Replies
Login or Register to Ask a Question