ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
i would need the line count of the files like this
Code:
if file_name*.txt
then
wc -l file_name*.txt - 1
if type_name*.txt
then
wc -l type_name*.txt -2
so my output file format should have
Code:
outputfile.txt
file_name1.txt - 10,type_name1.txt - 20
file_name2.txt - 20,type_name2.txt - 30
considering the line count of the files like below
Code:
file_name1.txt - 11
file_name2.txt - 12
type_name1.txt-22
type_name2.txt - 32
i tried that but cant able to format according to my output which i have mentioned
and also not able to check in if conditions
can u help me with the code please
my if conditions are it should apply line count for certain files for eg for names containing file_name*.txt(i.e file_name1.txt,file_name2.txt,....so on) in inputfile.txt, i need to apply line count with subtracting 1 and another if condition names containing like
type_name*.txt(i.e type_name1.txt,type_name2.txt,...so on) i need the to apply line count with subtracting 2
let me make it simple now with an example
i will read the files from my inputfile.txt which contains like below
file_name1.txt,type_name1.txt
file_name2.txt,type_name2.txt
.
.
.
i need to take line count of file_name1.txt,file_name1.txt,type_name1.txt,file_name2.txt,type_name2.txt these files are already present in my directory,
here i need to apply a logic either in if condition or case condition like if it contains like file_name*.txt then take line count and subtract -1 from the line count(wc -l file_name*.txt -1) and if contains line type_name*.txt then take line count and subract 2 from the line count(wc -l type_name*.txt -2)
this will be redirected to my output file in the below format
actual line counts of the file:
considering the line count of the files like below
Code:
file_name1.txt - 11
file_name2.txt - 12
type_name1.txt-22
type_name2.txt - 32
Please show how you are reading / getting the data out of inputfile.txt? (ie. before the if can be used we need to have the data from inputfile.txt stored in variable(s))
Well schneidz has presented a solution but I actually have two more questions:
1. Is the format of the data in inputfile.txt known? (ie. is it always file_name and then type_name)
2. Your example shows mock data of 10 lines in file_name1.txt, my question is, how do you then finish with output total of 11 if you are "subtracting" 1, should it not be 9?
the format in the input file remains same only it may extend with row level
for eg:
file_name1.txt,type1_name.txt,type3_name.txt
.
.
.
.
so on
how can i achieve the output in below format by applying the if condition logic
outputfile.txt
file_name1.txt - 10,type_name1.txt - 20
file_name2.txt - 20,type_name2.txt - 30
As there are potentially multiple type_name options I would read the line split into an array so you can then work of the individual entries and apply your calculations as necessary.
Code:
#!/bin/bash
while IFS=, read -a arr
do
for item in ${arr[*]}
do
[[ $item =~ file_name ]] && { s=1; c= ; } || { s=2; c=,; }
printf "%c%s = %d" "$c" $item $(($(cat $item | wc -l) - s))
done
echo
done<$1
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.