Need Help Matching a Substring


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need Help Matching a Substring
# 1  
Old 02-22-2010
Need Help Matching a Substring

All:
I am having trouble with matching substrings, and could use some input. I have a list of files in the form /path/to/filename.ext stored in a text file (one file per line; was created with find) referenced by $TEMPFILE. I need to take each file in the list and search for any number of substrings in that line (i.e., the path includes "string1" or part of the filename includes "string2", etc.) My first attempt was slong the lines of:

Code:
PRIHIGHPTRN="/ind"
PRILOWPTRN="/net"
...
for file in `cat ${TEMPFILE}`
do
...
  nawk -v cfile="$file" -v phi="$PRIHIGHPTRN" -v plo="$PRILOWPTRN" ' {
    if (index(cfile, phi)) {
      print "    <priority>1.0</priority>"
    } else {
      if (index(cfile, plo)) {
        print "    <priority>0.1</priority>"
      } else {
        print "    <priority>0.5</priority>"
      }
    }
  } ' >> ${XMLTMPFILE}

done

With this approach (using awk's index() function), it's been an unending chain of 'fix one problem, create a new one' for two days, now. If there's something that doesn't use awk, I'd be ecstatic to know, otherwise, some help with the block above would be not be turned away. This is on Solaris 5.8. The problem with the code above is that the script hangs just before writing the <priority></priority> line (no errors).

TIA
RJL
# 2  
Old 02-22-2010
Code:
$
$ cat -n tempfile.txt
     1  /dir1/dir2/ind/dir3/file1.txt
     2  /dir1/dir2/net/dir3/file2.txt
     3  /dir1/dir2/dir3/dir4/file3.txt
$
$ awk '{printf "<priority>";if (/\/ind/){printf "1.0"} else if (/\/net/){printf "0.1"} else {printf "0.5"} print "</priority>"}' tempfile.txt
<priority>1.0</priority>
<priority>0.1</priority>
<priority>0.5</priority>
$
$

tyler_durden

---------- Post updated at 01:29 PM ---------- Previous update was at 01:13 PM ----------

Quote:
Originally Posted by rjlohman
... If there's something that doesn't use awk, I'd be ecstatic to know,...
Yes, there's always Perl...

Code:
$
$
$ cat -n tempfile.txt
     1  /dir1/dir2/ind/dir3/file1.txt
     2  /dir1/dir2/net/dir3/file2.txt
     3  /dir1/dir2/ind/dir3/net/file3.txt
     4  /dir1/dir2/net/dir3/ind/file4.txt
     5  /dir1/dir2/dir3/dir4/file5.txt
$
$ perl -lne 'print "<priority>", $_ =~ /\/ind/ ? "1.0" : ($_ =~ /\/net/ ? "0.1" : "0.5"), "</priority>"' tempfile.txt
<priority>1.0</priority>
<priority>0.1</priority>
<priority>1.0</priority>
<priority>1.0</priority>
<priority>0.5</priority>
$
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to update field using matching value in file1 and substring in field in file2

In the awk below I am trying to set/update the value of $14 in file2 in bold, using the matching NM_ in $12 or $9 in file2 with the NM_ in $2 of file1. The lengths of $9 and $12 can be variable but what is consistent is the start pattern will always be NM_ and the end pattern is always ;... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Remove lines matching a substring in a specific column

Dear group, I have following input text file: Brit 2016 11 18 12 00 10 1.485,00 EUR Brit 2016 11 18 12 00 10 142,64 EUR Brit 2016 11 18 12 00 10 19,80 EUR Brit 2016 11 18 12 00 10 545,00 EUR Brit 2016 11 18 12 00 10 6.450,00 EUR... (3 Replies)
Discussion started by: gfhsd
3 Replies

3. Shell Programming and Scripting

awk to combine all matching dates and remove non-matching

Using the awk below I am able to combine all the matching dates in $1, but I can not seem to remove the non-matching from the file. Thank you :). file 20161109104500.0+0000,x,5631 20161109104500.0+0000,y,2 20161109104500.0+0000,z,2 20161109104500.0+0000,a,4117... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

5. Shell Programming and Scripting

Insert lines above matching line with content from matching

Hi, I have text file: Name: xyz Gender: M Address: "120_B_C; ksilskdj; lsudlfw" Zip: 20392 Name: KLM Gender: F Address: "65_D_F; wnmlsi;lsuod;,...." Zip:90233I want to insert 2 new lines before the 'Address: ' line deriving value from this Address line value The Address value in quotes... (1 Reply)
Discussion started by: ysrini
1 Replies

6. Shell Programming and Scripting

Substring by matching a pattern

Hi, I have a string which is in the below format "/abc/123/xyz/HI_I_AM_THE_FILE_12122012123456.TXT" I want to extract the file name which is "HI_IAM_THE_FILE_12122012123456.TXT". the depth of the directory in which the file is sitting may vary. The file can sit in /abc/123/xyz or... (2 Replies)
Discussion started by: siddu_chittari
2 Replies

7. Shell Programming and Scripting

Matching the substring and join two files

Hi I had two files like below. file-1 101001234567890 101001234567891 101001234567892 101001234567893 101001234567894 101001234567895 101001234567896 101001234567897 101001234567898 101001234567899 file-2 (6 Replies)
Discussion started by: p_sai_ias
6 Replies

8. UNIX for Dummies Questions & Answers

Getting Substring

Hi, I hav a string lets say aa.txt:bb:txt length of the string can vary.. I have to keep the token inside a array and the delimiter is : plz send me the code (2 Replies)
Discussion started by: Deekay.p
2 Replies

9. Shell Programming and Scripting

substring

I have a string '<Hi>abc</Hi>" How to print "abc" (6 Replies)
Discussion started by: sandy1028
6 Replies

10. Shell Programming and Scripting

Again Substring!!!! Help

Hi, To remove the date and time from the below data which is in a file abc.txt 29 Jul 2009 04:36:53,956 ERROR 1 Error with Java 29 Jul 2009 04:36:58,335 ERROR 2 29 Jul 2009 05:37:24,746 ERROR 3 I want the output as ERROR 1 Error with Java ERROR 2 ERROR 3 As, In the above... (2 Replies)
Discussion started by: Pank10
2 Replies
Login or Register to Ask a Question