Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pattern Matching 1

Status
Not open for further replies.

IMAUser

Technical User
May 28, 2003
121
CH

How can I look for a certain string of chars within a string containing all kinds of chars, specailly slashes.
What I need to do is look for a file name within a path.
So tha path is /home/myvolume/data/newfile.txt
I need to check if the word 'new' exists witihn the string
'/home/myvolume/data/newfile.txt'

Thanks
 
Hi,

string='/home/myvolume/data/newfile.txt'
echo $string | grep "new" > /dev/null

if [ $? -eq 0 ]
then
echo "String found"
else
echo "String not found"
fi

This will need modifying a bit to fit into your own script.

Matt.
 
I have just learnt this in an answer to one of my own posts as an alternative ...

------

#!/bin/ksh

string='/home/myvolume/data/newfile.txt'

[[ $string = *new* ]] && echo "Found" || echo "Not Found"

------
Matt.
 
Try this :
Code:
string='/home/myvolume/data/newfile.txt'
case "$string" in "*new*")
  ...
esac

Hope This Help
PH.
 
Thanks for all your replies.

A slight change in the things I had to do. The string is contained in a file, so the daa file looks like

AAA BBB TTT /home/myvolume/data/newfile.txt
AAA XXX YYY XXX ZZZ /home/myvolume/data/newfile.txt GGG TTT
AAA BBB TTT /home/myvolume/data/oldfile.txt FFF GGG
AAA BBB TTT /home/myvolume/data/newfile.txt

I was trying to use awk to read the datafile and process each record and produce a count of the occurences of 'new' and 'old'.
Since I m no good at awk, I am unbale to get the desired results.

Thanx
 
nawk 'match($0, "/[^ $]+new.*")' myFile.txt

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
to get the counts:

nawk -f newOld.awk myFile.txt

#-------------------------- newOld.awk
match($0, &quot;/[^ $]+&quot;) {
if ( substr($0, RSTART, RLENGTH) ~ /new/)
newC++;
if ( substr($0, RSTART, RLENGTH) ~ /old/)
oldC++;
}
END {
printf(&quot;newC->[%d] oldC->[%d]\n&quot;, newC, oldC);
}


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
This would put the counts into two shell environment variables, $OLD and $NEW .....

awk '/old/ {o++} /new/ {n++} END {print o,n}' datafile | read OLD NEW
 
Thanks all specially vgersh99.

The script you gave works perfectly fine. But I dont understand the pattern in the match command
match($0, &quot;/[^ $]+&quot;)
What does &quot;/[^ $]+&quot; mean, I know ^ = start of line $=end of line, but what is / and + for.
Ad also , how does it loop through all the records n the file.

Thanks a ton.
 
match($0, &quot;/[^ $]+&quot;)

here's I'm testing if there's an absolute pathname anywhere in the currect record. An absolute pathname starts with a '/' - that's what the first '/' is in the RE for the match.

I assume an absolute pathname starts with a '/' and contain ANY number of characters and ends either with a space (' ') OR with the EndOfLine ('$').

'[^ $]+' - means more than occurance ('+') of any character BUT either a space or a EndOfLine. The '^' as the FIRST char. in '[]' is a negation operator for the trailing list of characters.

Awk by default goes through all the records in the supplied file. By default a record is a stream of chars separated by the EndOfLine character. You can change your INPUT record separator (RS) by assigning RS a different value if your records separated but something OTHER than a EndOfLine (say blank lines separating records).

HTH

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top