Help with pattern search and return


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with pattern search and return
# 1  
Old 07-24-2007
Help with pattern search and return

I would like to write a script which will read a file containing a list of filenames of the format as shown below :

/usr/local/packages/runcmdlinetool
/home/john.doe/sdfsdf/sdfsdfsd/sdfsdf/sdfsdfTemplates.xml
/usr/local/bin/gtar
/home/mark.williams/sdfsdf/sdfsdfsd/sdfsdf/abdccd/sdfsdfTemplates.xml
/home/joe.smith/abcdfcddvdvdvdTemplates.xml
/home/mpt/sdfsdfsdfsdBindings.xml
/home/mark.joe/sdfsd/sdfsdf/sdfsd/sdf/abcddfgdfTemplates.xml
/home/trd/test/bin/sfsdfdsfsdfsdfdsfBindings.xml
/home/test/qa/jdk/bin/packages/jar

There will be some lines in the above file with the file named <randompattern>Templates.xml OR <randompattern>Bindings.xml
The script should search for the word "Templates" or "Bindings" in each line and if present, should return the randompattern existing before these 2 words, which kinda serve as Template or Binding name

Here is my script :

for cfilename in `cat 1.txt | sort -u`
do
filename_w_ext=`basename $cfilename`
export filename_w_ext
filename=`echo $filename_w_ext | sed 's;\(.*\)\..*;\1;'`
export filename
templatename= `echo $filename | awk -FS="Templates" '{print $1}'`
bindingname= `echo $filename | awk -FS="Bindings" '{print $1}'`
echo "filename is : " $filename
echo "templatename is : " $templatename
done

But looks like I am not getting the Tempaltes or Bindings names extracted correctl, some syntax and semantics errors.

As always help is really appreciated.
# 2  
Old 07-24-2007
Code:
#!/bin/ksh
typeset -i mCnt
mStr='Templates|Bindings'
while read cfilename
do
  mCnt=`echo ${cfilename} | egrep -c "$mStr"`
  if [ $mCnt -eq 0 ]; then
    continue
  fi
  mFirstPart=`echo ${cfilename} | sed 's;\(.*/\).*;\1;'`
  filename_w_ext=`basename $cfilename`
  filename=`echo $filename_w_ext | sed 's;\(.*\)\..*;\1;'`
  echo "filename = " $filename
  echo "First = "$mFirstPart
done < input_file

# 3  
Old 07-24-2007
My bad, I think I didn't clarify the requirement correctly. Let me illustrate by specifiying an example output :

So the file looks like this

/usr/local/packages/runcmdlinetool
/home/john.doe/sdfsdf/sdfsdfsd/sdfsdf/sdfsdfTemplates.xml
/usr/local/bin/gtar
/home/mark.williams/sdfsdf/sdfsdfsd/sdfsdf/abdccd/sdfsdfTemplates.xml
/home/joe.smith/abcdfcddvdvdvdTemplates.xml
/home/mpt/sdfsdfsdfsdBindings.xml
/home/mark.joe/sdfsd/sdfsdf/sdfsd/sdf/abcddfgdfTemplates.xml
/home/trd/test/bin/sfsdfdsfsdfsdfdsfBindings.xml
/home/test/qa/jdk/bin/packages/jar

The output should ignore all files NOT containing the patterns Templates OR Bindings. But for any line containing either the pattern Templates or Bindings, it should return an output like :

filename is : sdfsdfTemplates
templatename is : sdfsdf

OR if the pattern binding is present then should look like

filename is : sfsdfdsfsdfsdfdsfBindings.xml
bindingname is : sfsdfdsfsdfsdfdsf

I hope that clarifies. Sorry about the verbose post!
# 4  
Old 07-24-2007
I have narrowed down the problem further. I will strip out the absolute directory path for the file and extract a patter like

sfsdfsdfsdTemplates.xml
abcdefgsdfdsBindings.xml

from the list of files.

From this input, how do I produce an output lile

sfsdfsdfsd
abcdefgsdfds

So basically I want to strip out the last part of the filename containing a pattern similar to Tempaltes.xml or Bindings.xml

I prefer awk or sed. Is there substring function in awk or sed which I can use?

thanks,
I
# 5  
Old 07-25-2007
Sed

Hope this helps you

Code:
sed -e 's/\(.*\)Templates\.xml/\1/g' -e 's/\(.*\)Bindings\.xml/\1/g' filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. Shell Programming and Scripting

Search pattern on logfile and search for day/dates and skip duplicate lines if any

Hi, I've written a script to search for an Oracle ORA- error on a log file, print that line and the .trc file associated with it as well as the dateline of when I assumed the error occured. In most it is the first dateline previous to the error. Unfortunately, this is not a fool proof script.... (2 Replies)
Discussion started by: newbie_01
2 Replies

3. Shell Programming and Scripting

How to use sed to search a particular pattern in a file backward after a pattern is matched.?

Hi, I have two files file1.txt and file2.txt. Please see the attachments. In file2.txt (which actually is a diff output between two versions of file1.txt.), I extract the pattern corresponding to 1172c1172. Now ,In file1.txt I have to search for this pattern 1172c1172 and if found, I have to... (9 Replies)
Discussion started by: saurabh kumar
9 Replies

4. Shell Programming and Scripting

Pattern match exclusive return pattern/variable

I have an application(Minecraft Server) that generates a logfile live. Using Crontab and screen I send a 'list' command every minute. Sample Log view: 2013-06-07 19:14:37 <Willrocksyea1> hello* 2013-06-07 19:14:41 <Gromden29> hey 2013-06-07 19:14:42 Gromden29 lost connection:... (1 Reply)
Discussion started by: gatekeeper258
1 Replies

5. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

6. Shell Programming and Scripting

bash: need to have egrep to return a text string if the search pattern has NOT been found

Hello all, after spending hours of searching the web I decided to create an account here. This is my first post and I hope one of the experts can help. I need to resolve a grep / sed / xargs / awk problem. My input file is just like this: ----------------------------------... (6 Replies)
Discussion started by: bash4ever
6 Replies

7. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

8. UNIX for Dummies Questions & Answers

Search specific pattern in file and return number of occurence

Hi I want to search for a specific pattern in file Say ABC;HELLO_UNIX_WORLD;PQR ABC;HELLO_UNIX_WORLD_IS_NOT_ENOUGH;XYZ ABC;HELLO_UNIX_FORUM;LMN Pattern to search is : "HELLO_UNIX_*****" and not "HELLO_UNIX_***_***_" I mean after "HELLO_UNIX" there can only be one word.In this case... (2 Replies)
Discussion started by: dashing201
2 Replies

9. Shell Programming and Scripting

Print a pattern between the xml tags based on a search pattern

Hi all, I am trying to extract the values ( text between the xml tags) based on the Order Number. here is the sample input <?xml version="1.0" encoding="UTF-8"?> <NJCustomer> <Header> <MessageIdentifier>Y504173382</MessageIdentifier> ... (13 Replies)
Discussion started by: oky
13 Replies

10. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies
Login or Register to Ask a Question