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?
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
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.
(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.
If you want all of your sliced lines to go into 1 file, then use the shells ‘append-redirection’, i.e.
The other postings, with using the results of
find ...to drive your filelist, are an excellent approach.I hope this helps.