Computing.Net > Forums > Programming > unix script to count delimiter pipe

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

unix script to count delimiter pipe

Reply to Message Icon

Name: priya99
Date: June 24, 2009 at 13:43:57 Pacific
OS: Windows XP
Subcategory: General
Comment:

I want get help to write a unix script to count "|" pipe in the flat file.

The first row is header and the record count starts from 2nd row. I want to count the "|" in 2nd row only.

Thanks for your help



Sponsored Link
Ads by Google

Response Number 1
Name: ghostdog
Date: June 24, 2009 at 17:54:15 Pacific
Reply:

awk -F"|" 'NR==2{print NF-1}'  file

next time, try to do it on your own first before posting. that will indicate you put in effort, even if its for homework.

GNU win32 packages | Gawk


0

Response Number 2
Name: priya99
Date: June 25, 2009 at 11:10:31 Pacific
Reply:

Thanks for the response. I already tried in the following way, but somehow it goes into endless loop.

$ cat 1.txt
abc|pqr|ttt|fff|rrr

ctr=1
while true
do
var=`cat 1.txt | head -1 | cut -d'|' -f$ctr | wc -c`
if [ $var -eq 1 ]
break
fi
ctr=`expr $ctr + 1`
done
#number of field delimeters
echo $ctr


0

Response Number 3
Name: ghostdog
Date: June 25, 2009 at 16:28:06 Pacific
Reply:

that's a messy way to do what you want. learn how to use awk, as i have it to you.

GNU win32 packages | Gawk


0

Response Number 4
Name: nails
Date: June 25, 2009 at 16:32:06 Pacific
Reply:

If you played with this on the command line, you'd see that variable var never equals 1 so the while loop cycles forever:

cat 1.txt | head -1 | cut -d'|' -f 4 | wc -c

I do not understand your logic and what you are trying to do.

If you aren't going to use ghostdog's excellent awk one-liner, then consider cycling thru the file a line at a time and counting the pipe symbols:

#!/bin/ksh

pipecnt=0
while read line
do
   count=0
   linelen=`echo "$line"|wc -c`
   if [ $linelen -gt 0 ]
   then # don't do anything if line is zero len
      until [ $count -ge $linelen ]
      do
         count=`expr $count + 1`
         char=`echo "$line"|cut -b"$count"`
         if [ "$char" = "|" ]
         then # check if char is pipe
            pipecnt=`expr $pipecnt + 1`
         fi
      done
   fi
done < 1.txt
echo $pipecnt
# end script

I'm using ksh so you could use the ksh arithmetic method:
((pipecnt+=1)) # add 1 to pipecnt


0

Response Number 5
Name: ghostdog
Date: June 25, 2009 at 17:50:57 Pacific
Reply:

well, if OP wants to do it with the shell, something like this may suffice

while read line
do
  x=${line//[!|]/}
  echo ${#x}
done <file

GNU win32 packages | Gawk


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon

auto date stamping a file... create *.txt



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: unix script to count delimiter pipe

Unix script to sort .dat files www.computing.net/answers/programming/unix-script-to-sort-dat-files/18149.html

UNIX script for Extracting data www.computing.net/answers/programming/unix-script-for-extracting-data-/15009.html

help- -trying to get script to work www.computing.net/answers/programming/help-trying-to-get-script-to-work/1180.html