shell to find the count fields of each line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell to find the count fields of each line
# 1  
Old 03-09-2011
Bug shell to find the count fields of each line

hi,

i've many unload files with delimiter '|'. I'm trying to load them to the specific tables from those unl's. The problem here is, some unl's are corrupted. To be exact, some files doesnt seem to have the exact number of fields as in the table. So im trying to identify the corrupted files, i cant do it manually as im dealing with hundreds of such files.

I've a file with the table name(same as the file name) with the table name and the number of columns in it.

For Ex-

table1.unl

xx|xx|xxx|xxx|xx|xxx| - no of fields 6 (nt a problem)
xx|xxxx| - no of fields is 2 (report this)
xxx|xx|xx|xxxxxx| - no of fields is 4 (report this)


ref_file.txt
table1|6|
table2|10|
...

so i need a shell to check that ref_file.txt, get the number of fields in that table (which is 6 for table1) and compare it with the unl file. It has to report error when any of the row has fewer fields that that(<6 for here).

The corrupted line can be anywhere in the file, it doesnt require to be in the start. So it has to check the whole file.

Please help!
Thanks!Smilie
# 2  
Old 03-09-2011
Code:
$
$
$ # display the contents of ref_file.txt
$ cat ref_file.txt
table1|6|
table2|10|
$
$
$ # display the contents of table1.unl
$ cat table1.unl
xx|xx|xxx|xxx|xx|xxx|
xx|xxxx|
xxx|xx|xx|xxxxxx|
$
$
$ # Now run the Perl script to read ref_file.txt and then process all *.unl files
$
$ perl -lne 'BEGIN {print "The line number and the bad record will be printed for each file.\n"}
             if ($ARGV eq "ref_file.txt") { /^(.*?)\|(\d+)\|$/; $x{$1} = $2 }
             elsif ($ARGV =~ s/\.unl// and defined $x{$ARGV}) {
               print "File: $ARGV.unl";
               open (F, "$ARGV.unl");
               while (<F>) {
                 chomp($line = $_);
                 $c = s/\|//g;
                 print "$.:$line" if $c != $x{$ARGV};
               }
               close (F);
             }
            ' ref_file.txt *.unl
The line number and the bad record will be printed for each file.
 
File: table1.unl
2:xx|xxxx|
3:xxx|xx|xx|xxxxxx|
$
$
$

HTH,
tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 3  
Old 03-09-2011
Code:
sed 's/|/ /g' ref_file.txt | while read a b
do
     nawk -v X="$b" '(NF!=X){print $NR":"$0}'   "$a".unl
done

