LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-29-2013, 10:36 AM   #1
rohit_shinez
Member
 
Registered: Aug 2013
Posts: 40

Rep: Reputation: Disabled
Taking line count from file and printing in file


hi,

i am having a file which contains the below content, i need to take the line count of if and print the file name also


Code:
inputfile.txt
file_name1.txt,type_name1.txt
file_name2.txt,type_name2.txt

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
 
Old 08-29-2013, 10:43 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
you should use code tags to preserve your formatting.

i would the output of wc -l in a variable and use expr to subtract what you dont want.
 
Old 08-29-2013, 10:50 AM   #3
rohit_shinez
Member
 
Registered: Aug 2013
Posts: 40

Original Poster
Rep: Reputation: Disabled
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
 
Old 08-29-2013, 10:58 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
I would have to say I am a little confused (maybe a lot)

You have a file called inputfile.txt with 2 lines in it:
Code:
file_name1.txt,type_name1.txt
file_name2.txt,type_name2.txt
With you so far

But then you pull an 'if' on what exactly?
Code:
if file_name*.txt
The only file you have mentioned is inputfile.txt, so I would expect neither 'if' to work at all???

As for your output file, as I am lost on the above I cannot see how came up with any of the figures you have displayed
 
Old 08-29-2013, 11:00 AM   #5
rohit_shinez
Member
 
Registered: Aug 2013
Posts: 40

Original Poster
Rep: Reputation: Disabled
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
 
Old 08-29-2013, 11:41 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
And how exactly are you getting the data out of inputfile.txt so the 'if' statements can see the data?
Quote:
i need to apply line count with subtracting 1
This makes little sense, line count of what? There are 2 lines in inputfile.txt, are we subtracting 1 from that count?

You need to be clearer on how this is supposed to work and how and what data is being manipulated.
 
Old 08-29-2013, 11:50 AM   #7
rohit_shinez
Member
 
Registered: Aug 2013
Posts: 40

Original Poster
Rep: Reputation: Disabled
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

file_name1.txt - 10,type_name1.txt - 20
file_name2.txt - 20,type_name2.txt - 30

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
 
Old 08-29-2013, 11:56 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Ok ... that is becoming clearer

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))
 
Old 08-29-2013, 12:01 PM   #9
rohit_shinez
Member
 
Registered: Aug 2013
Posts: 40

Original Poster
Rep: Reputation: Disabled
while IFS=',' read -r filename data; do
...
done < "inputfile.txt"

this like i thought i would read it hope this would help
 
Old 08-29-2013, 12:04 PM   #10
rohit_shinez
Member
 
Registered: Aug 2013
Posts: 40

Original Poster
Rep: Reputation: Disabled
or can use the below logic as well

for i `çat inputfile.txt`
do
read -a FOO <<< $(wc -l $i)
echo "${FOO[1]} - ${FOO[0]}"
done

the above one can be used for reference
 
Old 08-29-2013, 03:36 PM   #11
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Quote:
Originally Posted by schneidz View Post
you should use code tags to preserve your formatting.

i would the output of wc -l in a variable and use expr to subtract what you dont want.
Code:
for item in *
do
 if [ "`echo $item | grep ^file_name`" ]
 then
  n=`wc -l $item | awk '{print $1}'` # 'grep -c . $item' is probably more elegant here
  n=`expr $n - 1`
  echo $item - $n
 fi
 if [ "`echo $item | grep ^type_name`" ]
 then
  n=`wc -l $item | awk '{print $1}'`
  n=`expr $n - 2`
  echo $item - $n
 fi
done

Last edited by schneidz; 08-29-2013 at 03:37 PM.
 
1 members found this post helpful.
Old 08-29-2013, 05:37 PM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
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?
 
Old 08-30-2013, 12:53 PM   #13
rohit_shinez
Member
 
Registered: Aug 2013
Posts: 40

Original Poster
Rep: Reputation: Disabled
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
 
Old 08-30-2013, 01:19 PM   #14
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
On most systems printf is a command you can call to create formatted output. For example:
Code:
for item in file_name*
do
  name=${item:9}
  n=$(wc -l ${item} | awk '{print $1}') # 'grep -c . $item' is probably more elegant here
  n=$((n - 1))
  if [ -f "type_name${name}" ]
  then
   m=$(wc -l "type_name${name}" | awk '{print $1}')
   m=$((m-2))
   printf "file_name%s = %d, type_name%s = %d\n" "${name}" ${n} "${name}" ${m}
 else
  printf "\nfile_name%s = %d, type_name%s not found.\n" "${name}" ${n} "${name}"
 fi
done
might work.
Warning: UNTESTED CODE

Last edited by PTrenholme; 08-30-2013 at 01:24 PM. Reason: Added error branch to if
 
Old 08-31-2013, 03:01 AM   #15
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] selecting a font when printing a text file from the command line frumpus Linux - General 2 07-29-2011 04:47 PM
Trying to identify file types(command line arguments) and count how many there are jdwalk Linux - Newbie 5 02-20-2010 03:51 PM
[SOLVED] spliting a text file into several parts by line count clstanton Linux - Newbie 2 09-21-2009 01:49 PM
[c shell] printing out each line in a file saiz66 Programming 4 10-12-2004 08:15 AM
Count number of line in a file. philipina Programming 7 03-18-2004 05:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration