Get first 5 lines of a file, using a file of program names as input (Unix)









up vote
0
down vote

favorite












Goal: using an input file with a list of file names, get the first 5 lines of each file and output to another file. Basically, I'm trying to find out what each program does by reading the header.



Shell: Ksh



Input: myfile.txt



tmp/file1.txt
tmp/file2.txt


Output:



tmp/file1.txt - "Creates web login screen"
tmp/file2.txt - "Updates user login"


I can use "head -5" but not sure how to get the input from the file. I'm assuming I could redirect (>> output.txt)the output for my output file.



Input file names use a relative path.



Update: I created a script below but I'm getting "syntax error: unexpected end of file". The script was created with VI.



! /bin/sh



cat $HOME/jmarti20.list | while read line
do
#echo $line" >> jmarti20.txt
head -n 5 /los_prod/$line >> $HOME/jmarti20.txt
done










share|improve this question























  • Did you have a question?
    – mypetlion
    Nov 6 at 17:09










  • "Creates web login screen" doesn't look like "5 lines", more like a single line.
    – Benjamin W.
    Nov 6 at 17:09










  • Also, /tmp/file1.txt looks like an absolute path.
    – Benjamin W.
    Nov 6 at 17:11










  • How do I get input from a file as input to my "head -5" command? In the response from hellork, "file1.txt" is a file with the names of files. 5 lines is in general. Sometimes it will be 1 sometimes more than 5.
    – Marty
    Nov 6 at 18:03















up vote
0
down vote

favorite












Goal: using an input file with a list of file names, get the first 5 lines of each file and output to another file. Basically, I'm trying to find out what each program does by reading the header.



Shell: Ksh



Input: myfile.txt



tmp/file1.txt
tmp/file2.txt


Output:



tmp/file1.txt - "Creates web login screen"
tmp/file2.txt - "Updates user login"


I can use "head -5" but not sure how to get the input from the file. I'm assuming I could redirect (>> output.txt)the output for my output file.



Input file names use a relative path.



Update: I created a script below but I'm getting "syntax error: unexpected end of file". The script was created with VI.



! /bin/sh



cat $HOME/jmarti20.list | while read line
do
#echo $line" >> jmarti20.txt
head -n 5 /los_prod/$line >> $HOME/jmarti20.txt
done










share|improve this question























  • Did you have a question?
    – mypetlion
    Nov 6 at 17:09










  • "Creates web login screen" doesn't look like "5 lines", more like a single line.
    – Benjamin W.
    Nov 6 at 17:09










  • Also, /tmp/file1.txt looks like an absolute path.
    – Benjamin W.
    Nov 6 at 17:11










  • How do I get input from a file as input to my "head -5" command? In the response from hellork, "file1.txt" is a file with the names of files. 5 lines is in general. Sometimes it will be 1 sometimes more than 5.
    – Marty
    Nov 6 at 18:03













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Goal: using an input file with a list of file names, get the first 5 lines of each file and output to another file. Basically, I'm trying to find out what each program does by reading the header.



Shell: Ksh



Input: myfile.txt



tmp/file1.txt
tmp/file2.txt


Output:



tmp/file1.txt - "Creates web login screen"
tmp/file2.txt - "Updates user login"


I can use "head -5" but not sure how to get the input from the file. I'm assuming I could redirect (>> output.txt)the output for my output file.



Input file names use a relative path.



Update: I created a script below but I'm getting "syntax error: unexpected end of file". The script was created with VI.



! /bin/sh



cat $HOME/jmarti20.list | while read line
do
#echo $line" >> jmarti20.txt
head -n 5 /los_prod/$line >> $HOME/jmarti20.txt
done










share|improve this question















Goal: using an input file with a list of file names, get the first 5 lines of each file and output to another file. Basically, I'm trying to find out what each program does by reading the header.



Shell: Ksh



Input: myfile.txt



tmp/file1.txt
tmp/file2.txt


Output:



tmp/file1.txt - "Creates web login screen"
tmp/file2.txt - "Updates user login"


I can use "head -5" but not sure how to get the input from the file. I'm assuming I could redirect (>> output.txt)the output for my output file.



Input file names use a relative path.



Update: I created a script below but I'm getting "syntax error: unexpected end of file". The script was created with VI.



! /bin/sh



cat $HOME/jmarti20.list | while read line
do
#echo $line" >> jmarti20.txt
head -n 5 /los_prod/$line >> $HOME/jmarti20.txt
done







shell unix






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 17:25

























asked Nov 6 at 16:59









Marty

11