This User Gave Thanks to ctsgnb For This Post:
# 4  
Old 03-09-2011
Code:
while read REF_FILE_REC
      do
             TABLE_NAME=`echo $REF_FILE_REC | awk -F "|" '{print $1}'`
             REC_LEN=`echo $REF_FILE_REC | awk -F "|" '{print $2}'`
             while read TABLE_REC
                      do
                              LAST_FIELD=`echo $TABLE_REC | awk -v X="$REC_LEN" -F "|" '{print $X}'`
                              if [ ${#LAST_FIELD} -gt 0 ]
                              then
                                echo "$TABLE_REC" >>$TABLE_NAME.ok
                       else
                               echo $TABLE_REC >>$TABLE_NAME.nok
                        fi
                 done <$TABLE_NAME.unl
   done<ref_file.txt


Last edited by Franklin52; 03-10-2011 at 06:35 AM.. Reason: Please use code tags, thank you
# 5  
Old 03-09-2011
Code:
cat ref_file.txt
table1|6|
table2|10|

cat table1.unl
xx|xx|xxx|xxx|xx|xxx|
xx|xxxx|
xxx|xx|xx|xxxxxx|
 
cat table2.unl
xx|xx|xxx|xxx|xx|xxx|
xx|xxxx|
xxx|xx|xx|xxxxxx|x|x|x|x|x|x|
 
awk -v FS="|" 'NR==FNR {a[$1".unl"]=$2}
NR>FNR{for (file in a) {if(FILENAME==file && NF-1<a[file]) {print >FILENAME"-output"}}}' ref_file.txt *.unl
 
cat table1.unl-output
xx|xxxx|
xxx|xx|xx|xxxxxx|
 
cat table2.unl-output
xx|xx|xxx|xxx|xx|xxx|
xx|xxxx|

This User Gave Thanks to yinyuemi For This Post:
# 6  
Old 03-10-2011
Quote:
Originally Posted by ctsgnb
Code:
sed 's/|/ /g' ref_file.txt | while read a b
do
     nawk -v X="$b" '(NF!=X){print $NR":"$0}'   "$a".unl
done

Thanks a lot.

This works fine. Last query, how can i modify this to print just the file name (only once) instead of printing the corrupted lines.

---------- Post updated at 04:13 PM ---------- Previous update was at 03:39 PM ----------

Quote:
Originally Posted by durden_tyler
Code:
$
$
$ # display the contents of ref_file.txt
$ cat ref_file.txt
table1|6|
table2|10|
$
$
$ # display the contents of table1.unl
$ cat table1.unl
xx|xx|xxx|xxx|xx|xxx|
xx|xxxx|
xxx|xx|xx|xxxxxx|
$
$
$ # Now run the Perl script to read ref_file.txt and then process all *.unl files
$
$ perl -lne 'BEGIN {print "The line number and the bad record will be printed for each file.\n"}
            if ($ARGV eq "ref_file.txt") { /^(.*?)\|(\d+)\|$/; $x{$1} = $2 }
            elsif ($ARGV =~ s/\.unl// and defined $x{$ARGV}) {
              print "File: $ARGV.unl";
              open (F, "$ARGV.unl");
              while (<F>) {
                chomp($line = $_);
                $c = s/\|//g;
                print "$.:$line" if $c != $x{$ARGV};
              }
              close (F);
            }
           ' ref_file.txt *.unl
The line number and the bad record will be printed for each file.
 
File: table1.unl
2:xx|xxxx|
3:xxx|xx|xx|xxxxxx|
$
$
$

HTH,
tyler_durden
Thanks for this.

I need to modify this to just make the corrupted file reported, instead of the content. Please help.
# 7  
Old 03-16-2011
Quote:
Originally Posted by dvah
...Last query, how can i modify this to print just the file name (only once) instead of printing the corrupted lines.
...
You could do something like this -

Code:
$
$ # show the content of ref_file.txt, table1.unl and table2.unl
$
$ cat ref_file.txt
table1|6|
table2|10|
$
$ cat table1.unl
xx|xx|xxx|xxx|xx|xxx|
xx|xxxx|
xxx|xx|xx|xxxxxx|
$
$ cat table2.unl
xxx|xxx|xxx|xxx|xxx|xxx|xxx|xxx|xxx|xxx|
xx|xx|xx|xx|xx|xx|x|xx|x|xxx|
xxx|xxx|xxx|xxx|xxx|xxx|xxx|xxx|xxx|xxx|
$
$
$ # Run the Perl script
$
$ perl -ne 'if ($ARGV eq "ref_file.txt") { /^(.*?)\|(\d+)\|$/; $x{$1} = $2 }
            elsif ($ARGV =~ s/\.unl// and defined $x{$ARGV}) {
              $status = "good";
              open (F, "$ARGV.unl");
              while (<F>) {
                chomp($line = $_);
                $c = s/\|//g;
                do {$status = "bad"; last} if $c != $x{$ARGV};
              }
              close (F);
              print "File: $ARGV.unl is $status\n";
            }
           ' ref_file.txt *.unl
File: table1.unl is bad
File: table2.unl is good
$
$

tyler_durden
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 find the count of IP addresses that belong to different subnets and display the count?

Hi, I have a file with a list of bunch of IP addresses from different VLAN's . I am trying to find the list the number of each vlan occurence in the output Here is how my file looks like 1.1.1.1 1.1.1.2 1.1.1.3 1.1.2.1 1.1.2.2 1.1.3.1 1.1.3.2 1.1.3.3 1.1.3.4 So what I am trying... (2 Replies)
Discussion started by: new2prog
2 Replies

2. Shell Programming and Scripting

Count the pipes "|" in line and delete line if count greter then number.

Hello, I have been working on Awk/sed one liner which counts the number of occurrences of '|' in pipe separated lines of file and delete the line from files if count exceeds "17". i.e need to get records having exact 17 pipe separated fields(no more or less) currently i have below : awk... (1 Reply)
Discussion started by: ketanraut
1 Replies

3. Shell Programming and Scripting

Count blank fields in every line

Hello All, I am trying a one liner for finding the number of null columns in every line of my flat file. The format of my flat file is like this a|b|c|d||||e|f|g| a|b|c|d||||e|f|g| I want to count the number of fields delimited by "|" which are blank. In above case the count should be... (6 Replies)
Discussion started by: nnani
6 Replies

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

5. Shell Programming and Scripting

Shell script to count number of ~ from each line and compare with next line

Hi, I have created one shell script in which it will count number of "~" tilda charactors from each line of the file.But the problem is that i need to count each line count individually, that means. if line one contains 14 "~"s and line two contains 15 "~"s then it should give an error msg.each... (3 Replies)
Discussion started by: Ganesh Khandare
3 Replies

6. Shell Programming and Scripting

use shellscript to find the count of a line in a set of lines

I have a file a.xml some portion of the file is given below.But the file format is same. CTYPE available_templates SYSTEM './available_templates.dtd'> <available_templates> <template_file name="Approve External" path="core/approve/bin" <command_list> <command... (1 Reply)
Discussion started by: millan
1 Replies

7. Shell Programming and Scripting

Need help in splitting a line into fields in shell scripting

I have a line of more than 3000 bytes which will contain & as fields separator..I am using following awk command ..Its working but its not accepting the line more than 3000 bytes...Anyother alternate solution even in othe shell command also fine... awk -F '&' '{for( i=1; i<=NF; i++ ) print $i}'... (2 Replies)
Discussion started by: punithavel
2 Replies

8. UNIX for Dummies Questions & Answers

find out the line count over FTP connection

I need help to find out the number of lines in a file which exists in remote machine. I the remote machine, auto login is enabled. So I do not need to worry about the username or password. But the problem is, I do not know how I can find out the line count once I get the FTP prompt. (1 Reply)
Discussion started by: Rita_questions
1 Replies

9. Shell Programming and Scripting

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 :mad: (7 Replies)
Discussion started by: babu@shell
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