find and print particular line from list of filename and line number

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












I have a file input.txt which contains multiple filename in the below format.
FILENAME_DATE_LINENUMBER , the input.txt contains many such filenames.
The filename itself has precisely 5 underscore.



FILE_NAME_1.DAT_20180123_4
FILE_NAME_2.DAT_20180123_5
FILE_NAME_3.DAT_20180123_6
FILE_NAME_4.DAT_20180123_7


All files are present in sub directory as input.txt. I want to parse input.txt , iterate through each filename and print FILENAME and the specified line number ( from the FILENAME ) to output.txt



I am beginner at shell script and I understand that sed or awk will be used , and below command can do the job.



awk 'FNR==LINENUMBER print FILENAME, $0' *.txt >output.txt


But how can i iterate through the file input.txt and find the FILENAME and extract LINENUMBER from FILENAME to output.txt



The FILENAME specified in input.txt can in one of the sub directories where input.txt is located. There can be only one file with FILENAME in input.txt inside one of the sub directory ( one level ) form the input.txt location.



DIR
├── input.txt
│ ├── DIR1
│ │ ├── FILE_NAME_1.DAT
│ ├── DIR2
│ │ ├── FILE_NAME_2.DAT
│ ├── DIR3
│ │ ├── FILE_NAME_3.DAT


In output.txt it should be printed as



FILENAME
LINE( Extracted from FILENAME present in input.txt )






share|improve this question


















  • 1




    With such questions you should always give example output. You want FILE1.DAT_20180123_4 transformed into FILE1.DAT 4?
    – Hauke Laging
    Jan 23 at 17:43










  • @shubhamdeodia, your output should be FILENAME LINE(under number) - update your question
    – RomanPerekhrest
    Jan 23 at 18:13











  • @RomanPerekhrest , thank you. I have updated.
    – shubham deodia
    Jan 23 at 18:19











  • @shubhamdeodia, ok, can we assume that all files FILE*.DAT... are in the same directory as input.txt ?
    – RomanPerekhrest
    Jan 23 at 18:35











  • @RomanPerekhrest , no they are in the sub directories ( one level down ) from where input.txt is and we don't even know the name of sub directory.
    – shubham deodia
    Jan 23 at 18:40














up vote
2
down vote

favorite












I have a file input.txt which contains multiple filename in the below format.
FILENAME_DATE_LINENUMBER , the input.txt contains many such filenames.
The filename itself has precisely 5 underscore.



FILE_NAME_1.DAT_20180123_4
FILE_NAME_2.DAT_20180123_5
FILE_NAME_3.DAT_20180123_6
FILE_NAME_4.DAT_20180123_7


All files are present in sub directory as input.txt. I want to parse input.txt , iterate through each filename and print FILENAME and the specified line number ( from the FILENAME ) to output.txt



I am beginner at shell script and I understand that sed or awk will be used , and below command can do the job.



awk 'FNR==LINENUMBER print FILENAME, $0' *.txt >output.txt


But how can i iterate through the file input.txt and find the FILENAME and extract LINENUMBER from FILENAME to output.txt



The FILENAME specified in input.txt can in one of the sub directories where input.txt is located. There can be only one file with FILENAME in input.txt inside one of the sub directory ( one level ) form the input.txt location.



DIR
├── input.txt
│ ├── DIR1
│ │ ├── FILE_NAME_1.DAT
│ ├── DIR2
│ │ ├── FILE_NAME_2.DAT
│ ├── DIR3
│ │ ├── FILE_NAME_3.DAT


In output.txt it should be printed as



FILENAME
LINE( Extracted from FILENAME present in input.txt )






share|improve this question


















  • 1




    With such questions you should always give example output. You want FILE1.DAT_20180123_4 transformed into FILE1.DAT 4?
    – Hauke Laging
    Jan 23 at 17:43










  • @shubhamdeodia, your output should be FILENAME LINE(under number) - update your question
    – RomanPerekhrest
    Jan 23 at 18:13











  • @RomanPerekhrest , thank you. I have updated.
    – shubham deodia
    Jan 23 at 18:19











  • @shubhamdeodia, ok, can we assume that all files FILE*.DAT... are in the same directory as input.txt ?
    – RomanPerekhrest
    Jan 23 at 18:35











  • @RomanPerekhrest , no they are in the sub directories ( one level down ) from where input.txt is and we don't even know the name of sub directory.
    – shubham deodia
    Jan 23 at 18:40












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have a file input.txt which contains multiple filename in the below format.
FILENAME_DATE_LINENUMBER , the input.txt contains many such filenames.
The filename itself has precisely 5 underscore.



FILE_NAME_1.DAT_20180123_4
FILE_NAME_2.DAT_20180123_5
FILE_NAME_3.DAT_20180123_6
FILE_NAME_4.DAT_20180123_7


All files are present in sub directory as input.txt. I want to parse input.txt , iterate through each filename and print FILENAME and the specified line number ( from the FILENAME ) to output.txt



I am beginner at shell script and I understand that sed or awk will be used , and below command can do the job.



awk 'FNR==LINENUMBER print FILENAME, $0' *.txt >output.txt


But how can i iterate through the file input.txt and find the FILENAME and extract LINENUMBER from FILENAME to output.txt



The FILENAME specified in input.txt can in one of the sub directories where input.txt is located. There can be only one file with FILENAME in input.txt inside one of the sub directory ( one level ) form the input.txt location.



DIR
├── input.txt
│ ├── DIR1
│ │ ├── FILE_NAME_1.DAT
│ ├── DIR2
│ │ ├── FILE_NAME_2.DAT
│ ├── DIR3
│ │ ├── FILE_NAME_3.DAT


In output.txt it should be printed as



FILENAME
LINE( Extracted from FILENAME present in input.txt )






share|improve this question














I have a file input.txt which contains multiple filename in the below format.
FILENAME_DATE_LINENUMBER , the input.txt contains many such filenames.
The filename itself has precisely 5 underscore.



FILE_NAME_1.DAT_20180123_4
FILE_NAME_2.DAT_20180123_5
FILE_NAME_3.DAT_20180123_6
FILE_NAME_4.DAT_20180123_7


All files are present in sub directory as input.txt. I want to parse input.txt , iterate through each filename and print FILENAME and the specified line number ( from the FILENAME ) to output.txt



I am beginner at shell script and I understand that sed or awk will be used , and below command can do the job.



awk 'FNR==LINENUMBER print FILENAME, $0' *.txt >output.txt


But how can i iterate through the file input.txt and find the FILENAME and extract LINENUMBER from FILENAME to output.txt



The FILENAME specified in input.txt can in one of the sub directories where input.txt is located. There can be only one file with FILENAME in input.txt inside one of the sub directory ( one level ) form the input.txt location.



DIR
├── input.txt
│ ├── DIR1
│ │ ├── FILE_NAME_1.DAT
│ ├── DIR2
│ │ ├── FILE_NAME_2.DAT
│ ├── DIR3
│ │ ├── FILE_NAME_3.DAT


In output.txt it should be printed as



FILENAME
LINE( Extracted from FILENAME present in input.txt )








share|improve this question













share|improve this question




share|improve this question








edited Jan 23 at 19:44

























asked Jan 23 at 17:36









shubham deodia

134




134







  • 1




    With such questions you should always give example output. You want FILE1.DAT_20180123_4 transformed into FILE1.DAT 4?
    – Hauke Laging
    Jan 23 at 17:43










  • @shubhamdeodia, your output should be FILENAME LINE(under number) - update your question
    – RomanPerekhrest
    Jan 23 at 18:13











  • @RomanPerekhrest , thank you. I have updated.
    – shubham deodia
    Jan 23 at 18:19











  • @shubhamdeodia, ok, can we assume that all files FILE*.DAT... are in the same directory as input.txt ?
    – RomanPerekhrest
    Jan 23 at 18:35











  • @RomanPerekhrest , no they are in the sub directories ( one level down ) from where input.txt is and we don't even know the name of sub directory.
    – shubham deodia
    Jan 23 at 18:40












  • 1




    With such questions you should always give example output. You want FILE1.DAT_20180123_4 transformed into FILE1.DAT 4?
    – Hauke Laging
    Jan 23 at 17:43










  • @shubhamdeodia, your output should be FILENAME LINE(under number) - update your question
    – RomanPerekhrest
    Jan 23 at 18:13











  • @RomanPerekhrest , thank you. I have updated.
    – shubham deodia
    Jan 23 at 18:19











  • @shubhamdeodia, ok, can we assume that all files FILE*.DAT... are in the same directory as input.txt ?
    – RomanPerekhrest
    Jan 23 at 18:35











  • @RomanPerekhrest , no they are in the sub directories ( one level down ) from where input.txt is and we don't even know the name of sub directory.
    – shubham deodia
    Jan 23 at 18:40







1




1




With such questions you should always give example output. You want FILE1.DAT_20180123_4 transformed into FILE1.DAT 4?
– Hauke Laging
Jan 23 at 17:43




With such questions you should always give example output. You want FILE1.DAT_20180123_4 transformed into FILE1.DAT 4?
– Hauke Laging
Jan 23 at 17:43












@shubhamdeodia, your output should be FILENAME LINE(under number) - update your question
– RomanPerekhrest
Jan 23 at 18:13





@shubhamdeodia, your output should be FILENAME LINE(under number) - update your question
– RomanPerekhrest
Jan 23 at 18:13













@RomanPerekhrest , thank you. I have updated.
– shubham deodia
Jan 23 at 18:19





@RomanPerekhrest , thank you. I have updated.
– shubham deodia
Jan 23 at 18:19













@shubhamdeodia, ok, can we assume that all files FILE*.DAT... are in the same directory as input.txt ?
– RomanPerekhrest
Jan 23 at 18:35





@shubhamdeodia, ok, can we assume that all files FILE*.DAT... are in the same directory as input.txt ?
– RomanPerekhrest
Jan 23 at 18:35













