• We did not send an email asking for donations - please read this post.

bash script, find amount of strings in files

krdgroup23

New Member
Joined
Nov 25, 2022
Messages
2
Reaction score
0
Credits
20
Hello everyone! I am writing script which must count amount of strings in files .z format. Name of folder can changes in dependence of current date, if it's 1-st date of the month script count amount in folder which is called 2210 (previous month ) if today is not 1-st day of month then folder is called 2211 (current month). For ex. I have catalogs with files:
Code:
WORK6\AXE\CNA5\LBN\2211\1.z # 16 strings in file
WORK6\AXE\TELLIN\2211\2.z # 16 strings in file
WORK6\AXE\TELLIN\2211\3.z # 16 strings in file

here is my script
Code:
#!/bin/bash
#assign a value to the variable
timestamp=$(date +%d)
#if today is 1-st date of month then previous month else current
if [[ $timestamp == 01 ]]; then
    folder=$(date -d " - $(date +%d) days" +%y%m)
else
    folder=$(date +%y%m)
fi
#find path to files
path=$(find ./ -type d -name "$folder")
#find files and count amount of strings
for file in $path/*
do
    $(zcat $file | wc –l)
done

it shows
Code:
wc: –l: No such file or directory
gzip: ./AXE/CNA5/LBN/2211 is a directory -- ignored
wc: –l: No such file or directory
wc: –l: No such file or directory

How should i change my script that the result will
Code:
16 WORK6\AXE\CNA5\LBN #  16 strings in file 1.z
32  WORK6\AXE\TELLIN # 32 strings in files  2.z+3.z
 


OP
K

krdgroup23

New Member
Joined
Nov 25, 2022
Messages
2
Reaction score
0
Credits
20
$path != $PATH
As i understand in these fragment path=$(find ./ -type d -name "$folder") i send several paths and my cycle try to process it all What's why it doesn't work? And how can i fix it?
 
Last edited:

osprey

Active Member
Joined
Apr 15, 2022
Messages
237
Reaction score
136
Credits
2,170
Post #2 was a cryptic reference to the naming of variables which I'll leave for the reader. If the post is homework, I shall offer only a hint which may or may not be useful. I interpret the expression "amount of strings" actually as "number of lines" since it's the output of the "wc -l" command that appears to be the output sought. The following will output the number of lines in a listing of files:
Code:
for file in $(ls)
  do
     wc -l $file
  done
 
Last edited:

wizardfromoz

Administrator
Staff member
Gold Supporter
Joined
Apr 30, 2017
Messages
7,779
Reaction score
6,605
Credits
28,603
We'll wait on an answer on whether this is homework and subject to that, it may get moved to Command Line, where scripting answers are provided.

Avagudweegend

Wiz
 

JasKinasis

Well-Known Member
Joined
Apr 25, 2017
Messages
1,562
Reaction score
2,224
Credits
11,517
wc counts the number of words, characters and lines in a file. Not the number of strings.
Your question states to find the number of strings in a file.

If English is a second language to you, then I’ll concede that perhaps ‘strings’ might be a bad translation.

However, if English is your first language, ‘strings’ would imply that you’re looking for string variables, NOT the number of words, characters, or lines in a file.

So for example:
Bash:
echo ‘This is a literal string’
echo "this is another string"
Everything in between single, or double quotes in the above is a string.

So perhaps you need to be using the strings command to find all of the strings in a file and then pipe the results to wc -l to determine the total number of strings found?!

I haven’t got time to write an example snippet atm. I’m on my phone and it’s early in the morning. My brain’s not fully in gear yet. But give that a go and let us know how you get on.

The strings command will search any binary, or executable program for strings and will list each string it finds.
And a .zip file is a binary format.

It might also be worth looking at the man page for string, because it may, or may not have an option that will cause it to count the number of strings found, instead of displaying all of the strings found. Which would mean that there would be no need to pipe the results to wc.

I haven’t used the strings for a while, so I can’t remember anything about its various command-line options/switches.

I hope this helps!
 
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Linux.org Hosting Donations
Consider making a donation

Members online


Top