help me to count no of fields in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help me to count no of fields in a file
# 1  
Old 10-30-2006
Network help me to count no of fields in a file

hi
i am a new unix user
i want to check whether a file contains spacefied no of fields
if so i should delete last fields
and then insert some fields in 2nd field
please help me
Thanks Regards
babu Smilie
# 2  
Old 10-30-2006
Try awk's NF(no. of fields) system variable, this will give you a start:
Code:
echo "A,B,C,D" | awk -F, '{print NF}'

# 3  
Old 10-30-2006
echo "A,B,C,D" | awk -F, '{print NF}'

Here NF signifies number of fields.

Each field of the line will be marked as $1, $2 etc

you can use a loop , if condition to print the required fields.
# 4  
Old 10-30-2006
hi motor
thanks for ur quick reply
i should check with NF >=100
if it is so i shoul del last two fields
# 5  
Old 10-30-2006
One way to do that:
Code:
#! /bin/ksh

IFS=","
str="A,B,C,D,E,F,G,H,I,J"
set -A arr $str
fields=${#arr[*]}
var=""
if [ $fields -ge 10 ]; then
        i=0
        while ((i < $fields-2)); do
                var=$var,${arr[$i]}
                ((i=i+1))
        done
        echo $var | tr ' ' "," | sed 's/^,//g'
fi

Output:
Code:
$./test.sh
A,B,C,D,E,F,G,H

Maybe someone else could come up with simpler code.

Regards,
Tayyab
# 6  
Old 10-30-2006
Quote:
Originally Posted by babu@shell
i should check with NF >=100
if it is so i shoul del last two fields
You can do something like tihis (tested with mawk) :
Code:
mawk 'NF>=100 { NF -=2; print $0}' unputfile

You can also do do :
Code:
awk 'NF>=100 { $NF=""; $(NF-1)=""; print $0}' inputfile

Jean-Pierre.
# 7  
Old 10-30-2006
Quote:
Originally Posted by aigles
You can do something like tihis (tested with mawk) :
Code:
mawk 'NF>=100 { NF -=2; print $0}' unputfile

You can also do do :
Code:
awk 'NF>=100 { $NF=""; $(NF-1)=""; print $0}' inputfile

Jean-Pierre.
That was a simpler way Smilie Out of interest, I never heard about mawk before, so you mean we can change values of awk's system variables in mawk like you did NF -= 2.

Regards,
Tayyab
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to count lines of CSV file where 2 fields match variables?

I'm trying to use awk to count the occurrences of two matching fields of a CSV file. For instance, for data that looks like this... Joe,Blue,Yes,No,High Mike,Blue,Yes,Yes,Low Joe,Red,No,No,Low Joe,Red,Yes,Yes,Low I've been trying to use code like this... countvar=`awk ' $2~/$color/... (4 Replies)
Discussion started by: nmoore2843
4 Replies

2. Shell Programming and Scripting

How To Count Fields For Cut?

I am new to cut and I want to use the field option with a space delimiter on an Apache log file. For example, if I wanted to find the 200 HTTP code using cut in this manner on the file below cat access_abc.log | cut -d' ' -f7 | grep "200" 157.55.39.183 - - "GET /content/696-news041305... (4 Replies)
Discussion started by: sharingsunshine
4 Replies

3. 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

4. Shell Programming and Scripting

awk count fields not working

Hi, i am trying to count the fields in a file. Input: 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 6 5 6 awk -F"," '{print $NF}' file1.txt When i try with above awk... (3 Replies)
Discussion started by: onesuri
3 Replies

5. 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

6. Shell Programming and Scripting

Looping over a file to count common fields from another file

Hi, I would like to know how can I get the number of rows in file1 that: - the 1st and 2nd field should be the same (text) - the 3rd field should be less or equal (numeric) when comparing to file2. So for each row of file1, I would like to have the number of rows in file2 that follow the... (5 Replies)
Discussion started by: fadista
5 Replies

7. UNIX for Dummies Questions & Answers

Count Fields with Quoted Field

Hi, I used to count number of fields using following command head -1 <filename> | awk -F"," '{print NF}' Now the scenario is the delimiter(comma) occurs inside one of the data field. How to ignore the comma inside data and consider only delimiter and count number of fields. The fields are... (1 Reply)
Discussion started by: ethanr100
1 Replies

8. Shell Programming and Scripting

Count the number of fields in column

Hi I was going through the below thread https://www.unix.com/shell-programming-scripting/48535-how-count-number-fields-record.html I too have something similar requirement as specified in this thread but the number of columns in my case can be very high, so I am getting following error. ... (3 Replies)
Discussion started by: shekharjchandra
3 Replies

9. Shell Programming and Scripting

Get count on different fields along the raws in a file

Dear All, Please help me to do this. I have a file like this. 5|94662240807|94776109911|94776325901|94779007172|||||| 5|94112925421|94352240384|94352259199|94672229012|||||| 5|94714242745|94722952461|94777660793|94788914465|||||| 5|94242224624|94776145420|94776172499|94776531059|||||| ... (7 Replies)
Discussion started by: Nayanajith
7 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