Different field number in the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Different field number in the file
# 1  
Old 10-05-2016
Different field number in the file

Hello Friends

I have a data file which is comma seperate (,) where i am expecting 2 column but there are number of time when file comes with data having more than 2 column.

I want to check which line has more columns

Code:
20141115,15/11/2014
20141129,29/11/2014
20141003,03/10/2014
20141016,16/10/2014
20140403,03/04/2014
20140415,15/04/2014
20161023,23/10/2016
20161105,05/11/2016
20161205,05/12/2016
20161218,18/12/2016
20170120,20/01/2017,1,4
20170202,02/02/2017
20170302,02/03/2017
20151026,26/10/2015
20151108,08/11/2015
20151208,08/12/2015
20150808,08/08/2015,1,3

Like in above data it is visible that last line and 7th line from bottom has more than 2 column

Please help

Last edited by jim mcnamara; 10-05-2016 at 08:15 AM.. Reason: fixed code tag
# 2  
Old 10-05-2016
Code:
# show lines 
awk -F ','  'NF>2 {print NR, $0}'  filename
# or count lines
awk -F ','  'NF>2 {sum+=1} END {print sum}'  filename

# 3  
Old 10-05-2016
Hi I tried this
Code:
awk -F ','  'NF>2 {print NR, $0}' test.txt
awk: syntax error near line 1
awk: bailing out near line 1
tdc2solv112:kumarra# ls -ltr

Code:
tdc2solv112:kumarra# awk -F ','  'NF>2 {sum+=1} END {print sum}' test.txt
awk: syntax error near line 1
awk: bailing out near line 1

Moderator's Comments:
Mod Comment Please use code tags

Last edited by jim mcnamara; 10-05-2016 at 08:30 AM..
# 4  
Old 10-05-2016
I get no errors, either with Solaris 10 (nawk), or cygwin64

Something else is going on. Try typing the command instead of using cut and paste.

What OS name and version are you on?
# 5  
Old 10-05-2016
Many thanks

I corrected it

Code:
 awk -F',' 'NF>2 {print NR, $0}' test.txt

I just removed the space and it worked


Moderator's Comments:
Mod Comment
Please wrap all code, files, input & output/errors in code tags.
It makes them far easier to read and preserves spaces for indenting or fixed width data.

Last edited by rbatte1; 10-05-2016 at 08:32 AM.. Reason: Added CODE tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort file by field 1 that has text as well as a number

I am using the below awk that results in the below output: awk '{k=$1 OFS $2; s+=$4; c++} END{for(i in s) print i, s/c}' input.txt > output.txt output.txt chr20:43625799-43625957 STK4:exon.6;STK4:exon.7 310.703 chr20:36770455-36770611 TGM2:exon.6;TGM2:exon.7 614.756... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

3. Shell Programming and Scripting

Replace a field with line number in file

I am working on a script to convert bank data to a csv file. I have the format done - columns etc. The final piece of the puzzle is to change the second field (after the R) of every line to reflect its' line number in the file. I am stumped. I can use awk on each line but need help looping through... (9 Replies)
Discussion started by: Melah Gindi
9 Replies

4. Shell Programming and Scripting

Changed Field Number

HI, I have two files and contains many Fields or columns with | (pipe) delimitor, wanted to compare both the files and get only unmatched perticular fields number. ex: first.txt 1 | 2 | 3 111 |abc| 230 hbc | bb2 | cs second.txt 1 | 2 | 3 111 |abc |230 abn | bb2 | fp Here the... (7 Replies)
Discussion started by: prawinmca
7 Replies

5. UNIX for Dummies Questions & Answers

Inserting a sequential number into a field on a flat file

I have a csv flatfile with a few million rows. I need to replace a field (field number is 85) in the file with a sequential number. As an example, let's assume there are only 4 fields in the file: A,A,,32 A,A,,27 A,B,,43 C,C,,354 If I wanted to amend the 3rd field in this way my... (2 Replies)
Discussion started by: BristolSmithy
2 Replies

6. Shell Programming and Scripting

Adding total of first field for each number in the second field

Dears, I need a script or command which can find the unique number from the second filed and against that number it adds the total of first field . 17215630 , 0 907043 ,1 201050 ,10 394149 ,4 1964 ,9 17215630, 0 907043 ,1 201050, 10 394149 ,4 1964 ,9 1234234, 55 23 ,100 33 ,67 ... (2 Replies)
Discussion started by: shary
2 Replies

7. Shell Programming and Scripting

find the field number

######################## SOLVED ################## Hi I have a header file like the following and the field "IDENTIFIER" can be at any possition on this line, The line can containt a variable number of field, not alway the same depending of the header file i use ... (6 Replies)
Discussion started by: kykyboss023
6 Replies

8. Shell Programming and Scripting

Count number of occurences of a character in a field defined by the character in another field

Hello, I have a text file with n lines in the following format (9 column fields): Example: contig00012 149606 G C 49 68 60 18 c$cccccacccccccccc^c I need to count the number of lower-case and upper-case occurences in column 9, respectively, of the... (3 Replies)
Discussion started by: s052866
3 Replies

9. Shell Programming and Scripting

Sorting on two fields time field and number field

Hi, I have a file that has data in it that says 00:01:48.233 1212 00:01:56.233 345 00:09:01.221 5678 00:12:23.321 93444 The file has more line than this but i just wanted to put in a snippet to ask how I would get the highest number with time stamp into another file. So from the above... (2 Replies)
Discussion started by: pat4519
2 Replies

10. Shell Programming and Scripting

How to search a number in a certain field

Hello, I have a file that looks like this: num11 num12 num13 word1 num21 num22 num23 word2 num31 num32 num33 word3 . . . I would like to search for the lines that contain a given number, but I need to narrow the search only to the first field, that means that the number has to... (4 Replies)
Discussion started by: shira
4 Replies
Login or Register to Ask a Question