11











  • Did you have a question?
    – mypetlion
    Nov 6 at 17:09










  • "Creates web login screen" doesn't look like "5 lines", more like a single line.
    – Benjamin W.
    Nov 6 at 17:09










  • Also, /tmp/file1.txt looks like an absolute path.
    – Benjamin W.
    Nov 6 at 17:11










  • How do I get input from a file as input to my "head -5" command? In the response from hellork, "file1.txt" is a file with the names of files. 5 lines is in general. Sometimes it will be 1 sometimes more than 5.
    – Marty
    Nov 6 at 18:03

















  • Did you have a question?
    – mypetlion
    Nov 6 at 17:09










  • "Creates web login screen" doesn't look like "5 lines", more like a single line.
    – Benjamin W.
    Nov 6 at 17:09










  • Also, /tmp/file1.txt looks like an absolute path.
    – Benjamin W.
    Nov 6 at 17:11










  • How do I get input from a file as input to my "head -5" command? In the response from hellork, "file1.txt" is a file with the names of files. 5 lines is in general. Sometimes it will be 1 sometimes more than 5.
    – Marty
    Nov 6 at 18:03
















Did you have a question?
– mypetlion
Nov 6 at 17:09




Did you have a question?
– mypetlion
Nov 6 at 17:09












"Creates web login screen" doesn't look like "5 lines", more like a single line.
– Benjamin W.
Nov 6 at 17:09




"Creates web login screen" doesn't look like "5 lines", more like a single line.
– Benjamin W.
Nov 6 at 17:09












Also, /tmp/file1.txt looks like an absolute path.
– Benjamin W.
Nov 6 at 17:11




Also, /tmp/file1.txt looks like an absolute path.
– Benjamin W.
Nov 6 at 17:11












How do I get input from a file as input to my "head -5" command? In the response from hellork, "file1.txt" is a file with the names of files. 5 lines is in general. Sometimes it will be 1 sometimes more than 5.
– Marty
Nov 6 at 18:03





How do I get input from a file as input to my "head -5" command? In the response from hellork, "file1.txt" is a file with the names of files. 5 lines is in general. Sometimes it will be 1 sometimes more than 5.
– Marty
Nov 6 at 18:03













2 Answers
2






active

oldest

votes

















up vote
0
down vote













Right, you can append output with >> to a file.



head -n 5 file1.txt >> file_descriptions.txt


You can also use sed to print lines, from documentation at pinfo sed.



sed 5q file1.txt >> file_descriptions.txt


Personal preference is to put file description in line 3, and only print line 3 of files.



sed -n 3p file1.txt >> file_descriptions.txt


The reasoning for using line 3 has to do with the first line often containing a "shebang" like #!/bin/bash, and the 2nd line having localization strings, such as # -*- coding: UTF-8 -*-, to allow proper display of extra character glyphs and languages in terminals and text editors that support them.






share|improve this answer






















  • Great response! How do I input a list of files instead of "file1.txt"? Meaning I have a file from another process that generated a bunch of file names.
    – Marty
    Nov 6 at 18:06










  • You could use a while loop and read command to put each input line in a $filename variable, and append that to the output file. mywiki.wooledge.org/BashFAQ/001
    – hellork
    Nov 11 at 6:02


















up vote
0
down vote













Below is what I came up with and seems to work fairly well:



#! /bin/sh
cat $HOME/jmarti20.list | while read line
do
temp=$line
temp2=$(head -n 5 /los_prod/$line)
echo "$temp" "$temp2" >> jmarti20.txt
#echo "$line" >> jmarti20.txt
#head -n 5 /los_prod/$line >> $HOME/jmarti20.txt
done





