pebau
Junior Member
Posts: 81
|
Post by pebau on Jan 23, 2020 21:54:00 GMT 1
Is there an easier way to reduce multiple Blanks to a single Blank Character inside a string ? How could I write this in BaCon, without using EXEC$ and the tr command for squeezing ? Maybe its better to use linux.die.net/man/2/stat for getting the filesize ? FOR file$ IN ls("-l") IF REGEX (file$,".bac$") THEN PRINT "A BaCon File:",file$ ' echo "This is for testing" | tr -s [:space:] " " 'squeeze via bash trim$="echo " & CHR$(34) & file$ & CHR$(34) & " | tr -s [:space:] " & CHR$(39) & " " & CHR$(39) tr$=EXEC$(trim$) PRINT "Filesize: ",TOKEN$(tr$,5," ") ' in ls -l the filesize is on position 5 ENDIF NEXT FUNCTION ls (STRING param) RETURN EXEC$("ls" & " " & param) END FUNCTION
|
|
|
Post by Pjot on Jan 24, 2020 10:14:22 GMT 1
Is there an easier way to reduce multiple Blanks to a single Blank Character inside a string ? Of course, that is very easy: PRINT REPLACE$(a$, "[[:space:]]+", " ", TRUE)
I don't see why? What is it you want to achieve? BR Peter
|
|
pebau
Junior Member
Posts: 81
|
Post by pebau on Jan 24, 2020 13:35:27 GMT 1
Hello, I just wanted to count files from an "ls -l" output with a certain file extension like *.bac or *.log Then I had a working bash onliner with the bc calculator.
echo Bytes:; (ls -l ./*bac | awk '{print $5}' | tr "\012" "+"; echo 0 ) | bc
Then i thought lets do it in BaCon. After that i have seen that the TOKEN$ function can not use a delimiter which could be "one blank or could be more blanks", like it is in ls -l outputs.
VAL(TOKEN$(file$,5," ")) is about the same like awk '{print $5}', but awk knows how to work with different numbers of blanks (delimiters) and justs jumps over them in the input stream data flow until it finds the next real data
So REPLACE$ with Regexes is needed to squeeze the Blanks, thank you Pjot !
[[:blank:]] means space and tab. This makes it similar to: [ \t]. [[:space:]], in addition to space and tab, includes newline, linefeed, formfeed, and vertical tab. This makes it similar to: [ \t\n\r\f\v].
Current Version works good ! (but still slower than bash)
GLOBAL sum_filesize TYPE int
FOR file$ IN ls("-l") IF REGEX (file$,".bac$") THEN ' files which end with .bac sum_filesize=sum_filesize + VAL(TOKEN$(file$,5," ")) ENDIF NEXT PRINT "Sum: ",sum_filesize
FUNCTION ls (STRING param) retval$=EXEC$("ls" & " " & param) retval$=REPLACE$(retval$,"[[:blank:]]+"," ",1) ' keep Newlines, squeeze Blanks RETURN retval$ END FUNCTION
Python version: (about same performance as BaCon, slower than bash)
#!/usr/bin/python3.6 import os my_dir="/home/peter/src_bacon/" complete_size=0
for filename in os.listdir(my_dir): if filename.endswith(".bac"): complete_size+=(os.stat(my_dir+filename).st_size) #get the filesize with os.stat print ("Complete Size:",complete_size,"Bytes")
BR, pebau
|
|
|
Post by Pjot on Jan 24, 2020 15:18:25 GMT 1
How about using WALK$? Count the files with ".bac" extension: PRINT AMOUNT(WALK$("/path/to/my/dir", 1, ".bac$", FALSE, NL$), NL$)
Obtain the total file size: path$ = "/path/to/my/dir/" FOR f$ IN WALK$(path$, 1, ".bac$", FALSE, NL$) STEP NL$ INCR total, FILELEN(path$ & f$) NEXT PRINT total
HTH Peter
|
|
pebau
Junior Member
Posts: 81
|
Post by pebau on Jan 24, 2020 15:48:53 GMT 1
Thank You Pjot, i should have started with WALK$ and not "ls -l" Another awk version: ls -l *.bac | awk '{sum += $5} END {print sum}' BR, Pebau How about using WALK$? Count the files with ".bac" extension: PRINT AMOUNT(WALK$("/path/to/my/dir", 1, ".bac$", FALSE, NL$), NL$)
Obtain the total file size: path$ = "/path/to/my/dir/" FOR f$ IN WALK$(path$, 1, ".bac$", FALSE, NL$) STEP NL$ INCR total, FILELEN(path$ & f$) NEXT PRINT total
HTH Peter
|
|