Writing a script to print the number of lines in multiple files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Writing a script to print the number of lines in multiple files
# 1  
Old 09-10-2012
Writing a script to print the number of lines in multiple files

Hi

I have 1000 files labelled data1.txt through data1000.txt. I want to write a script that prints out the number of lines in each txt file and outputs it in the following format:

Column 1: number of data file (1 through 1000)
Column 2: number of lines in the text file

Thanks!
# 2  
Old 09-10-2012
Try this

Code:
for filename in `ls data*`
do
echo "$filename \t `wc -l < $filename`"
done


This User Gave Thanks to NareshN For This Post:
# 3  
Old 09-10-2012
Like this?
Code:
if wc -l data*.txt 2>/dev/null >flist
then
 awk '$2!="total"{gsub(/^data|\.txt$/,"",$2);print $2,$1}' flist
fi
rm flist

And if you are sure that at least 1 file exists (which matches the pattern) and if your shell supports process substitution, you could try:
Code:
awk '$2!="total"{gsub(/^data|\.txt$/,"",$2);print $2,$1}' <(wc -l data*.txt)


Last edited by elixir_sinari; 09-10-2012 at 04:46 AM..
This User Gave Thanks to elixir_sinari For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print number of lines for files in directory, also print number of unique lines

I have a directory of files, I can show the number of lines in each file and order them from lowest to highest with: wc -l *|sort 15263 Image.txt 16401 reference.txt 40459 richtexteditor.txt How can I also print the number of unique lines in each file? 15263 1401 Image.txt 16401... (15 Replies)
Discussion started by: spacegoose
15 Replies

2. Shell Programming and Scripting

Removing carriage returns from multiple lines in multiple files of different number of columns

Hello Gurus, I have a multiple pipe separated files which have records going over multiple Lines. End of line separator is \n and records going over multiple lines have <CR> as separator. below is example from one file. 1|ABC DEF|100|10 2|PQ RS T|200|20 3| UVWXYZ|300|30 4| GHIJKL|400|40... (7 Replies)
Discussion started by: dJHa
7 Replies

3. Shell Programming and Scripting

Run one script on multiple files and print out multiple files.

How can I Run one script on multiple files and print out multiple files. FOR EXAMPLE i want to run script.pl on 100 files named 1.txt ....100.txt under same directory and print out corresponding file 1.gff ....100.gff.THANKS (4 Replies)
Discussion started by: grace_shen
4 Replies

4. UNIX for Dummies Questions & Answers

Run one script on multiple files and print out multiple files.

How can I run the following command on multiple files and print out the corresponding multiple files. perl script.pl genome.gff 1.txt > 1.gff However, there are multiples files of 1.txt, from 1----100.txt Thank you so much. No duplicate posting! Continue here. (0 Replies)
Discussion started by: grace_shen
0 Replies

5. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 Replies

6. Shell Programming and Scripting

Compare multiple files and print unique lines

Hi friends, I have multiple files. For now, let's say I have two of the following style cat 1.txt cat 2.txt output.txt Please note that my files are not sorted and in the output file I need another extra column that says the file from which it is coming. I have more than 100... (19 Replies)
Discussion started by: jacobs.smith
19 Replies

7. UNIX for Dummies Questions & Answers

Writing a loop to process multiple input files by a shell script

I have multiple input files that I want to manipulate using a shell script. The files are called 250.1 through 250.1000 but I only want the script to manipulate 250.300 through 250.1000. Before I was using the following script to manipulate the text files: for i in 250.*; do || awk... (4 Replies)
Discussion started by: evelibertine
4 Replies

8. Shell Programming and Scripting

Writing a Perl Script that processes multiple files

I want to write a Perl script that manipulates multiple files. In the directory, I have files 250.*chr$.ped where * is from 1 to 1000 and $ is from 1-22 for a total of 22 x 10,000 = 22,000 files. I want to write a script that only manipulates files 250.1chr*.ped where * is from 1 to 22.... (10 Replies)
Discussion started by: evelibertine
10 Replies

9. UNIX for Dummies Questions & Answers

Writing a loop to manipulate a script and store it in multiple output files

I have a script where the the 9th line looks like this: $filename=sprintf("250.1chr%d.ped", $N); I want to modify this script 1000 times, changing 250.1chr%d.ped to 250.2chr%d.ped, 250.3chr%.ped.......and so on all the way to 250.1000chr%d.ped and store each output in files called ... (4 Replies)
Discussion started by: evelibertine
4 Replies

10. Shell Programming and Scripting

AWK print lines into multiple files

Hi, i have an input text file like this: Student 1 maths science = Student 2 maths science = Student 3 maths science i would like to print each student information into separate files, each student id is separated by "=". (1 Reply)
Discussion started by: saint2006
1 Replies
Login or Register to Ask a Question