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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to check the number of columns in a line??
# 15  
Old 10-18-2013
can you explain me the below command

Code:
awk -F\| 'NF != 6 {exit 1}' file1.txt || test=1

|| is a OR condition so

Code:
0 0 = 0
0 1 = 1
1 0 = 1
1 1 = 1

1 = true
0 = false

so if the file1.txt is not having 6 columns in each of the row, awk -F\| 'NF != 6 {exit 1}' file1.txt will exit with 1 (condition is true). if the first condition evaluates to true, then it wont evaluate the 2nd condition i.e test=1. but it is evaluating. and setting test=1.
# 16  
Old 10-18-2013
I guess not sure you are getting old values, did you unset variable test for each loop iteration ?
if not do like this
unset test
# 17  
Old 10-18-2013
i have kept the above awk line inside a shell script. and before executing the script i am setting test=0. when the shell script completes. test doesnot exist.

Code:
file=$1
test=0
echo "test: $test"
awk -F\| 'NF != 6 {exit 1}' $file || test=1
if [ $test -eq 1 ]; then
        echo "test = 1"
elif [ $test -eq 0 ]; then
        echo "file is OK"
fi

# 18  
Old 10-18-2013
I can't reproduce that on cygwin. Could you post an example input file?
# 19  
Old 10-18-2013
the command is working fine.
Code:
awk -F\| 'NF != 6 {exit 1}' $file || test=1

just want to know how the condition gets evaluated?
refer my second last (#15) post.

any ways my input file is like
correct input file:
Code:
111|abc|xxx|yyy|zzz|123
111|abc|xxx|yyy|zzz|123
111|abc|xxx|yyy|zzz|123
111|abc|xxx|yyy|zzz|123
111|abc|xxx|yyy|zzz|123
111|abc|xxx|yyy|zzz|123

for the above file test will be = 0

false input file:
Code:
111|abc|xxx|yyy|zzz|123
111|abc|xxx|yyy|zzz|123
111|abc|xxx|yyy|zzz|123
111|abc|xxx|yyy|zzz|123
111|abc|xxx|yyy|zzz
111|abc|xxx|yyy|zzz|123

for the above input file test will be equal to 1
# 20  
Old 10-18-2013
That isn't what you said before?
Quote:
if the first condition evaluates to true, then it wont evaluate the 2nd condition i.e test=1 .
is correct, but
Quote:
but it is evaluating. and setting test=1 .
Is it setting test when it shouldn't, or not?
# 21  
Old 10-18-2013
OR works like this:
Code:
 command1 || command2 
  command2 is executed if, and only if, command1 returns a non-zero exit status.

I think it was mentioned earlier by others.
Code:
test=0;awk -F\| 'NF != 6 {exit 1}' INPUT || test=1 && echo $test

Code:
can INPUT
111|abc|xxx|yyy|zzz|123
111|abc|xxx|yyy|zzz|123
111|abc|xxx|yyy|zzz|123
111|abc|xxx|yyy|zzz|123
111|abc|xxx|yyy|zzz|123
111|abc|xxx|yyy|zzz|123

Code:
test=0;awk -F\| 'NF != 6 {exit 1}' INPUT || test=1 && echo $test

Code:
cat INPUT
111|abc|xxx|yyy|zzz|123
111|abc|xxx|yyy|zzz|123
111|abc|xxx|yyy|zzz|123
111|abc|xxx|yyy|zzz|123
111|abc|xxx|yyy|zzz
111|abc|xxx|yyy|zzz|123

Line 5 has 5 fields and awk exits with exit status 1. Hence variable test gets value 1.
( and it prints in my example )
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

Script to check for a specific number of columns in a file

Hi All I have a file which has five columns in each rows. cat file.txt a|b|c|d|e 1|2|3|4|5 a1|a2|a3|a4|a5 . . . I need to make sure that there are no less than five or more than five columns (in all the rows) by mistake. I tried this : cat file.txt | awk 'BEGIN{FS="|"};{print... (3 Replies)
Discussion started by: chatwithsaurav
3 Replies

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

4. Shell Programming and Scripting

How do i find the first number in each line and insert dummy string into the missing columns?

Hi, I have one input file with the following content: MY_inpfile.txt Aname1 Cname1 Cname2 1808 5 Aname2 Cname1 1802 47 Bname1 ? 1819 22 Bname2 Cname1 1784 11 Bname3 1817 9 Zname1 Cname1 1805 59 Zname2 Cname1 Cname2 Cname3 1797 27 Every line in my input file have a 4 digit... (5 Replies)
Discussion started by: Szaffy
5 Replies

5. Shell Programming and Scripting

splitting a huge line of file into multiple lines with fixed number of columns

Hi, I have a huge file with a single line. But I want to break that line into lines of with each line having five columns. My file is like this: code: "hi","there","how","are","you?","It","was","great","working","with","you.","hope","to","work","you." I want it like this: code:... (1 Reply)
Discussion started by: rajsharma
1 Replies

6. UNIX for Dummies Questions & Answers

How to read contents of a file from a given line number upto line number again specified by user

Hello Everyone. I am trying to display contains of a file from a specific line to a specific line(let say, from line number 3 to line number 5). For this I got the shell script as shown below: if ; then if ; then tail +$1 $3 | head -n $2 else ... (5 Replies)
Discussion started by: grc
5 Replies

7. Shell Programming and Scripting

perform a check based on number of @ in a log line

Hello, I am intending to perform a check based on number of "@" , present in a line in a log file . The idea is basically to perform a check on cc or bcc sender, based on an email log, which shows all the for email address. Say if the number of @ is more than 30, I will consider it as a mass... (12 Replies)
Discussion started by: fed.linuxgossip
12 Replies

8. Shell Programming and Scripting

how to get the data from line number 1 to line number 100 of a file

Hi Everybody, I am trying to write a script that will get some perticuler data from a file and redirect to a file. My Question is, I have a Very huge file,In that file I have my required data is started from 25th line and it will ends in 100th line. I know the line numbers, I need to get all... (9 Replies)
Discussion started by: Anji
9 Replies

9. Shell Programming and Scripting

Adding a columnfrom a specifit line number to a specific line number

Hi, I have a huge file & I want to add a specific text in column. But I want to add this text from a specific line number to a specific line number & another text in to another range of line numbers. To be more specific: lets say my file has 1000 lines & 4 Columns. I want to add text "Hello"... (2 Replies)
Discussion started by: Ezy
2 Replies

10. Shell Programming and Scripting

Appending line number to each line and getting total number of lines

Hello, I need help in appending the line number of each line to the file and also to get the total number of lines. Can somebody please help me. I have a file say: abc def ccc ddd ffff The output should be: Instance1=abc Instance2=def Instance3=ccc Instance4=ddd Instance5=ffff ... (2 Replies)
Discussion started by: chiru_h
2 Replies
Login or Register to Ask a Question