Counting the number of pipes in line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Counting the number of pipes in line
# 1  
Old 10-17-2008
Question Counting the number of pipes in line

Hi,

I'm using the ksh shell.

The scenario:

I have a couple of directories

/home/fd
/home/fd/prsd
home/fd/stg

now i have number of files in each of these directories.

some of the files are zipped using gzip so their extension is .gz

the content of the files is as follows

D|abc|1324|ba92|adfds||324 -1 1 | | bcd |||||
D|as|cdsa|235|as|gf=12|sdf34|$||||sas|a5#|


Basically I mean each record is in a newline and is delimeted with the pipe.

All the files have the same structure(remember some are zipped)

and there can be anywhere between 40K and 200K records in a file

So if you count the pipes in the record(line) we have 13 pipes in each line(record). All the files will have the 13 pipes.

But i have one file which has 14 pipes instead of 13(means each record in that file will have 14 pipes)

and I need to find this fiel with 14 pipes.........

It may be in either of these directories and may or maynot be zipped...


Please help me out.....


Many Thanks
# 2  
Old 10-17-2008
I hope you don't mind a quick answer that can certainly be improved. I'm afraid I've been a bit unwell, so please pardon the rather brutish nature of the script, and the tcsh. Hopefully, since this can be placed in its own script file, the particular shell won't matter. First, note this relationship:

(# of | in line) = (number of fields in line) -1

So, for example, there are 3 fields and 2 pipes in:

a|b|c

So, simply expect values 1 higher than the terms in which you expressed your problem; a metric boatload of files with 14 fields, and 1 with 15.


Code:
#!/bin/tcsh

foreach file ( `ls | grep -v fieldcounter` )
set ext = `echo $file | awk -F. '{print $NF}'`
echo -n $file
if ( $ext == "gz" ) then
     set fields = `zcat $file | head -n 1 | awk -F\| '{print NF}'`
else
     set fields = `cat $file | head -n 1 | awk -F\| '{print NF}'`
endif
echo " has fieldcount:  $fields"
end

I named this script "fieldcounter". You can call it whatever you like, but make sure to change "fieldcounter" in the foreach line accordingly. There are a couple of assumptions. First, I assume that everything in the directory is a file (as opposed to a directory, say); second, that you want to examine everything in the directory; third, that the first line of each file is just data, and isn't a special, differently-formatted header line. Each of these assumptions is easy enough to change and account for, but they are there nonetheless.

So, give it a shot... if there are, in fact, errors in my assumptions, or some other factor, let us know. I'm certain we can piece something together that'll work.

Last of all, this is written as an infrequently-applied solution. If you're going to do this often, something faster would be beneficial.
# 3  
Old 10-18-2008
One more option

sed 's/[^|]//g' <filename>|wc -c

you need to add logic to loop through multiple files, and if the above command gives 14, you can print the file name.
# 4  
Old 10-19-2008
this will give the all the file name which are having 14 pipes in that(from current dir only)
Code:
for i in *
do
while read line
do
echo "$line"|awk -F\| -v v=$i 'END{if(NF==14){print v}}'
break
done < "$i"
done

# 5  
Old 10-19-2008
14 pipes would be 15 fields (NF=15). This is simpler:

Code:
awk -F'|' 'NF==15 { print "Found:", FILENAME; nextfile }' *

MrC
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Limit on number of pipes after long-running command?

I'm trying to create a minimal, crude keylogger for X using only a shell script. I was quickly stumped: Why do these two commands entered in a terminal emulator produce output when I type... $ xinput test 6 | grep press $ xinput test 6 | awk '{print $3}' ...but this command produces no... (13 Replies)
Discussion started by: DevuanFan
13 Replies

2. Shell Programming and Scripting

Help: Counting values less than a number

So I have several files (35000, to be exact) in the format rmsd_protein_*.dat each with 2 columns and 35000 rows. I would like to count how many values in the second column are less than 3 for each file, and output it into a new file so that it ultimately appears as: 1 14057 2 ... (12 Replies)
Discussion started by: Alexandryne
12 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

Counting the number of characters

Hi all, Can someone help me in getting the following o/p I/p:... (7 Replies)
Discussion started by: Sri3001
7 Replies

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

6. Shell Programming and Scripting

counting number of sentence

Hi all I want to count total numbers of sentences separated by fullstop (.) in different files under a directory at one go. Any help is appreciated. (3 Replies)
Discussion started by: my_Perl
3 Replies

7. Shell Programming and Scripting

counting the number of occurences

say i've got a text file with >10million sequences: ssss ssss tttttt uuuuuu uuuuuu uuuuuu ... I'd like to convert the file so that the output will report the number of occurence right by each sequence: 2 ssss 2 ssss 1 tttttt 3 uuuuuu 3 uuuuuu 3 uuuuuu .... (3 Replies)
Discussion started by: johjoh
3 Replies

8. Shell Programming and Scripting

counting the number of lines - again

Hi all, I use bash shell and I have a problem with wc. I would like to determine the number of lines in a file so I do wc -l filename but I don't want to get the filename again I just would like to have the number of lines and use it in a variable. Can anybody help? Thank you, (7 Replies)
Discussion started by: f_o_555
7 Replies

9. UNIX for Dummies Questions & Answers

Counting number of occurences

Hi All, I have to count the number of occurences of the character " ; " in a given line. I had used the following awk command to achieve the same echo $KOP.dat|awk '{split($1,my,";"); for(i in my)c++ }END{print c-1}' My file KOP.dat had the following data ... (1 Reply)
Discussion started by: kingofprussia
1 Replies

10. Linux

counting the number of lines

Hello, I have afile which begins with a few urls on multiple lines and then there is listing of some information on separate lines. The listing begins with the word Name on a given line followed by teh actual list. I want to count the number of lines in this file after the line having... (6 Replies)
Discussion started by: nayeemmz
6 Replies
Login or Register to Ask a Question