share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53176547%2fget-first-5-lines-of-a-file-using-a-file-of-program-names-as-input-unix%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Right, you can append output with >> to a file.



    head -n 5 file1.txt >> file_descriptions.txt


    You can also use sed to print lines, from documentation at pinfo sed.



    sed 5q file1.txt >> file_descriptions.txt


    Personal preference is to put file description in line 3, and only print line 3 of files.



    sed -n 3p file1.txt >> file_descriptions.txt


    The reasoning for using line 3 has to do with the first line often containing a "shebang" like #!/bin/bash, and the 2nd line having localization strings, such as # -*- coding: UTF-8 -*-, to allow proper display of extra character glyphs and languages in terminals and text editors that support them.






    share|improve this answer






















    • Great response! How do I input a list of files instead of "file1.txt"? Meaning I have a file from another process that generated a bunch of file names.
      – Marty
      Nov 6 at 18:06










    • You could use a while loop and read command to put each input line in a $filename variable, and append that to the output file. mywiki.wooledge.org/BashFAQ/001
      – hellork
      Nov 11 at 6:02















    up vote
    0
    down vote













    Right, you can append output with >> to a file.



    head -n 5 file1.txt >> file_descriptions.txt


    You can also use sed to print lines, from documentation at pinfo sed.



    sed 5q file1.txt >> file_descriptions.txt


    Personal preference is to put file description in line 3, and only print line 3 of files.



    sed -n 3p file1.txt >> file_descriptions.txt


    The reasoning for using line 3 has to do with the first line often containing a "shebang" like #!/bin/bash, and the 2nd line having localization strings, such as # -*- coding: UTF-8 -*-, to allow proper display of extra character glyphs and languages in terminals and text editors that support them.






    share|improve this answer






















    • Great response! How do I input a list of files instead of "file1.txt"? Meaning I have a file from another process that generated a bunch of file names.
      – Marty
      Nov 6 at 18:06










    • You could use a while loop and read command to put each input line in a $filename variable, and append that to the output file. mywiki.wooledge.org/BashFAQ/001
      – hellork
      Nov 11 at 6:02













    up vote
    0
    down vote










    up vote
    0
    down vote









    Right, you can append output with >> to a file.



    head -n 5 file1.txt >> file_descriptions.txt


    You can also use sed to print lines, from documentation at pinfo sed.



    sed 5q file1.txt >> file_descriptions.txt


    Personal preference is to put file description in line 3, and only print line 3 of files.



    sed -n 3p file1.txt >> file_descriptions.txt


    The reasoning for using line 3 has to do with the first line often containing a "shebang" like #!/bin/bash, and the 2nd line having localization strings, such as # -*- coding: UTF-8 -*-, to allow proper display of extra character glyphs and languages in terminals and text editors that support them.






    share|improve this answer














    Right, you can append output with >> to a file.



    head -n 5 file1.txt >> file_descriptions.txt


    You can also use sed to print lines, from documentation at pinfo sed.



    sed 5q file1.txt >> file_descriptions.txt


    Personal preference is to put file description in line 3, and only print line 3 of files.



    sed -n 3p file1.txt >> file_descriptions.txt


    The reasoning for using line 3 has to do with the first line often containing a "shebang" like #!/bin/bash, and the 2nd line having localization strings, such as # -*- coding: UTF-8 -*-, to allow proper display of extra character glyphs and languages in terminals and text editors that support them.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 11 at 6:06

























    answered Nov 6 at 17:21









    hellork

    1035




    1035











    • Great response! How do I input a list of files instead of "file1.txt"? Meaning I have a file from another process that generated a bunch of file names.
      – Marty
      Nov 6 at 18:06










    • You could use a while loop and read command to put each input line in a $filename variable, and append that to the output file. mywiki.wooledge.org/BashFAQ/001
      – hellork
      Nov 11 at 6:02

















    • Great response! How do I input a list of files instead of "file1.txt"? Meaning I have a file from another process that generated a bunch of file names.
      – Marty
      Nov 6 at 18:06










    • You could use a while loop and read command to put each input line in a $filename variable, and append that to the output file. mywiki.wooledge.org/BashFAQ/001
      – hellork
      Nov 11 at 6:02
















    Great response! How do I input a list of files instead of "file1.txt"? Meaning I have a file from another process that generated a bunch of file names.
    – Marty
    Nov 6 at 18:06




    Great response! How do I input a list of files instead of "file1.txt"? Meaning I have a file from another process that generated a bunch of file names.
    – Marty
    Nov 6 at 18:06












    You could use a while loop and read command to put each input line in a $filename variable, and append that to the output file. mywiki.wooledge.org/BashFAQ/001
    – hellork
    Nov 11 at 6:02





    You could use a while loop and read command to put each input line in a $filename variable, and append that to the output file. mywiki.wooledge.org/BashFAQ/001
    – hellork
    Nov 11 at 6:02













    up vote
    0
    down vote













    Below is what I came up with and seems to work fairly well:



    #! /bin/sh
    cat $HOME/jmarti20.list | while read line
    do
    temp=$line
    temp2=$(head -n 5 /los_prod/$line)
    echo "$temp" "$temp2" >> jmarti20.txt
    #echo "$line" >> jmarti20.txt
    #head -n 5 /los_prod/$line >> $HOME/jmarti20.txt
    done





    share|improve this answer
























      up vote
      0
      down vote













      Below is what I came up with and seems to work fairly well:



      #! /bin/sh
      cat $HOME/jmarti20.list | while read line
      do
      temp=$line
      temp2=$(head -n 5 /los_prod/$line)
      echo "$temp" "$temp2" >> jmarti20.txt
      #echo "$line" >> jmarti20.txt
      #head -n 5 /los_prod/$line >> $HOME/jmarti20.txt
      done





      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        Below is what I came up with and seems to work fairly well:



        #! /bin/sh
        cat $HOME/jmarti20.list | while read line
        do
        temp=$line
        temp2=$(head -n 5 /los_prod/$line)
        echo "$temp" "$temp2" >> jmarti20.txt
        #echo "$line" >> jmarti20.txt
        #head -n 5 /los_prod/$line >> $HOME/jmarti20.txt
        done





        share|improve this answer












        Below is what I came up with and seems to work fairly well:



        #! /bin/sh
        cat $HOME/jmarti20.list | while read line
        do
        temp=$line
        temp2=$(head -n 5 /los_prod/$line)
        echo "$temp" "$temp2" >> jmarti20.txt
        #echo "$line" >> jmarti20.txt
        #head -n 5 /los_prod/$line >> $HOME/jmarti20.txt
        done






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 at 12:50









        Marty

        11




        11



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53176547%2fget-first-5-lines-of-a-file-using-a-file-of-program-names-as-input-unix%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto