Delimiter Count Line by Line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delimiter Count Line by Line
# 1  
Old 12-19-2010
Delimiter Count Line by Line

Hi All,

I have totally 10 source files. I want to count the delimiter In my source files line by line and need to store the result in another file. I got the output for the total count of delimiter for one file. But I am struggling to get the delimiter count line by line for each my files. Plz give me your valueable Inputs to resolve this Issues.

My delimiter for each file is "|"


Code:
count=0while read linedolinedel=`echo "$line" | awk -F"|" '{print NF-1}'`count=$((count + linedel))done < srcfile
# 2  
Old 12-19-2010
Here is one way of doing it using Perl
Code:
#!/usr/local/bin/perl

use strict;
use warnings;

my $count = 0;

open (FILE, "<file.1") or die $!;

while (my $line = <FILE>) {
    my $linecount = $line =~ tr/|//;
    print $linecount, " > ", $line;
    $count += $linecount;
}
close FILE;

print "Total = $count\n";

Suppose "file1" contains:
Code:
1|2|3|4|5|6
one|two|three

then the output from this script will be
Code:
5 > 1|2|3|4|5|6
2 > one|two|three
Total = 7

# 3  
Old 12-19-2010
Code:
awk '{t+=NF}END{print t-NR}' file*

Code:
awk '{t+=NF-1}END{print t}' file*

# 4  
Old 12-19-2010
Code:
for file in file*; do awk -F'|' 'NF{x+=NF-1;print FILENAME, "LINE #" NR,"delimiter=" NF-1}END{print FILENAME, "Total delimiter:"x}' $file;done > outfile

# 5  
Old 12-21-2010
I would like to thanks for your response. I have tried all your options. But It was throwing erros. My requirement Is If I have 100 records in one file, then my output should be like this. The delimiter should show based on the columns in the file. Plz help me how to do this.
Code:
Row  DelimiterCount
1      5
2     7
.
.
100  7


Last edited by Franklin52; 12-21-2010 at 07:39 AM.. Reason: Please use code tags, thank you
# 6  
Old 12-21-2010
Code:
nawk '{print FNR,NF-1}' infile

# 7  
Old 12-21-2010
Code:
nawk  -F  "|" '{print FNR,NF-1}' infile

I tried this query. It's working fine. Thanks to everyone. Plz one more help

can you tell me how can I fetch 1st row with corresponding delimiter

I tried like this
Code:
head -1 | nawk  -F  "|" '{print FNR,NF-1}' infile

But It shows all the line. I just want to fetch now only the first record with that corresponding delimiter count.

Ex:
Code:
Row DelimiterCount
1    6

How to achieve this.

---------- Post updated at 02:10 AM ---------- Previous update was at 01:55 AM ----------

Thanks to everyone. I resolved the Issues. This is really helpful for all the beginners like me.

Last edited by Franklin52; 12-21-2010 at 07:39 AM.. Reason: Code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Count pattern line by line

I have a file like below with more than 30,000 lines: Someword "mypattern blah blah mypattern blah mypattern blah " Someotherword "mypattern blah blah mypattern blah mypattern blah" Someword "mypattern blah blah blah mypattern blah " Someword "mypattern blah blah mypattern blah ... (3 Replies)
Discussion started by: ctrld
3 Replies

2. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

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

4. Shell Programming and Scripting

Compare file1 header count with file2 line count

What I'm trying to accomplish. I receive a Header and Detail file for daily processing. The detail file comes first which holds data, the header is a receipt of the detail file and has the detail files record count. Before processing the detail file I would like to put a wrapper around another... (4 Replies)
Discussion started by: pone2332
4 Replies

5. Shell Programming and Scripting

Need next line as a space delimiter in awk

Hi,Below is the output for p3fi_dev services 1/app/oracle> . ./oraprofile_p3fi_dev p3fi_dev_01 (P):/devoragridcn_01/app/oracle> srvctl config service -d p3fi_dev p3fi_p3fi_dev.world PREF: p3fi_dev_01 AVAIL: p3fi_dev_02 pplnet_p3fidev PREF: p3fi_dev_01 AVAIL: p3fi_dev_02 nexus_p3fidev PREF:... (3 Replies)
Discussion started by: Vishal_dba
3 Replies

6. Shell Programming and Scripting

search for a string without fixed delimiter in the line

Hi Need your help to assign the string to a variable from a line which has no fixed delimter in unix. for example , my file contains Name="report"" File Name one="test1" File Name two="test2" now how do I read report , test1 and test2 ? var1=report var2=test1 var3=test2 ... (1 Reply)
Discussion started by: rashmisb
1 Replies

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

8. Shell Programming and Scripting

Append line count to each line

Hello forum, I need to append the total line count to the end of each line in a file. The file where this line count needs to be appended is generated by this script: The script does a word frequency count by the first column of a file. if I add wc -l at the end then the line count... (4 Replies)
Discussion started by: jaysean
4 Replies

9. Shell Programming and Scripting

Get the line count from 2nd line of the file ?

Hi, I want to get the line count of the file from the 2nd line of the file ? The first line is header so want to skip that. Thanks. (8 Replies)
Discussion started by: smc3
8 Replies

10. Shell Programming and Scripting

How to find last delimiter in line?

I am working in a ksh script. I am reading a login, password, and database name from a pre-existing config file. Login and password are simple, I take the value after the first "=" sign, but the dbname has multiple equal signs in it. I have it working by temporarily reading the 23rd field, but... (4 Replies)
Discussion started by: prismtx
4 Replies
Login or Register to Ask a Question