Sponsored Content
Top Forums Shell Programming and Scripting shell to find the count fields of each line Post 302503265 by dvah on Thursday 10th of March 2011 05:43:18 AM
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.
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
All times are GMT -4. The time now is 11:57 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy