Search string and line in files and subdirectories

Discussion in 'Programming/Scripts' started by jboud, Jul 26, 2019.

  1. jboud

    jboud Member

    Hello, I've been busting my head to create a script or search with > export to find 2 things in files in sub directories.
    There are hundreds of files in tenths of sub directories which I need to extract a thing that is in a fixed line and something that is always after a string.
    For example:
    line 1
    line 2
    line 3 <--- this (fixed position, always in line 3)
    followed by lines that can be different and all sorts
    Unique string <--- and I want this too
    so I want 2 lines exported from each file that are included in sub directories.

    I tried doing this in separate searches, with awk, grep, sid and sometimes in combination with find, producing 2 different files that I hoped to combine when imported in a spreadsheet, but unfortunately all my results are not in a similar sorting order so I could match them. Is it possible to do this in one command or even a script or even 2 scripts that are in the same sorting order?

    For example I have succeeded in doing:
    grep -d recurse "string" * > /somewhere/strings.txt
    to get all unique strings and
    find . -type f | awk 'FNR == 12' **/* >/somewhere/fixedpositionedstrings.txt
    but when opening the files they are not in order, it's like the commands are reading the files in different order.
    The grep command is also typing the file it got the result from, if I could do it with awk that would also help me to work around it with sorting through a spreadsheet.

    Any help is appreciated.
     
  2. Taleman

    Taleman Well-Known Member HowtoForge Supporter

    That program specification is vague.
    That unique sting, is it known in advance and the same in all files? If this is the case, why read it from the files?
    They probably are in alphabetic order or the order they were read. You do not specify what order they should appear in.
    This shows the third line in all files:
    Code:
    find . -type f -exec thirdonly.sh '{}' \; 
    Notice that I use the CODE tags to keep command line or program listing readable.
    I created that thirdonly.sh script to my ~/bin directory, it is like this:
    Code:
    #!/bin/bash
    cat "$1" | head -3 | tail -1
    My test setup has these files:
    Code:
    $ ls -lhR
    .:
    yhteensä 16K
    drwxr-xr-x 2 tale tale 4,0K heinä 27 09:51 subdir
    -rw-r--r-- 1 tale tale   85 heinä 27 09:28 test1.txt
    -rw-r--r-- 1 tale tale   85 heinä 27 09:28 test2.txt
    -rw-r--r-- 1 tale tale   85 heinä 27 09:28 test3.txt
    
    ./subdir:
    yhteensä 4,0K
    -rw-r--r-- 1 tale tale 85 heinä 27 09:27 test4.txt
    
    This is file test1.txt as an example:
    Code:
    Line 1 Test1.txt
    Line 2 Test1.txt
    Line 3 Test1.txt
    Line 4 Test1.txt
    Line 5 Test1.txt
    
    Example run:
    Code:
    $ find . -type f -exec thirdonly.sh '{}' \;
    Line 3 Test4.txt
    Line 3 Test3.txt
    Line 3 Test2.txt
    Line 3 Test1.txt
    
     
  3. jboud

    jboud Member

    Hello, thanks for the reply, I will test this when I go home. To clarify about the unique string, the line has always a unique string, something like "number: 345345". The string "number" appears only once in each file, but as you understand now, I need the whole line just because of the numbers that follow.
    Also about the sorting, I don't care how they are sorted as long as they a sorted in the exact same way, so I can line them up in a spreadsheet.
    Thanks again.
     
  4. Taleman

    Taleman Well-Known Member HowtoForge Supporter

    I am guessing here, but do you want a printout that has
    1. filename
    2. 3rd line from that file
    3. line with string "number" from that file?
    Run the output through command sort, then the are in the same order in subsequent runs.
     
  5. jboud

    jboud Member

    If 3rd line and the line with the string and number are next to each other there is no need for the filename. To explain in more detail what these files are, the fixed line is a persons name, then some lines follows with things they did, could be 1 could be more and after those lines there are points the person gained. So fetching the name and the points is my goal.
    Thank you once again.
     
  6. jboud

    jboud Member

    With your example I get different results. Weird, I'm not getting the filenames, only the Line 3
    Code:
    $ find . -type f -exec thirdonly.sh '{}' \;
    Line 3
    Line 3
    Line 3
    Line 3
    
    Maybe it has to do something with my system, it's fresh Debian Buster.
     
  7. Taleman

    Taleman Well-Known Member HowtoForge Supporter

    I believe it has to do with the contents of the test files you used.
     

Share This Page