Sponsored Content
Top Forums Shell Programming and Scripting Display file date after grepping a string in the file Post 302645385 by neutronscott on Wednesday 23rd of May 2012 10:07:56 AM
Old 05-23-2012
put this in a file called recent_grep and "source recent_grep", then you can use the command

Code:
recent_grep() {
  (
    shopt -s nullglob
    # search only recent directories
    find . -type d -mtime -1 -print0 | while IFS= read -rd '' dir; do
      files=( "$dir"/*.csv???????? )
      # skip empty directory
      (( ${#files[@]} )) || continue
      for file in "${files[@]}"; do
        date=${file##*.csv}
        grep "$@" "$file" | while read -r line; do
          #prefix grep output with date
          printf '%s\t%s\t%s\n' "$date" "$dir" "$line"
        done
      done
    done | sort -r
  )
}

i re-created the data from your pastes. here is output.

Code:
[mute@geek ~/temp/quemalr]$ source recent_grep
[mute@geek ~/temp/quemalr]$ recent_grep 0:15:a3:e2:f8:c7
12022122        ./modemdata/Knock1      80:192.168.12.127,0:15:a3:e2:f8:c7,256000000,31400000,-66,241,340,469848408,19876103,10,469925662,19876851,10
12022022        ./modemdata/Knock1      68:192.168.12.127,0:15:a3:e2:f8:c7,256000000,31400000,-63,229,340,3648043108,13423757,10,3648043108,13423757,10
12021922        ./modemdata/Knock1      62:192.168.12.127,0:15:a3:e2:f8:c7,256000000,31400000,-48,256,330,2530162130,7754073,10,2530162130,7754073,10
12021822        ./modemdata/Knock1      70:192.168.12.127,0:15:a3:e2:f8:c7,256000000,31400000,-45,259,320,1411569669,3419626,0,1411569669,3419626,0
12021722        ./modemdata/Knock1      80:192.168.12.127,0:15:a3:e2:f8:c7,256000000,31400000,-59,232,330,290816964,1040554,0,290816964,1040554,0
12021622        ./modemdata/Knock1      8:192.168.12.17,0:15:a3:e2:f8:c7,256000000,31400000,-64,246,340,3148191623,22637172,678615,3148191623,22637172,678615
12021522        ./modemdata/Knock1      9:192.168.12.17,0:15:a3:e2:f8:c7,256000000,31400000,-57,248,330,2039658009,8482050,360918,2039734114,8483880,360987
12021422        ./modemdata/Knock1      9:192.168.12.17,0:15:a3:e2:f8:c7,256000000,31400000,-55,256,330,925261982,11,0,925339984,11,0
12021322        ./modemdata/Knock1      8:192.168.12.17,0:15:a3:e2:f8:c7,256000000,31400000,-51,258,330,4097068359,10,0,4097068359,10,0
12021222        ./modemdata/Knock1      8:192.168.12.17,0:15:a3:e2:f8:c7,256000000,31400000,-60,252,340,2974019233,10,0,2974019233,10,0
12021122        ./modemdata/Knock1      7:192.168.12.17,0:15:a3:e2:f8:c7,256000000,31400000,-53,252,330,1850779668,8,0,1850857670,8,0
12021022        ./modemdata/Knock1      6:192.168.12.17,0:15:a3:e2:f8:c7,256000000,31400000,-62,250,340,727539103,8,0,727617105,8,0
12020922        ./modemdata/Knock1      5:192.168.12.17,0:15:a3:e2:f8:c7,256000000,31400000,-54,260,340,3899376153,8,0,3899454155,8,0
12020822        ./modemdata/Knock1      7:192.168.12.17,0:15:a3:e2:f8:c7,256000000,31400000,-60,250,340,2776416010,7,0,2776494013,7,0
12020722        ./modemdata/Knock1      7:192.168.12.17,0:15:a3:e2:f8:c7,256000000,31400000,-59,253,330,1653310971,5,0,1653388974,5,0
12020622        ./modemdata/Knock1      8:192.168.12.17,0:15:a3:e2:f8:c7,256000000,31400000,-43,264,320,530476324,4,0,530554327,4,0
12020522        ./Knock7        227:192.168.95.75,0:15:a3:e2:f8:c7,274000000,18600000,-23,266,330,,41602,0,704484102,41602,0
12020422        ./Knock7        227:192.168.95.75,0:15:a3:e2:f8:c7,274000000,18600000,-9,273,320,2269039706,24149,0,2269229200,24149,0
12020321        ./Knock7        230:192.168.95.75,0:15:a3:e2:f8:c7,274000000,,,275,320,3756465866,13229,0,3756465866,13229,0
08092508        ./mish1 320:192.168.22.253,0:15:a3:e2:f8:c7,250000000,24000000,-30,262,340,2771687393,10,0,2771765396,10,0
08092408        ./mish1 325:192.168.22.253,0:15:a3:e2:f8:c7,250000000,24000000,-16,272,340,1650163261,3,0,1650241264,3,0
08092308        ./mish1 323:192.168.22.253,0:15:a3:e2:f8:c7,250000000,24000000,-16,269,340,526142531,3,0,526142531,3,0
08092008        ./mish1 326:192.168.22.253,0:15:a3:e2:f8:c7,250000000,24000000,-31,263,350,3912425699,6,0,3912425699,6,0
08091908        ./mish1 319:192.168.22.253,0:15:a3:e2:f8:c7,250000000,24000000,-33,255,350,2788951199,6,0,2789029202,6,0
08091808        ./mish1 324:192.168.22.253,0:15:a3:e2:f8:c7,250000000,24000000,-26,265,360,1664462990,6,0,1664540993,6,0
08091708        ./mish1 318:192.168.22.253,0:15:a3:e2:f8:c7,250000000,24000000,-18,268,360,543875389,2,0,543875389,2,0
08091614        ./mish1 322:192.168.22.253,0:15:a3:e2:f8:c7,250000000,24000000,-16,272,340,1542683488,10227,1034,1542683488,10227,1034

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

display the file with in the date range

Hi All, I want a shell script which can display the file with in the date range. For Example I have 15 files with the following format abc_01-01-2009.txt to abc_15-01-2009.txt. Now I want to have the files between 4th of jan to 12th files. How can I acheive this. Advance... (1 Reply)
Discussion started by: fareed_do
1 Replies

2. Shell Programming and Scripting

Grepping string from out file

Guys .. Need to pull this highlighted strings irrespective of line numbers & should be echoed . But these strings are from Outfile from different dir. In which way this can be grepped ?? Need an idea http-timeout 120 seconds persistent-timeout 180 seconds host-rewriting on ... (7 Replies)
Discussion started by: raghunsi
7 Replies

3. AIX

Help with looping a file and grepping a string

I have 2 files: fileA and fileB. content of fileA --------------- admin.teacher is in new york; admin.mason is in new york; admin.driver is in new york city; user.trucker is in hartford; admin.developer is in new york state; content of fileB ---------------- admin.teacher is in... (8 Replies)
Discussion started by: lowprofile
8 Replies

4. Shell Programming and Scripting

How to search for file and display by date

I like "ls -ltr". I would like to search for a file in a large directory recursively and and display all the candidates in reverse order. /usr/bin/find . -name \*.txt This works. How do I display the date and sort the file names by the date in reverse order for the entire directory... (1 Reply)
Discussion started by: siegfried
1 Replies

5. HP-UX

find command to display size and date of a file

Hi, The blow code does not yeild any output. find . -name "*.jar" -o -name "*.ksh" -o -name "*.properties" -name "*.war" -o -name "*.ear" -o -name "*.sh" -o -name "*.cfg" -exec ls -l {} \; I wish to print the filename filesize filedate in HP-UX. Can anyone help ? (9 Replies)
Discussion started by: mohtashims
9 Replies

6. Shell Programming and Scripting

Display lines of two date range from syslog file

Hi Guys, I want to display lines from Solaris syslog file but with 2 dates range. I have some similar solution (https://www.unix.com/shell-programming-scripting/39293-grep-log-file-between-2-dates-4.html) which works fine but as you know syslog has different date format (Jan 22) so this is not... (1 Reply)
Discussion started by: prashant2507198
1 Replies

7. Shell Programming and Scripting

Extract count of string in all files and display on date wise

Hi All, hope you all are doing well! I kindly ask you for shell scripting help, here is the description: I have huge number of files shown below on date wise, which contains different strings(numbers you can say) including 505001 and 602001. ... (14 Replies)
Discussion started by: VasuKukkapalli
14 Replies

8. AIX

How do I display a file's last modified date?

I'm using a script that I need to get a file's "last modified date" in a format like 01:51:14 PM. We are running on AIX 6.1.0.0. I can't seem to find the right command parameters. Help! (4 Replies)
Discussion started by: mattadams1983
4 Replies

9. UNIX for Beginners Questions & Answers

Search a string and display its location on the entire string and make a text file

I want to search a small string in a large string and find the locations of the string. For this I used grep "string" -ob <file name where the large string is stored>. Now this gives me the locations of that string. Now how do I store these locations in a text file. Please use CODE tags as... (7 Replies)
Discussion started by: ANKIT ROY
7 Replies

10. Shell Programming and Scripting

ksh Script, Reading A File, Grepping A File Contents In Another File

So I'm stumped. First... APOLOGIES... my work is offline in an office that has zero internet connectivity, as required by our client. If need be, I could print out my script attempts and retype them here. But on the off chance... here goes. I have a text file (file_source) of terms, each line... (3 Replies)
Discussion started by: Brusimm
3 Replies
All times are GMT -4. The time now is 07:50 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy