Apr 25, 2004 #1 rab54 Programmer Joined Jan 28, 2004 Messages 112 Location GB hi gurus - What is a quick way to do this .... ? cheers# Rab
Apr 25, 2004 #2 PaulTEG Technical User Joined Sep 26, 2002 Messages 4,469 Location IE Code: $/=undef; open FH, "<myfile.txt"; $commaline=<FH>; close FH; @array= split (/\,/, $commaline); $count= $#array; print "$count commas in file"; Upvote 0 Downvote
Code: $/=undef; open FH, "<myfile.txt"; $commaline=<FH>; close FH; @array= split (/\,/, $commaline); $count= $#array; print "$count commas in file";
Apr 25, 2004 #3 aigles Technical User Joined Sep 20, 2001 Messages 464 Location FR Another way : Code: $/=undef; open FH, "<myfile.txt"; $commaline=<FH>; close FH; ($count= ($commaline =~ s/,//g)) += 0; print "$count commas in file"; Jean Pierre. Upvote 0 Downvote
Another way : Code: $/=undef; open FH, "<myfile.txt"; $commaline=<FH>; close FH; ($count= ($commaline =~ s/,//g)) += 0; print "$count commas in file"; Jean Pierre.
Apr 26, 2004 #4 toolkit Programmer Joined Aug 5, 2001 Messages 771 Location GB Or an all-UNIX solution ;-) Code: cat test.txt | tr -c -d ',' | wc -c Cheers, Neil Upvote 0 Downvote
Apr 26, 2004 #5 duncdude Programmer Joined Jul 28, 2003 Messages 1,979 Location GB toolkit - fantastic! can you explain how it works? Code: cat [b]file.txt[/b] pipe output to [b]tr[/b] -cd switches ? ',' - comma pipe output to [b]wc[/b] (word count) -c (character count) Kind Regards Duncan Upvote 0 Downvote
toolkit - fantastic! can you explain how it works? Code: cat [b]file.txt[/b] pipe output to [b]tr[/b] -cd switches ? ',' - comma pipe output to [b]wc[/b] (word count) -c (character count) Kind Regards Duncan
Apr 26, 2004 #6 toolkit Programmer Joined Aug 5, 2001 Messages 771 Location GB -d means delete specified characters -c means complement specified characters Together, this means delete all characters except those specified. So for an example file: Code: qwjdoqdojw,qwoijdoqjwd,oqijdoqjw, qiwkjdoqiwjdo, qiwdjoijdqom, Then cat file | tr -c -d ',' produces: Code: ,,,,, Then wc -c is obvious. Cheers, Neil Upvote 0 Downvote
-d means delete specified characters -c means complement specified characters Together, this means delete all characters except those specified. So for an example file: Code: qwjdoqdojw,qwoijdoqjwd,oqijdoqjw, qiwkjdoqiwjdo, qiwdjoijdqom, Then cat file | tr -c -d ',' produces: Code: ,,,,, Then wc -c is obvious. Cheers, Neil
Apr 26, 2004 #7 duncdude Programmer Joined Jul 28, 2003 Messages 1,979 Location GB cheers Neil... nice one! Kind Regards Duncan Upvote 0 Downvote
Apr 27, 2004 Thread starter #8 rab54 Programmer Joined Jan 28, 2004 Messages 112 Location GB Cheers to you all -I used PaulTeg's solution Rab Upvote 0 Downvote
Apr 27, 2004 #9 duncdude Programmer Joined Jul 28, 2003 Messages 1,979 Location GB another way:- Code: $commaline = <DATA>; print split (",", $commaline) - 1 . " commas in file"; __DATA__ this, is, an example, of a, text, file with, commas Kind Regards Duncan Upvote 0 Downvote
another way:- Code: $commaline = <DATA>; print split (",", $commaline) - 1 . " commas in file"; __DATA__ this, is, an example, of a, text, file with, commas Kind Regards Duncan