Quick Question: counting columns?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Quick Question: counting columns?
# 1  
Old 06-06-2013
Quick Question: counting columns?

Hi everyone

I have a quick question... I have an ascii file that looks like the one below, and I wanted to find a way to make a listing of how many columns there is in each row, similar to the example below.

Anyone has any ideas of how I can do this?

Thanks!!
Code:
6 Sep 2008 -158.535 33.6617
3 Sep 2006 -159.525
25 Jul 2008 -147.8617 33.7017
17 2009 -167.39 33.7417
14 Aug 2006 -159.24 33.76
18 Aug -167.4133 33.7617
5 Sep 2004 -149.995 33.8167

List of columns per line:
Code:
5
4
5
4
5
4
5


Last edited by Scott; 06-06-2013 at 03:19 PM.. Reason: Code tags, please...
# 2  
Old 06-06-2013
AWK's NF variable shows this.

Code:
awk '{print NF}' file

You can use a shell solution if you like:
Code:
while read LINE; do
  set -- $LINE
  echo $#
done < file

# 3  
Old 06-06-2013
Awesome! Thanks Scott! Works like a charm
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Counting number of times content in columns occurs in other files

I need to figure out how many times a location (columns 1 and 2) is present within a group of files. I figured using a combination of 'while read' and 'grep' I could count the number of instances but its not working for me. cat file.txt | while read line do grep $line *08-new.txt | wc -l... (6 Replies)
Discussion started by: ncwxpanther
6 Replies

2. Red Hat

Counting columns in a delimited record with NULLs

I am trying to count the number of columns in a delimited flat file. The record is tab delimited. When I use the command wc -w, I am getting unexpected results when the record contains a NULL value. I believe it is because there is a "word" missing. The below example will return 4 words... (2 Replies)
Discussion started by: nmalencia
2 Replies

3. Shell Programming and Scripting

Quick question

When I have a file like this: 0084AF aj-123-a NAME Ajay NAME Kumar Engineer 015ED6 ck-345-c 020B25 ef-456-e 027458 pq-890-p NAME Peter NAME Salob Doctor 0318F0 xy-123-x NAME Xavier Arul NAME Yesu Supervisor 0344CA de-456-d where - The first NAME is followed by... (6 Replies)
Discussion started by: ajay41aj
6 Replies

4. UNIX for Dummies Questions & Answers

Quick question.

I'd like to list all userid's on the system that have a .bashrc file in their home directory with a command like "cat /etc/passwd | grep -f", however I'm not quite familiar with using grep. Any suggestions? (2 Replies)
Discussion started by: raidkridley
2 Replies

5. Shell Programming and Scripting

quick question

I am using sed to find a pattern in a line and then I want to retain the pattern + the rest of the line. How is this possible? ie: line is: 14158 05-15-08 20:00 123-1234-A21/deliverable/dhm.a search for 123-1234-A21 ie: echo $line | sed 's/.*\(\{3\}-\{4\}-\{3\}\{5\}\).*/\1/' ... (1 Reply)
Discussion started by: phreezr
1 Replies

6. UNIX for Dummies Questions & Answers

quick question

from command prompt I did grep two words on a same line for eg: grep abc | grep xyz and I got tht particular line, but I want to know when I vi that file how to directly search for that particular line? I appreciate if any one can provide answer, thanks in advance (2 Replies)
Discussion started by: pkolishetty
2 Replies

7. Shell Programming and Scripting

Ok quick question

Hi i just wanted to know is there anyway to log the keystrokes on a remote computer? For example i let my nieces play on my other computer downstairs *my computer and the one downstairs are on a LAN* and i want to see everything they type in to make sure they arent doing anything they are supposed... (1 Reply)
Discussion started by: Corrail
1 Replies

8. UNIX for Dummies Questions & Answers

Another quick question

Hi guys sed -e "s/$<//g" the $< can allow me to assign an input value to the variable right? do the double quotes check the previous context? (1 Reply)
Discussion started by: hamoudzz
1 Replies

9. UNIX for Dummies Questions & Answers

quick question

hi guys trying to understand what this line means sed is a stream editor and i understand that, i have a file already selected i want to edit so i use -e sed -e the next stesp is s/$* s is a subsititute replacement sed -e s/$*//g $ is in reference of the last line /g makes it... (2 Replies)
Discussion started by: hamoudzz
2 Replies

10. UNIX for Dummies Questions & Answers

Quick Question

Hello There! I am trying to write this SIMPLE script in Bourne Shell but I keep on getting syntax errors. Can you see what I am doing wrong? I've done this before but I don't see the difference. I am simply trying to take the day of the week from our system and when the teachers sign on I want... (7 Replies)
Discussion started by: catbad
7 Replies
Login or Register to Ask a Question