Search directory for string and print file name

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Search directory for string and print file name
# 1  
Old 05-17-2017
Search directory for string and print file name

Good Evening Folks -

I have a need to search a specific directory and ALL sub-directories for the following string:

Code:
Data Load Updated

If this string is found, I need to print content from the line directory above it between [] symbols, as well as the file name where it is found.

Here is a sample file content:

Code:
[Tue Mar 07 15:08:31 2017]Local/GENJBAPP/Fin_Plan/admin@Native Directory/209240/Info(1013166)
Received Command [Import] from user [admin@Native Directory] using [FCSTCD.rul]

[Tue Mar 07 15:08:31 2017]Local/GENJBAPP/Fin_Plan/admin@Native Directory/209240/Info(1003040)
Parallel dataload enabled: [1] block prepare threads, [1] block write threads.

[Tue Mar 07 15:08:31 2017]Local/GENJBAPP/Fin_Plan/admin@Native Directory/209240/Info(1003037)
Data Load Updated [648] cells

[Tue Mar 07 15:08:31 2017]Local/GENJBAPP/Fin_Plan/admin@Native Directory/209240/Info(1003053)
Data Load Elapsed Time with [FCSTCD.rul] : [0.011] seconds

[Tue Mar 07 15:08:31 2017]Local/GENJBAPP///175440/Info(1013210)
User [admin@Native Directory] set active on database [Fin_Plan]

[Tue Mar 07 15:08:31 2017]Local/GENJBAPP///176308/Info(1042059)
Connected from [::1]

[Tue Mar 07 15:08:31 2017]Local/GENJBAPP/Fin_Plan/admin@Native Directory/176308/Info(1013091)
Received Command [Calculate] from user [admin@Native Directory]

Therefore, if this string is found in 'Application1.log' for instance, the results file should contain:

Code:
Application1.log
Tue Mar 07 15:08:31 2017

Is this possible?

Thank you!

---------- Post updated at 07:41 PM ---------- Previous update was at 06:36 PM ----------

I came up with this solution and it seems to be working fine:

Code:
find /home/oracle/Hyperion_Batch/Scripts/Batch -type f | xargs awk '{A[NR]=$0}/Data Load Updated/{print A[NR-1]}'>>output.txt

If this is not the correct approach, please let me know.

Last edited by SIMMS7400; 05-17-2017 at 07:48 PM..
# 2  
Old 05-18-2017
How about
Code:
grep -rB1 "Data Load Elapsed" file[12] | grep -v "Data Load Elapsed"
file1-[Tue Mar 07 15:08:31 2017]Local/GENJBAPP/Fin_Plan/admin@Native Directory/209240/Info(1003053)
--
file2-[Tue Mar 07 15:08:31 2017]Local/GENJBAPP/Fin_Plan/admin@Native Directory/209240/Info(1003053)

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search strings from a file in files in a directory recursively; then print the string with a status

Hi All, I hope somebody would be able to help me. I would need to search a string coming from a file, example file.txt: dog cat goat horse fish For every string, I would need to know if there are any files inside a directory(recursively) that contains the string regardless of case.... (9 Replies)
Discussion started by: kokoro
9 Replies

2. Shell Programming and Scripting

Script to search every file in a directory and print last few lines

Hi everyone, I need to write a script to search a directory, output the name of a file to an ouput file and print the last few lines of the files to the output file such that I would have something like this: FILE1: LINE LINE LINE FILE2: LINE LINE LINE FILE3: LINE LINE LINE... (2 Replies)
Discussion started by: mojoman
2 Replies

3. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

4. Shell Programming and Scripting

Search string and print the above line and below lines?.

if the first string matches then print the previous line and current line and also print the following lines if the other string search matches. Input ------ TranTime 2012 10 12 The Record starts here Accountnumber: 4632473431274 TxnCode 323 TranID 329473242834 ccsdkcnsdncskd... (7 Replies)
Discussion started by: laknar
7 Replies

5. Shell Programming and Scripting

search-word-print-specific-string

Hi, Our input xml looks like: <doc> <str name="account_id">1111</str> <str name="prd_id">DHEP155EK</str> </doc> - <doc> <str name="account_id">6666</str> <str name="prd_id">394531662</str> </doc> - <doc> <str name="account_id">6666</str> <str... (1 Reply)
Discussion started by: Jassz
1 Replies

6. Shell Programming and Scripting

search a word and print specific string using awk

Hi, I have list of directory paths in a variable and i want to delete those dirs and if dir does not exist then search that string and get the correct path from xml file after that delete the correct directory. i tried to use grep and it prints the entire line from the search.once i get the entire... (7 Replies)
Discussion started by: dragon.1431
7 Replies

7. Shell Programming and Scripting

Print #of lines after search string in a big file

I have a command which prints #lines after and before the search string in the huge file nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r;print;c=a}b{r=$0}' b=0 a=10 s="STRING1" FILE The file is 5 gig big. It works great and prints 10 lines after the lines which contains search string in... (8 Replies)
Discussion started by: prash184u
8 Replies

8. Shell Programming and Scripting

awk print second line after search string

I have multiple config files where I need to pull the ip address from loopback3. The format is the same in every file, the ip is the second line after interface loopback3. interface loopback2 loopback description router ID ip address 192.168.1.1 interface loopback3 loopback description... (3 Replies)
Discussion started by: numele
3 Replies

9. Shell Programming and Scripting

Search a string and print the rest of line

Hi Guys, I need to search a string and print the rest of the lines... input: 8 0 90 1 0 59 20 2488 96 30006dde372 S ? 0:00 /etc/opt/SUNWconn/atm/bin/atmsnmpd -n output: 00 /etc/opt/SUNWconn/atm/bin/atmsnmpd -n Actually i don even need the first "00".. any suggestions is appreciated..... (13 Replies)
Discussion started by: mac4rfree
13 Replies

10. UNIX for Dummies Questions & Answers

Unix find command to print directory and search string

Hi i need to print pathname in which the string present using 'find' command sample output like this Pathname String to be searched ---------- -------------------- /usr/test/myfile get /opt/test/somefile get Thanks in... (4 Replies)
Discussion started by: princein
4 Replies
Login or Register to Ask a Question