Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to count how many ; in a file 1

Status
Not open for further replies.

jpor

Technical User
Nov 29, 2000
212
GB
Hi all,

This may be a stupid question. But I need to count how many ; are in each line in a file.

I have tried the following:

cat <filename> |grep ";" |wc -c

But all I get is the whole count of characters.

Can anyone help?



( &quot;To become Wise, first you must ask Questions&quot;)
 
how many ; are in each line in a file
awk -F';' '{print NR,NF-1}' filename

If you want the total number:
awk -F';' '{t+=(NF-1)}END{print t}' filename

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
grep will return the whole line containing any ;'s, hence your problem. There will be a way to do this using sed or awk, but I'm no expert there, I'm afraid. Someone who is will be along soon, I'm sure.
 
If the file may have empty lines:
awk -F';' 'length($0){print NR,NF-1}' filename
or
awk -F';' 'length($0){t+=(NF-1)}END{print t}' filename

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
That was quick. Thanks people.

( &quot;To become Wise, first you must ask Questions&quot;)
 
Okay. Just used PHV's example. I now need to run the command:

awk -F';' '{print NR,NF-1}' filename

And capture this into a variable and if any line counts different. I.e less or greater than 36 ";" then I need to show this.

( &quot;To become Wise, first you must ask Questions&quot;)
 
Something like this ?
awk -F';' 'NF!=37' filename

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Close. But I still need a count of each line as each line will be uploaded into a Informix Database table and each line must have 36 ";". I need to identify any line that does not have 36 ; so it can be fixed. If that makes sense?


( &quot;To become Wise, first you must ask Questions&quot;)
 
This ?
awk -F';' 'NF!=37{print NR,$0}' filename

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Sorry PHV. This may shed some light what I'm trying to achieve:

##-- Check if amount of ; is correct, if not e-mail --##

test=`awk -F';' '{print NR,NF-1}' /dump_area/p4uin/p4uin.csv >> /dump_area/p4uchk`
test=" "

while read line
do
test =`awk '{print$2}'`

done < /tmp/p4uchk

if [ "$test" != "36" ]
then
echo $test >> /tmp/p4uerr.err
sleep 3
/usr/local/bin/mpack -s "P4u File cannot be loaded for `date`" /tmp/p4uerr.err user@company.com
exit


else

echo "File is Okay to load"

fi

Basically I am reading back each line (the line with toatal of ";") then checking each one to see if it = 36 or NOT. If NOT = 36 then I need to know which one.

Thanks.



( &quot;To become Wise, first you must ask Questions&quot;)
 
Something like this ?
awk -F';' 'NF!=37{print NR,$0}' /dump_area/p4uin/p4uin.csv > /tmp/p4uerr.err
[ -s /tmp/p4uerr.err ] && {
/usr/local/bin/mpack -s "P4u File cannot be loaded for `date`" /tmp/p4uerr.err user@company.com
exit
}
echo "File is Okay to load"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
PHV. You've done it again. That works. Tried with an extra ; and it e-mails if it has the correct amount it goes through.

Thanks again.


( &quot;To become Wise, first you must ask Questions&quot;)
 
Hi again,

From the code PHV has kindly donated:

##-- Check if amount of ; is correct, if not e-mail --##

awk -F';' 'NF!=37{print NR,$0}' /dump_area/p4uin/p4uin.csv > /tmp/p4uerr.err
[ -s /tmp/p4uerr.err ] && {
/usr/local/bin/mpack -s "P4u File cannot be loaded due to field length for `date` " /tmp/p4uerr.err ccsrvjzp@mprc.co.uk ccsrvadr@m
prc.co.uk
exit
}

How can I Strip the lines away and move them to another file? Am I right in saying I need to use SED fro this?

Thanks for your patience.


( &quot;To become Wise, first you must ask Questions&quot;)
 
You may consider something like this:
cp /dump_area/p4uin/p4uin.csv /tmp
awk -F';' '
NF!=37{print NR,$0 > "/tmp/p4uerr.err";next}
{print}
' /tmp/p4uin.csv > /dump_area/p4uin/p4uin.csv

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks yet again PHV. Your genius.


( &quot;To become Wise, first you must ask Questions&quot;)
 
Code:
cat <filename> |grep ";" |wc -c
This is an abuse of cat, and would be even if it did work. The line should read:
Code:
grep ";" <filename> | wc -l
Granted this will under count, if you have a line that has more than one semi on it. My pedant was about the misuse of cat.
 
grep -c \; <file>

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
-c Print only a count of the lines that contain the
pattern.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks for the replies people. I am learning new things everyday.


( &quot;To become Wise, first you must ask Questions&quot;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top