Finding nth line across multiple files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Finding nth line across multiple files
# 1  
Old 10-01-2010
Finding nth line across multiple files

I have several files (around 50) that have the similar format. I need to extract the 5th line from every file and output that into a text file. So far, I have been able to figure out how to do it for a single file:

$ awk 'NR==5' text1.txt > results.txt
OR
$ sed -n '5p' text1.txt > results.txt
OR
$ head -n5 text1.txt | tail -1 > results.txt

However, if I replace "text1.txt" with *.txt and run it across 50 files, I only get the result of the last file. How do I get the results of all files?

Thanks in advance for your help!
# 2  
Old 10-01-2010
Code:
$ awk 'NR==5' *.txt >> results.txt

# 3  
Old 10-01-2010
This didn't work.

I think it's because the ">>" only appends if you have several outputs, whereas the ">" replaces if you have several outputs.
The awk command you posted (and the other commands I have) only produce one output even when it spans across several files.
# 4  
Old 10-01-2010
Try:
Code:
$ awk 'FNR==5' *.txt > results.txt

This User Gave Thanks to bartus11 For This Post:
# 5  
Old 10-01-2010
Thanks! That helped a lot! Not to be a pain, but would you know how to format it so all the results don't appear on one line?
# 6  
Old 10-01-2010
Hmmm.. they shouldn't appear on one line. Post your some sample of your input file.
# 7  
Old 10-01-2010
----------------------------------------------
INF: File: 11.hst
INF: Completion time: 00:00:41 Records/Second: 48

INF: Total Number of Records in File(s): 2005
INF: Number of Records Included in Analysis: 2005
INF: Number of Records Excluded from Analysis: 0


That's a sample from one input file. There's more, but I'm interested in the 5th line from about 40-something more...

---------- Post updated at 04:11 PM ---------- Previous update was at 04:08 PM ----------

OK. I feel extremely stupid. It was my text editor. The file formats just fine... Thanks for all your help Bartus!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calculating average for every Nth line in the Nth column

Is there an awk script that can easily perform the following operation? I have a data file that is in the format of 1944-12,5.6 1945-01,9.8 1945-02,6.7 1945-03,9.3 1945-04,5.9 1945-05,0.7 1945-06,0.0 1945-07,0.0 1945-08,0.0 1945-09,0.0 1945-10,0.2 1945-11,10.5 1945-12,22.3... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

2. Shell Programming and Scripting

finding matches between multiple files from different directories

Hi all... Can somebody pls help me with this... I have a directory (dir1) which has many subdirectories(vr001,vr002,vr003..) with each subdir containing similar text file(say ras.txt). I have another directory(dir2) which has again got some subdir(vr001c,vr002c,vr003c..) with each subdir... (0 Replies)
Discussion started by: bramya07
0 Replies

3. Shell Programming and Scripting

Finding Nth Column

Please help me how can I display every nth field present in a "|" delimited file. Ex: If a have a file with data as a|b|c|d|e|f|g|h|k|l|m|n I want to display every 3rd feild which means the output should be c f k n Please help me. (1 Reply)
Discussion started by: ngkumar
1 Replies

4. UNIX for Dummies Questions & Answers

finding the nth match

I have a file that has information for a person....each person gets 3 or more lines to describe them. I was hoping to match person 1, then person 2.....BUT I do not know how to tell grep I only want the first (2nd, 3rd or nth) match. The alternative is doing line by line logic, which is fine... (8 Replies)
Discussion started by: countryStyle
8 Replies

5. Shell Programming and Scripting

finding multiple files using find command

I am trying to search for 2 files using the find command as below find -name file1.txt -a -name file2.txt It doesn't give a result although the files exist in the folder, however when i try the following find -name file1.txt -o -name file2.txt It does give me the result. ./file2.txt... (4 Replies)
Discussion started by: vivek_damodaran
4 Replies

6. Shell Programming and Scripting

extract nth line of all files and print in output file on separate lines.

Hello UNIX experts, I have 124 text files in a directory. I want to extract the 45678th line of all the files sequentialy by file names. The extracted lines should be printed in the output file on seperate lines. e.g. The input Files are one.txt, two.txt, three.txt, four.txt The cat of four... (1 Reply)
Discussion started by: yogeshkumkar
1 Replies

7. Shell Programming and Scripting

multiple regex finding in files

Hello folks, I have a text file aa.txt that contains below text (\')|(\-\-) ((\%3D)|(=)) 20%0d% i want to search each line pattern in /opt/1.log and /opt/2.log. Can some one suggest (1 Reply)
Discussion started by: learnbash
1 Replies

8. UNIX for Dummies Questions & Answers

Finding names in multiple files - second attempt

I couldn't find the original thread that I created and since I didn't get a definitive answer, I figured I'd try again. Maybe this time I can describe what I want a little better. I've got two files, each with thousands of names all separated by new line. I want to know if 'name in file1'... (2 Replies)
Discussion started by: Rally_Point
2 Replies

9. UNIX for Dummies Questions & Answers

Finding Names in multiple files

What's the best way to see if a common name exists in two separate files? (3 Replies)
Discussion started by: Rally_Point
3 Replies

10. UNIX for Dummies Questions & Answers

Finding nth occurrence in line and replacing it

Hi, I have several files with data that have to be imported to a database. These files contain records with separator characters. Some records are corrupt (2 separators are missing) and I need to correct them prior to importing them into the db. Example: ... (5 Replies)
Discussion started by: stresing
5 Replies
Login or Register to Ask a Question