Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6763693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:30:24+00:00 2026-05-26T14:30:24+00:00

I have a folder called foo. Foo has some other folders which might have

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:30:25+00:00Added an answer on May 26, 2026 at 2:30 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a folder called Etc which has an image I want to use
In my site I have a folder called module . It has two sub-folders
I have a folder called Client which contains many subfolders. I want to create
Lets say I have a script called foo in some folder and a script
I have a folder called exam. This folder has 3 folders called math, physics
I have a folder called myfolder , that has some items inside, like: item1
say that i have a project which lies in a folder called 'bin', and
I have a dvd copied in my hard drive. It has a folder called
I have a folder, called files. It's already in the repository. Now, new files
I have a folder called Datas. This folder has a subfolder called Inbox inside

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.