Visit Our UNIX and Linux User Community


Pattern matching and Printing Filename


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Pattern matching and Printing Filename
# 1  
Old 07-23-2009
Pattern matching and Printing Filename

Hi,

My requirement is to search for a paritcular string from a group of .gz files and to print the lines containing that string and the name of the files in which that string is present.

Daily 500 odd .gz files will be generated in a directory(directory name will be in the form of daymonthyear i.e 230709) in /data3/home path.
Means the mentioned path has many such directories with .gz files.

I used below command, it's printing the lines containing that string but not filename.


find /data3/home/ -type f -name 'TRCansLog*.gz' -exec gzcat *.gz {} \; | nawk '/xfvc010423465107/ {print $0"\t" FILENAME}'

Pls help me

If possible pls tell how to print the directory name in which the requried file and string present.


Thanks in advance
# 2  
Old 07-23-2009
Code:
find /data3/home/ -type f -name 'TRCansLog*.gz' |
while read filename 
do   
   gzcat $filename | nawk -v fname=$filename '/xfvc010423465107/ {print $0 "\t" fname}'
done

# 3  
Old 07-23-2009
Thank you very much JIM,

It's working.

Pls do one more favor to me, I want to run above one as script, pls tell me what changes to be done inorder to give pattern at the begining instead of giving at nawk command. i.e ./pattern_filename_script xfvc010423465107

Thanks in advance.
# 4  
Old 07-23-2009
Quote:
Originally Posted by krao
Thank you very much JIM,

It's working.

Pls do one more favor to me, I want to run above one as script, pls tell me what changes to be done inorder to give pattern at the begining instead of giving at nawk command. i.e ./pattern_filename_script xfvc010423465107

Thanks in advance.
You do something like the following:

Code:
SEARCH="$1"
find /data3/home/ -type f -name 'TRCansLog*.gz' |
while read filename 
do   
   gzcat $filename | nawk -v fname=$filename '/'$SEARCH'/ {print $0 "\t" fname}'
done

# 5  
Old 07-24-2009
Thanks a lot
Its working
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #188
Difficulty: Easy
Python is an interpreted, high-level, general-purpose programming language, created by Linus Torvalds and first released in 1991
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help on pattern matching and printing the same

Hi, I need to match for the pattern '.py' in my file and print the word which contains. For example: cat testfile a b 3 4.py 5 6 a b.py c.py 4 5 6 7 8 1.py 2.py 3 4 5 6 Expected output: 4.py b.py c.py 1.py 2.py (3 Replies)
Discussion started by: Sumanthsv
3 Replies

2. Shell Programming and Scripting

UNIX awk pattern matching and printing lines

I have the below plain text file where i have some result, in order to mail that result in html table format I have written the below script and its working well. cat result.txt Page 2015-01-01 2000 Colors 2015-02-01 3000 Landing 2015-03-02 4000 #!/bin/sh LOG=/tmp/maillog.txt... (1 Reply)
Discussion started by: close2jay
1 Replies

3. Shell Programming and Scripting

Find: filename in every subdirectory matching a pattern

Hi, I have multiple directories built in following manner /app/red/tmp /app/blue/upd /app/blue/tmp /app/green/tmp /app/red/upd /app/green/upd I have filenames having pattern ONE.XXX.dat TWO.ZZZ.dat and so on across the folders listed above My objective is to list all filenames of a... (4 Replies)
Discussion started by: wahi80
4 Replies

4. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

5. Shell Programming and Scripting

Pattern Matching and printing

Dear All, I have a log file like below 13:26:31 |152.22 13:27:31 |154.25 13:28:31 |154.78 13:29:31 |151.23 13:30:31 |145.63 13:31:31 |142.10 13:32:31 |145.45 where values will be there from 00:00 hrs to 23:59 hrs. I'm matching for last occurance of 23:59 and printing 1440 lines (grep... (4 Replies)
Discussion started by: Naga06
4 Replies

6. UNIX for Dummies Questions & Answers

Help with filename pattern matching

Hi, I have files in a directory with filenames that match three specific patterns: 1) *'.L2_LAC'* 2) *'.L2_LAC_OC'* 3) *'.L2_LAC_SST'* I would like to have an "ls" command that will list only files matching the first two patterns. However, if I type: ls *'.L2_LAC'* I will get files that... (2 Replies)
Discussion started by: msb65
2 Replies

7. UNIX for Advanced & Expert Users

fgrep - printing pattern and filename

Hi, I have a patternfile with following pattern cat dog cow pig Let's say I have thousand files file0001 file0002 file0003 . . . file1000 Each pattern can occur multiple times in multiple files. How can I search for pattern so the output of pattern and the filename is printed... (1 Reply)
Discussion started by: yoda9691
1 Replies

8. Shell Programming and Scripting

counting the lines matching a pattern, in between two pattern, and generate a tab

Hi all, I'm looking for some help. I have a file (very long) that is organized like below: >Cluster 0 0 283nt, >01_FRYJ6ZM12HMXZS... at +/99% 1 279nt, >01_FRYJ6ZM12HN12A... at +/99% 2 281nt, >01_FRYJ6ZM12HM4TS... at +/99% 3 283nt, >01_FRYJ6ZM12HM946... at +/99% 4 279nt,... (4 Replies)
Discussion started by: d.chauliac
4 Replies

9. Shell Programming and Scripting

comment/delete a particular pattern starting from second line of the matching pattern

Hi, I have file 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433... (4 Replies)
Discussion started by: imas
4 Replies

10. Shell Programming and Scripting

Filename and Pattern matching

Hiiiii every one, I am facing a problem while giving a file name which has space in it. The problem is ... I have to read a file where the set of input files are stored. Now getting that name I have to open the file and i have to extract the name of the user from where it is written like"by... (2 Replies)
Discussion started by: kheyali Mitra
2 Replies

Featured Tech Videos