@RomanPerekhrest , no they are in the sub directories ( one level down ) from where input.txt is and we don't even know the name of sub directory.
– shubham deodia
Jan 23 at 18:40




@RomanPerekhrest , no they are in the sub directories ( one level down ) from where input.txt is and we don't even know the name of sub directory.
– shubham deodia
Jan 23 at 18:40










4 Answers
4






active

oldest

votes

















up vote
0
down vote



accepted










#!/bin/bash 

do_one()
# two args: $1=filename_no_dir $2=line_number
# Find the single filename
eval file=*"/$1"
echo $1
# $. == line number
perl -ne 'chomp; $.=='"$2"' and print "LINE($_)n"' $file

export -f do_one

# Generate som test data
parallel 'mkdir DIR; seq 100 110 >DIR/FILE_NAME_.DAT' ::: 1..4

# Test input.txt
cat <<EOF |
FILE_NAME_1.DAT_20180123_4
FILE_NAME_2.DAT_20180123_5
FILE_NAME_3.DAT_20180123_6
FILE_NAME_4.DAT_20180123_7
EOF
# Remove _YYYYMMDD.* to get filename, and .*_ to get line number
parallel do_one '= s/_201ddddd.*// =' '= s/.*_// ='


Output:



FILE_NAME_1.DAT
LINE(103)
FILE_NAME_2.DAT
LINE(104)
FILE_NAME_3.DAT
LINE(105)
FILE_NAME_4.DAT
LINE(106)





share|improve this answer



























    up vote
    0
    down vote













    :> awk -F_ ' print $1; print $3; ' inputfile
    FILE1.DAT
    4
    FILE2.DAT
    5
    FILE3.DAT
    6
    FILE4.DAT
    7





    share|improve this answer




















    • what if FILE2.DAT is in nested directory from where inputfile.txt is. I mean we don't know the relative path for FILE2.DAT from inputfile and we don't even have the exact name of subdirectory.
      – shubham deodia
      Jan 23 at 17:57











    • @shubhamdeodia Are you complaining to me about your input data...?
      – Hauke Laging
      Jan 23 at 18:02










    • I am sorry , my question was a little confusing, I have corrected it. I want to find the FILENAME in input.txt and then find line number in FILENAME and print it to putput.txt
      – shubham deodia
      Jan 23 at 18:08

















    up vote
    0
    down vote













    If I'm understanding you correctly,



    while IFS=_ read -r filename unuseddate linenum
    do
    printf "%sn" "$filename"
    sed -n "$linenump;q" */"$filename"
    done < input.txt > output.txt


    This reads one line at a time from input.txt, splitting the line into 3 parts based on the underscore. It prints the filename then fires off a sed command that (by default prints nothing) and then on the specified line number, prints the line and quits that invocation of sed. The location of the filename should be in one of the immediate subdirectories of the current directory.



    All of the output is then redirected to output.txt.






    share|improve this answer






















    • Hi , Is there a way we can tell the script to select last occurrence of underscore for line Number instead of 3rd occurrence because filename may contain more than 3 underscore.
      – shubham deodia
      Jan 23 at 19:09











    • Not this script, because I based it on your sample input. You should really post representative data in your question in order to get usable answers.
      – Jeff Schaller
      Jan 23 at 19:15











    • Yes, you are right, this script works perfectly with 3 underscore. Thank you.
      – shubham deodia
      Jan 23 at 19:18

















    up vote
    0
    down vote













    Complex solution with GNU parallel + find + awk:



    Let's say each input file has a content similar to the following:



    cat DIR1/FILE1.DAT_20180123_4
    FILE1 a
    FILE1 b
    FILE1 c
    FILE1 d
    FILE1 e
    FILE1 f
    FILE1 g



    So, by the above scheme, the 2nd line in file FILE2.DAT_20180123_5 would be FILE2 b and the 7th line in file FILE4.DAT_20180123_7 - FILE4 g.
    Assume input.txt file is the same as in the question.



    The job:



    find . -type f -regextype posix-egrep -regex ".*/($(paste -s -d'|' input.txt))" 
    | parallel -j0 "awk -v n="=s/.*_//=" -v fn="/"
    'NR==n print fn,$0; exit ' " > output.txt


    The final output.txt contents:



    $ cat output.txt 
    FILE4.DAT_20180123_7 FILE4 g
    FILE3.DAT_20180123_6 FILE3 f
    FILE1.DAT_20180123_4 FILE1 d
    FILE2.DAT_20180123_5 FILE2 e





    share|improve this answer




















    • Hi , but the fileName itself has some underscore , to be specific 5 underscore.
      – shubham deodia
      Jan 23 at 19:35










    • @shubhamdeodia, elaborate your file name convention: should all the files be ended with .DAT only?
      – RomanPerekhrest
      Jan 23 at 19:40











    • Yes, they all end with .DAT , but I can't install GNU parallel in my Linux distro. Don't have the privileges.
      – shubham deodia
      Jan 23 at 19:41











    • @shubhamdeodia, the main problem is in your input.txt - it should have include full filenames not basenames. So reconsider your input.txt scheme and your life will be easier
      – RomanPerekhrest
      Jan 23 at 19:49











    • @shubhamdeodia Are the reasons covered on oletange.blogspot.dk/2013/04/why-not-install-gnu-parallel.html If not, please elaborate.
      – Ole Tange
      Jan 31 at 15:39










    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    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: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f419143%2ffind-and-print-particular-line-from-list-of-filename-and-line-number%23new-answer', 'question_page');

    );

    Post as a guest






























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    #!/bin/bash 

    do_one()
    # two args: $1=filename_no_dir $2=line_number
    # Find the single filename
    eval file=*"/$1"
    echo $1
    # $. == line number
    perl -ne 'chomp; $.=='"$2"' and print "LINE($_)n"' $file

    export -f do_one

    # Generate som test data
    parallel 'mkdir DIR; seq 100 110 >DIR/FILE_NAME_.DAT' ::: 1..4

    # Test input.txt
    cat <<EOF |
    FILE_NAME_1.DAT_20180123_4
    FILE_NAME_2.DAT_20180123_5
    FILE_NAME_3.DAT_20180123_6
    FILE_NAME_4.DAT_20180123_7
    EOF
    # Remove _YYYYMMDD.* to get filename, and .*_ to get line number
    parallel do_one '= s/_201ddddd.*// =' '= s/.*_// ='


    Output:



    FILE_NAME_1.DAT
    LINE(103)
    FILE_NAME_2.DAT
    LINE(104)
    FILE_NAME_3.DAT
    LINE(105)
    FILE_NAME_4.DAT
    LINE(106)





    share|improve this answer
























      up vote
      0
      down vote



      accepted










      #!/bin/bash 

      do_one()
      # two args: $1=filename_no_dir $2=line_number
      # Find the single filename
      eval file=*"/$1"
      echo $1
      # $. == line number
      perl -ne 'chomp; $.=='"$2"' and print "LINE($_)n"' $file

      export -f do_one

      # Generate som test data
      parallel 'mkdir DIR; seq 100 110 >DIR/FILE_NAME_.DAT' ::: 1..4

      # Test input.txt
      cat <<EOF |
      FILE_NAME_1.DAT_20180123_4
      FILE_NAME_2.DAT_20180123_5
      FILE_NAME_3.DAT_20180123_6
      FILE_NAME_4.DAT_20180123_7
      EOF
      # Remove _YYYYMMDD.* to get filename, and .*_ to get line number
      parallel do_one '= s/_201ddddd.*// =' '= s/.*_// ='


      Output:



      FILE_NAME_1.DAT
      LINE(103)
      FILE_NAME_2.DAT
      LINE(104)
      FILE_NAME_3.DAT
      LINE(105)
      FILE_NAME_4.DAT
      LINE(106)





      share|improve this answer






















        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        #!/bin/bash 

        do_one()
        # two args: $1=filename_no_dir $2=line_number
        # Find the single filename
        eval file=*"/$1"
        echo $1
        # $. == line number
        perl -ne 'chomp; $.=='"$2"' and print "LINE($_)n"' $file

        export -f do_one

        # Generate som test data
        parallel 'mkdir DIR; seq 100 110 >DIR/FILE_NAME_.DAT' ::: 1..4

        # Test input.txt
        cat <<EOF |
        FILE_NAME_1.DAT_20180123_4
        FILE_NAME_2.DAT_20180123_5
        FILE_NAME_3.DAT_20180123_6
        FILE_NAME_4.DAT_20180123_7
        EOF
        # Remove _YYYYMMDD.* to get filename, and .*_ to get line number
        parallel do_one '= s/_201ddddd.*// =' '= s/.*_// ='


        Output:



        FILE_NAME_1.DAT
        LINE(103)
        FILE_NAME_2.DAT
        LINE(104)
        FILE_NAME_3.DAT
        LINE(105)
        FILE_NAME_4.DAT
        LINE(106)





        share|improve this answer












        #!/bin/bash 

        do_one()
        # two args: $1=filename_no_dir $2=line_number
        # Find the single filename
        eval file=*"/$1"
        echo $1
        # $. == line number
        perl -ne 'chomp; $.=='"$2"' and print "LINE($_)n"' $file

        export -f do_one

        # Generate som test data
        parallel 'mkdir DIR; seq 100 110 >DIR/FILE_NAME_.DAT' ::: 1..4

        # Test input.txt
        cat <<EOF |
        FILE_NAME_1.DAT_20180123_4
        FILE_NAME_2.DAT_20180123_5
        FILE_NAME_3.DAT_20180123_6
        FILE_NAME_4.DAT_20180123_7
        EOF
        # Remove _YYYYMMDD.* to get filename, and .*_ to get line number
        parallel do_one '= s/_201ddddd.*// =' '= s/.*_// ='


        Output:



        FILE_NAME_1.DAT
        LINE(103)
        FILE_NAME_2.DAT
        LINE(104)
        FILE_NAME_3.DAT
        LINE(105)
        FILE_NAME_4.DAT
        LINE(106)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 31 at 15:55









        Ole Tange

        11.3k1344101




        11.3k1344101






















            up vote
            0
            down vote













            :> awk -F_ ' print $1; print $3; ' inputfile
            FILE1.DAT
            4
            FILE2.DAT
            5
            FILE3.DAT
            6
            FILE4.DAT
            7





            share|improve this answer




















            • what if FILE2.DAT is in nested directory from where inputfile.txt is. I mean we don't know the relative path for FILE2.DAT from inputfile and we don't even have the exact name of subdirectory.
              – shubham deodia
              Jan 23 at 17:57











            • @shubhamdeodia Are you complaining to me about your input data...?
              – Hauke Laging
              Jan 23 at 18:02










            • I am sorry , my question was a little confusing, I have corrected it. I want to find the FILENAME in input.txt and then find line number in FILENAME and print it to putput.txt
              – shubham deodia
              Jan 23 at 18:08














            up vote
            0
            down vote













            :> awk -F_ ' print $1; print $3; ' inputfile
            FILE1.DAT
            4
            FILE2.DAT
            5
            FILE3.DAT
            6
            FILE4.DAT
            7





            share|improve this answer




















            • what if FILE2.DAT is in nested directory from where inputfile.txt is. I mean we don't know the relative path for FILE2.DAT from inputfile and we don't even have the exact name of subdirectory.
              – shubham deodia
              Jan 23 at 17:57











            • @shubhamdeodia Are you complaining to me about your input data...?
              – Hauke Laging
              Jan 23 at 18:02










            • I am sorry , my question was a little confusing, I have corrected it. I want to find the FILENAME in input.txt and then find line number in FILENAME and print it to putput.txt
              – shubham deodia
              Jan 23 at 18:08












            up vote
            0
            down vote










            up vote
            0
            down vote









            :> awk -F_ ' print $1; print $3; ' inputfile
            FILE1.DAT
            4
            FILE2.DAT
            5
            FILE3.DAT
            6
            FILE4.DAT
            7





            share|improve this answer












            :> awk -F_ ' print $1; print $3; ' inputfile
            FILE1.DAT
            4
            FILE2.DAT
            5
            FILE3.DAT
            6
            FILE4.DAT
            7






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 23 at 17:51









            Hauke Laging

            53.4k1282130




            53.4k1282130











            • what if FILE2.DAT is in nested directory from where inputfile.txt is. I mean we don't know the relative path for FILE2.DAT from inputfile and we don't even have the exact name of subdirectory.
              – shubham deodia
              Jan 23 at 17:57











            • @shubhamdeodia Are you complaining to me about your input data...?
              – Hauke Laging
              Jan 23 at 18:02










            • I am sorry , my question was a little confusing, I have corrected it. I want to find the FILENAME in input.txt and then find line number in FILENAME and print it to putput.txt
              – shubham deodia
              Jan 23 at 18:08
















            • what if FILE2.DAT is in nested directory from where inputfile.txt is. I mean we don't know the relative path for FILE2.DAT from inputfile and we don't even have the exact name of subdirectory.
              – shubham deodia
              Jan 23 at 17:57











            • @shubhamdeodia Are you complaining to me about your input data...?
              – Hauke Laging
              Jan 23 at 18:02










            • I am sorry , my question was a little confusing, I have corrected it. I want to find the FILENAME in input.txt and then find line number in FILENAME and print it to putput.txt
              – shubham deodia
              Jan 23 at 18:08















            what if FILE2.DAT is in nested directory from where inputfile.txt is. I mean we don't know the relative path for FILE2.DAT from inputfile and we don't even have the exact name of subdirectory.
            – shubham deodia
            Jan 23 at 17:57





            what if FILE2.DAT is in nested directory from where inputfile.txt is. I mean we don't know the relative path for FILE2.DAT from inputfile and we don't even have the exact name of subdirectory.
            – shubham deodia
            Jan 23 at 17:57













            @shubhamdeodia Are you complaining to me about your input data...?
            – Hauke Laging
            Jan 23 at 18:02




            @shubhamdeodia Are you complaining to me about your input data...?
            – Hauke Laging
            Jan 23 at 18:02












            I am sorry , my question was a little confusing, I have corrected it. I want to find the FILENAME in input.txt and then find line number in FILENAME and print it to putput.txt
            – shubham deodia
            Jan 23 at 18:08




            I am sorry , my question was a little confusing, I have corrected it. I want to find the FILENAME in input.txt and then find line number in FILENAME and print it to putput.txt
            – shubham deodia
            Jan 23 at 18:08










            up vote
            0
            down vote













            If I'm understanding you correctly,



            while IFS=_ read -r filename unuseddate linenum
            do
            printf "%sn" "$filename"
            sed -n "$linenump;q" */"$filename"
            done < input.txt > output.txt


            This reads one line at a time from input.txt, splitting the line into 3 parts based on the underscore. It prints the filename then fires off a sed command that (by default prints nothing) and then on the specified line number, prints the line and quits that invocation of sed. The location of the filename should be in one of the immediate subdirectories of the current directory.



            All of the output is then redirected to output.txt.






            share|improve this answer






















            • Hi , Is there a way we can tell the script to select last occurrence of underscore for line Number instead of 3rd occurrence because filename may contain more than 3 underscore.
              – shubham deodia
              Jan 23 at 19:09











            • Not this script, because I based it on your sample input. You should really post representative data in your question in order to get usable answers.
              – Jeff Schaller
              Jan 23 at 19:15











            • Yes, you are right, this script works perfectly with 3 underscore. Thank you.
              – shubham deodia
              Jan 23 at 19:18














            up vote
            0
            down vote













            If I'm understanding you correctly,



            while IFS=_ read -r filename unuseddate linenum
            do
            printf "%sn" "$filename"
            sed -n "$linenump;q" */"$filename"
            done < input.txt > output.txt


            This reads one line at a time from input.txt, splitting the line into 3 parts based on the underscore. It prints the filename then fires off a sed command that (by default prints nothing) and then on the specified line number, prints the line and quits that invocation of sed. The location of the filename should be in one of the immediate subdirectories of the current directory.



            All of the output is then redirected to output.txt.






            share|improve this answer






















            • Hi , Is there a way we can tell the script to select last occurrence of underscore for line Number instead of 3rd occurrence because filename may contain more than 3 underscore.
              – shubham deodia
              Jan 23 at 19:09











            • Not this script, because I based it on your sample input. You should really post representative data in your question in order to get usable answers.
              – Jeff Schaller
              Jan 23 at 19:15











            • Yes, you are right, this script works perfectly with 3 underscore. Thank you.
              – shubham deodia
              Jan 23 at 19:18












            up vote
            0
            down vote










            up vote
            0
            down vote









            If I'm understanding you correctly,



            while IFS=_ read -r filename unuseddate linenum
            do
            printf "%sn" "$filename"
            sed -n "$linenump;q" */"$filename"
            done < input.txt > output.txt


            This reads one line at a time from input.txt, splitting the line into 3 parts based on the underscore. It prints the filename then fires off a sed command that (by default prints nothing) and then on the specified line number, prints the line and quits that invocation of sed. The location of the filename should be in one of the immediate subdirectories of the current directory.



            All of the output is then redirected to output.txt.






            share|improve this answer














            If I'm understanding you correctly,



            while IFS=_ read -r filename unuseddate linenum
            do
            printf "%sn" "$filename"
            sed -n "$linenump;q" */"$filename"
            done < input.txt > output.txt


            This reads one line at a time from input.txt, splitting the line into 3 parts based on the underscore. It prints the filename then fires off a sed command that (by default prints nothing) and then on the specified line number, prints the line and quits that invocation of sed. The location of the filename should be in one of the immediate subdirectories of the current directory.



            All of the output is then redirected to output.txt.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 23 at 18:52

























            answered Jan 23 at 17:54









            Jeff Schaller

            31.7k847107




            31.7k847107











            • Hi , Is there a way we can tell the script to select last occurrence of underscore for line Number instead of 3rd occurrence because filename may contain more than 3 underscore.
              – shubham deodia
              Jan 23 at 19:09











            • Not this script, because I based it on your sample input. You should really post representative data in your question in order to get usable answers.
              – Jeff Schaller
              Jan 23 at 19:15











            • Yes, you are right, this script works perfectly with 3 underscore. Thank you.
              – shubham deodia
              Jan 23 at 19:18
















            • Hi , Is there a way we can tell the script to select last occurrence of underscore for line Number instead of 3rd occurrence because filename may contain more than 3 underscore.
              – shubham deodia
              Jan 23 at 19:09











            • Not this script, because I based it on your sample input. You should really post representative data in your question in order to get usable answers.
              – Jeff Schaller
              Jan 23 at 19:15











            • Yes, you are right, this script works perfectly with 3 underscore. Thank you.
              – shubham deodia
              Jan 23 at 19:18















            Hi , Is there a way we can tell the script to select last occurrence of underscore for line Number instead of 3rd occurrence because filename may contain more than 3 underscore.
            – shubham deodia
            Jan 23 at 19:09





            Hi , Is there a way we can tell the script to select last occurrence of underscore for line Number instead of 3rd occurrence because filename may contain more than 3 underscore.
            – shubham deodia
            Jan 23 at 19:09













            Not this script, because I based it on your sample input. You should really post representative data in your question in order to get usable answers.
            – Jeff Schaller
            Jan 23 at 19:15





            Not this script, because I based it on your sample input. You should really post representative data in your question in order to get usable answers.
            – Jeff Schaller
            Jan 23 at 19:15













            Yes, you are right, this script works perfectly with 3 underscore. Thank you.
            – shubham deodia
            Jan 23 at 19:18




            Yes, you are right, this script works perfectly with 3 underscore. Thank you.
            – shubham deodia
            Jan 23 at 19:18










            up vote
            0
            down vote













            Complex solution with GNU parallel + find + awk:



            Let's say each input file has a content similar to the following:



            cat DIR1/FILE1.DAT_20180123_4
            FILE1 a
            FILE1 b
            FILE1 c
            FILE1 d
            FILE1 e
            FILE1 f
            FILE1 g



            So, by the above scheme, the 2nd line in file FILE2.DAT_20180123_5 would be FILE2 b and the 7th line in file FILE4.DAT_20180123_7 - FILE4 g.
            Assume input.txt file is the same as in the question.



            The job:



            find . -type f -regextype posix-egrep -regex ".*/($(paste -s -d'|' input.txt))" 
            | parallel -j0 "awk -v n="=s/.*_//=" -v fn="/"
            'NR==n print fn,$0; exit ' " > output.txt


            The final output.txt contents:



            $ cat output.txt 
            FILE4.DAT_20180123_7 FILE4 g
            FILE3.DAT_20180123_6 FILE3 f
            FILE1.DAT_20180123_4 FILE1 d
            FILE2.DAT_20180123_5 FILE2 e





            share|improve this answer




















            • Hi , but the fileName itself has some underscore , to be specific 5 underscore.
              – shubham deodia
              Jan 23 at 19:35










            • @shubhamdeodia, elaborate your file name convention: should all the files be ended with .DAT only?
              – RomanPerekhrest
              Jan 23 at 19:40











            • Yes, they all end with .DAT , but I can't install GNU parallel in my Linux distro. Don't have the privileges.
              – shubham deodia
              Jan 23 at 19:41











            • @shubhamdeodia, the main problem is in your input.txt - it should have include full filenames not basenames. So reconsider your input.txt scheme and your life will be easier
              – RomanPerekhrest
              Jan 23 at 19:49











            • @shubhamdeodia Are the reasons covered on oletange.blogspot.dk/2013/04/why-not-install-gnu-parallel.html If not, please elaborate.
              – Ole Tange
              Jan 31 at 15:39














            up vote
            0
            down vote













            Complex solution with GNU parallel + find + awk:



            Let's say each input file has a content similar to the following:



            cat DIR1/FILE1.DAT_20180123_4
            FILE1 a
            FILE1 b
            FILE1 c
            FILE1 d
            FILE1 e
            FILE1 f
            FILE1 g



            So, by the above scheme, the 2nd line in file FILE2.DAT_20180123_5 would be FILE2 b and the 7th line in file FILE4.DAT_20180123_7 - FILE4 g.
            Assume input.txt file is the same as in the question.



            The job:



            find . -type f -regextype posix-egrep -regex ".*/($(paste -s -d'|' input.txt))" 
            | parallel -j0 "awk -v n="=s/.*_//=" -v fn="/"
            'NR==n print fn,$0; exit ' " > output.txt


            The final output.txt contents:



            $ cat output.txt 
            FILE4.DAT_20180123_7 FILE4 g
            FILE3.DAT_20180123_6 FILE3 f
            FILE1.DAT_20180123_4 FILE1 d
            FILE2.DAT_20180123_5 FILE2 e





            share|improve this answer




















            • Hi , but the fileName itself has some underscore , to be specific 5 underscore.
              – shubham deodia
              Jan 23 at 19:35










            • @shubhamdeodia, elaborate your file name convention: should all the files be ended with .DAT only?
              – RomanPerekhrest
              Jan 23 at 19:40











            • Yes, they all end with .DAT , but I can't install GNU parallel in my Linux distro. Don't have the privileges.
              – shubham deodia
              Jan 23 at 19:41











            • @shubhamdeodia, the main problem is in your input.txt - it should have include full filenames not basenames. So reconsider your input.txt scheme and your life will be easier
              – RomanPerekhrest
              Jan 23 at 19:49











            • @shubhamdeodia Are the reasons covered on oletange.blogspot.dk/2013/04/why-not-install-gnu-parallel.html If not, please elaborate.
              – Ole Tange
              Jan 31 at 15:39












            up vote
            0
            down vote










            up vote
            0
            down vote









            Complex solution with GNU parallel + find + awk:



            Let's say each input file has a content similar to the following:



            cat DIR1/FILE1.DAT_20180123_4
            FILE1 a
            FILE1 b
            FILE1 c
            FILE1 d
            FILE1 e
            FILE1 f
            FILE1 g



            So, by the above scheme, the 2nd line in file FILE2.DAT_20180123_5 would be FILE2 b and the 7th line in file FILE4.DAT_20180123_7 - FILE4 g.
            Assume input.txt file is the same as in the question.



            The job:



            find . -type f -regextype posix-egrep -regex ".*/($(paste -s -d'|' input.txt))" 
            | parallel -j0 "awk -v n="=s/.*_//=" -v fn="/"
            'NR==n print fn,$0; exit ' " > output.txt


            The final output.txt contents:



            $ cat output.txt 
            FILE4.DAT_20180123_7 FILE4 g
            FILE3.DAT_20180123_6 FILE3 f
            FILE1.DAT_20180123_4 FILE1 d
            FILE2.DAT_20180123_5 FILE2 e





            share|improve this answer












            Complex solution with GNU parallel + find + awk:



            Let's say each input file has a content similar to the following:



            cat DIR1/FILE1.DAT_20180123_4
            FILE1 a
            FILE1 b
            FILE1 c
            FILE1 d
            FILE1 e
            FILE1 f
            FILE1 g



            So, by the above scheme, the 2nd line in file FILE2.DAT_20180123_5 would be FILE2 b and the 7th line in file FILE4.DAT_20180123_7 - FILE4 g.
            Assume input.txt file is the same as in the question.



            The job:



            find . -type f -regextype posix-egrep -regex ".*/($(paste -s -d'|' input.txt))" 
            | parallel -j0 "awk -v n="=s/.*_//=" -v fn="/"
            'NR==n print fn,$0; exit ' " > output.txt


            The final output.txt contents:



            $ cat output.txt 
            FILE4.DAT_20180123_7 FILE4 g
            FILE3.DAT_20180123_6 FILE3 f
            FILE1.DAT_20180123_4 FILE1 d
            FILE2.DAT_20180123_5 FILE2 e






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 23 at 19:32









            RomanPerekhrest

            22.4k12144




            22.4k12144











            • Hi , but the fileName itself has some underscore , to be specific 5 underscore.
              – shubham deodia
              Jan 23 at 19:35










            • @shubhamdeodia, elaborate your file name convention: should all the files be ended with .DAT only?
              – RomanPerekhrest
              Jan 23 at 19:40











            • Yes, they all end with .DAT , but I can't install GNU parallel in my Linux distro. Don't have the privileges.
              – shubham deodia
              Jan 23 at 19:41











            • @shubhamdeodia, the main problem is in your input.txt - it should have include full filenames not basenames. So reconsider your input.txt scheme and your life will be easier
              – RomanPerekhrest
              Jan 23 at 19:49











            • @shubhamdeodia Are the reasons covered on oletange.blogspot.dk/2013/04/why-not-install-gnu-parallel.html If not, please elaborate.
              – Ole Tange
              Jan 31 at 15:39
















            • Hi , but the fileName itself has some underscore , to be specific 5 underscore.
              – shubham deodia
              Jan 23 at 19:35










            • @shubhamdeodia, elaborate your file name convention: should all the files be ended with .DAT only?
              – RomanPerekhrest
              Jan 23 at 19:40











            • Yes, they all end with .DAT , but I can't install GNU parallel in my Linux distro. Don't have the privileges.
              – shubham deodia
              Jan 23 at 19:41











            • @shubhamdeodia, the main problem is in your input.txt - it should have include full filenames not basenames. So reconsider your input.txt scheme and your life will be easier
              – RomanPerekhrest
              Jan 23 at 19:49











            • @shubhamdeodia Are the reasons covered on oletange.blogspot.dk/2013/04/why-not-install-gnu-parallel.html If not, please elaborate.
              – Ole Tange
              Jan 31 at 15:39















            Hi , but the fileName itself has some underscore , to be specific 5 underscore.
            – shubham deodia
            Jan 23 at 19:35




            Hi , but the fileName itself has some underscore , to be specific 5 underscore.
            – shubham deodia
            Jan 23 at 19:35












            @shubhamdeodia, elaborate your file name convention: should all the files be ended with .DAT only?
            – RomanPerekhrest
            Jan 23 at 19:40





            @shubhamdeodia, elaborate your file name convention: should all the files be ended with .DAT only?
            – RomanPerekhrest
            Jan 23 at 19:40













            Yes, they all end with .DAT , but I can't install GNU parallel in my Linux distro. Don't have the privileges.
            – shubham deodia
            Jan 23 at 19:41





            Yes, they all end with .DAT , but I can't install GNU parallel in my Linux distro. Don't have the privileges.
            – shubham deodia
            Jan 23 at 19:41













            @shubhamdeodia, the main problem is in your input.txt - it should have include full filenames not basenames. So reconsider your input.txt scheme and your life will be easier
            – RomanPerekhrest
            Jan 23 at 19:49





            @shubhamdeodia, the main problem is in your input.txt - it should have include full filenames not basenames. So reconsider your input.txt scheme and your life will be easier
            – RomanPerekhrest
            Jan 23 at 19:49













            @shubhamdeodia Are the reasons covered on oletange.blogspot.dk/2013/04/why-not-install-gnu-parallel.html If not, please elaborate.
            – Ole Tange
            Jan 31 at 15:39




            @shubhamdeodia Are the reasons covered on oletange.blogspot.dk/2013/04/why-not-install-gnu-parallel.html If not, please elaborate.
            – Ole Tange
            Jan 31 at 15:39












             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f419143%2ffind-and-print-particular-line-from-list-of-filename-and-line-number%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?