Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I read the Nth line of a file and print it to a new file? [duplicate]

Tags:

bash

shell

unix

I have a folder called foo. Foo has some other folders which might have sub folders and text files. I want to find every file which begins with the name year and and read its Nth line and print it to a new file. For example foo has a file called year1 and the sub folders have files called year2, year3 etc. The program will print the 1st line of year1 to a file called writeout, then it will print the 2nd line of year2 to the file writeout etc.

I also didn't really understand how to do a for loop for a file.

So far I have:

#!/bin/bash

for year* in ~/foo
do
  Here I tried writing some code using the sed command but I can't think of something       else.
done

I also get a message in the terminal which says `year*' not a valid identifier. Any ideas?

like image 723
captain Avatar asked Nov 03 '11 14:11

captain


People also ask

What command will print nth line what will print last line?

N,$ with “p” command prints from Nth line to end of file.


2 Answers

Sed can help you.

Recall that sed will normally process all lines in a file AND print each line in the file.

You can turn off that feature, and have sed only print lines of interest by matching a pattern or line number.

So, to print the 2nd line of file 2, you can say

sed -n '2p' file2 > newFile2

To print the 2nd line and then stop processing add the q (for quit) command (you also need braces to group the 2 commands together), i.e.

sed -n '2{p;q;}' file2 > newFile2

(if you are processing large files, this can be quite a time saving).

To make that more general, you can change the number to a variable that will hold a number, i.e.

  lineNo=3
  sed -n "${lineNo}{p;q;}" file3 > newFile3

If you want all of your sliced lines to go into 1 file, then use the shells 'append-redirection', i.e.

 for lineNo in 1 2 3 4 5 ; do
     sed -n  "${lineNo}{p;q;}" file${lineNo} >> aggregateFile
 done

The other postings, with using the results of find ... to drive your filelist, are an excellent approach.

I hope this helps.

like image 175
shellter Avatar answered Sep 28 '22 04:09

shellter


Here is one way to do it:

awk "NR==$YEAR" $file
like image 36
Karoly Horvath Avatar answered Sep 28 '22 03:09

Karoly Horvath