Number of columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Number of columns
# 1  
Old 01-05-2017
Number of columns

Hi All

I am using the below code to check the number of columns in a delimited file are same as expected. Code is give below

Code:
awk -F '|' -v line=$2 'NF!=line {print NF,$0}' file

Here the challenge is that , delimiter is different for each file, it can be pipe, comma, or semi colon.I want to pass the delimiter value into the above awk command using external parameter.Currently I am passing the number of fields as external parameter.

Kindly help me on this
# 2  
Old 01-05-2017
Hello ginrkf,

Here is an example for this you could set it as follows.
Code:
VAL="|"
awk '{print $1}' FS="$VAL" Input_file

Similarly you could set it for any file as per your need. As you haven't shown us complete requirement so one could take it as a starting point, let me know if this helps.

Thanks,
R. Singh
# 3  
Old 01-05-2017
Actually my requirement is to find out where the number of columns present in the file is same a predefined.

I have two files A & B

A is suppose to have three columns and is a pipe delimited file

Code:
1|2|3
3|4
5|6|7

I want to write a awk command to find the records where number of columns are not equal to three.So if run my awk command to the file A, I should get out put as

Code:
3|4

B is suppose to have two columns and is a comma delimiter file

Code:
1,2
3,4
5

I want to write a awk command to find the records where number of columns are not equal to two.So if run my awk command to the file B, I should get out put as

Code:
5

What I need is a command command where I will be able to pass the number of columns and delimiter as external parameter while running the script.If you see my below script I am passing number of columns as a parameter

Code:
awk -F '|' -v line=$2 'NF!=line {print NF,$0}' file

In addition to that I need to pass the delimiter also as a parameter. I tried like below, but not working

Code:
VAL=$1 awk -v line=$2 'NF!=line {print NF,$0}' FS=$VAL file

# 4  
Old 01-05-2017
You also want to pass the filename.?
Code:
awk -F "$3" -v line="$2" 'NF!=line {print NF,$0}' "$1"

Put this in a script or in a function.
And run it like this
Code:
myfunc "file" 3 "|"

This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 01-05-2017
Quote:
Originally Posted by ginrkf
Actually my requirement is to find out where the number of columns present in the file is same a predefined.

I have two files A & B

A is suppose to have three columns and is a pipe delimited file

Code:
1|2|3
3|4
5|6|7

I want to write a awk command to find the records where number of columns are not equal to three.So if run my awk command to the file A, I should get out put as

Code:
3|4

B is suppose to have two columns and is a comma delimiter file

Code:
1,2
3,4
5

I want to write a awk command to find the records where number of columns are not equal to two.So if run my awk command to the file B, I should get out put as

Code:
5

What I need is a command command where I will be able to pass the number of columns and delimiter as external parameter while running the script.If you see my below script I am passing number of columns as a parameter

Code:
awk -F '|' -v line=$2 'NF!=line {print NF,$0}' file

In addition to that I need to pass the delimiter also as a parameter. I tried like below, but not working

Code:
VAL=$1 awk -v line=$2 'NF!=line {print NF,$0}' FS=$VAL file

If you invoke your script with two parameters where the 1st parameter is the field separator and the 2nd parameter is the expected number of fields, just using:
Code:
awk -F "$1" -v cnt="$2" 'NF!=cnt {print NR,NF,$0}' file

will give you output where each line is the line number of a line with the wrong number of fields, the number of fields found on that line, and the text from that line. Another way to write that awk script and get identical results would be:
Code:
awk 'NF!=cnt {print NR,NF,$0}' FS="$1" cnt="$2" file

These 2 Users Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding columns from 2 files with variable number of columns

I have two files, file1 and file2 who have identical number of rows and columns. However, the script is supposed to be used for for different files and I cannot know the format in advance. Also, the number of columns changes within the file, some rows have more and some less columns (they are... (13 Replies)
Discussion started by: maya3
13 Replies

2. Shell Programming and Scripting

Request: How to Parse dynamic SQL query to pad extra columns to match the fixed number of columns

Hello All, I have a requirement in which i will be given a sql query as input in a file with dynamic number of columns. For example some times i will get 5 columns, some times 8 columns etc up to 20 columns. So my requirement is to generate a output query which will have 20 columns all the... (7 Replies)
Discussion started by: vikas_trl
7 Replies

3. Shell Programming and Scripting

How to check the number of columns in a line??

hi, i have a file with many records and each record may or may not have 6 columns. for example file1 : first second third fourth fifth sixth first second third fourth fifth first second third fourth fifth sixth first second third fourth fifth sixth seventh eigth if i cat the file and... (21 Replies)
Discussion started by: Little
21 Replies

4. Shell Programming and Scripting

Number of columns in xml file

How to find the number of columns in xml file. i tried following command. #!bin/ksh cat $1 | grep -c "</mdm:attribute>" exit 0 but i am not getting accurate result which is not matching with the manual count. data format : <mdm:attribute> value </mdm:attribute> (6 Replies)
Discussion started by: victory
6 Replies

5. Shell Programming and Scripting

Delete rows with particular number of columns

Hello Guys I have a flat file with few thousands of rows. Now each rows have different number of columns I want to delete the rows which has not equal to 749 columns Can you guys please let me know how to do the same Your help is much appreciated. (2 Replies)
Discussion started by: Pratik4891
2 Replies

6. Shell Programming and Scripting

How to subtract a number from all columns?

Hi, I want to subtract a number from all columns except the first column. I have a number of files each having different columns around 60/70. How to do that in awk or any other command? Thanks Input Col 1 Col 2 Col3 - - - - Col55 1 .0123 .098 - - - 0.6728 2 - -... (3 Replies)
Discussion started by: Surabhi_so_mh
3 Replies

7. Shell Programming and Scripting

concatenate 'n' number of columns in a file

i have a file which may have 'n' number of columns 1 222 fafda 32 afdaf 4343 4343 234 43fdaf 4343 fdd fdfd fdfd fdd fdfd fdfd fdfd fdfd fdfd fdd fdfd fdfd need to concatenate the columns with... (3 Replies)
Discussion started by: mlpathir
3 Replies

8. UNIX for Dummies Questions & Answers

Find number of columns in a file

Hi all, may seem a very stupid question.but me stuck up in it for long.... How to find the number of columns in a ASCII file. EX:-Demo.lst songs 1 34 45 67 Number of columns should be 5. Regards, Anindya ;) (13 Replies)
Discussion started by: rahul26
13 Replies

9. Shell Programming and Scripting

number subtraction of multiple columns

I get the point of number subtraction in one column awk 'NR==1 {n=$1; next}; {n-=$1} END {print n}' inputfile but I cannot figure it out how to do this to multiple columns. awkward. (6 Replies)
Discussion started by: awkward
6 Replies

10. UNIX for Dummies Questions & Answers

using Grep for only a certain number of columns

I am given a file with a list of names for the first 20 characters. The 20 characters after is a list of names that somebody is supposed to team up with. Now i'm trying to write a simple script that will read a name somebody inputs and then looks only in the first 20 characters. For example.... (2 Replies)
Discussion started by: MaestroRage
2 Replies
Login or Register to Ask a Question