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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

test if string exists on a line

Status
Not open for further replies.

mbach

Technical User
Dec 12, 2000
24
US
All,

I have a file which contains 2 lines. One line has a server name listed and the line below it shows a path. Example:
ECCN088
C:\winnt\temp
I want to write a short script that will read each line in the file and echo whether the line contains a server or a file path. I was trying to test each line for a \ or : since the lines with file paths will always contain those special characters. That way if it finds those characters on a line, the script would echo "This is a file path" and if the script doesn't see those characters it would echo "This is a server".
Any hints?
Thanks,
Mike
 
nawk '{printf("This is a %s\n", (FNR % 2) ? "server" : "file path"); } myFile.txt

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
ooops

nawk '{printf(&quot;This is a %s\n&quot;, (FNR % 2) ? &quot;server&quot; : &quot;file path&quot;); }' myFile.txt

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
while read -r line
do
case $line in
*:*|*\\*) print this $line is a file path ;;
*) print this $line is a server ;;
esac
done < file1
 
vgersh99/ygor,

Thanks!!!
You guys are good!!!
Mike
 
ygor,
The print statement prints D: emp\system32 instead of D:\temp\system32. How can I make it print the exact file path as it reads it from a file?
Mike
 
print -r

from man ksh..

print[-Rnprsu[n]] [arg ...]
The shell output mechanism. With no options or with
option - or -- the arguments are printed on standard
output as described by echo(1). Raw mode, -R or -r,
ignores the escape conventions of echo.
 
ygor,
That did the trick!!!!!
Thank You!!
 
Ygor,
I would like my script to do one more thing. After it determines which lines are servers and which lines are file paths, I want it to plug in the value to the appropriate variable in a backup command. Example: A file is created with a list of servers and files which were not backed up. It looks like this:
ECCN096
C:\winnt\system32
D:\home\users
ECCN085
C:\temp
I want the script to determine all the file paths that are associated with the correct server (the server listed above the file paths) and put this info into a command line backup command. So, there would be 2 backup commands run for the first server, followed by another backup command for the second server. The command would look like this.....
bpbackup -i $server $file_path. I want the script to test each line and if the first line is a server, read the next line. If the next line is a file path, then plug the info into the bpbackup command. As long as the next lines are file paths, the same server would be used for the bpbackup command. As soon as it finds a line which is a server, it would look at the following lines for the file path info and run the bpbackup command. I hope this makes some sense, this is the last step needed for my backup rerun automation project to be a success.
Thanks for your help!!!!
Mike
 
How about:

# to debug:
nawk '{printf(&quot;%s&quot;, (FNR % 2) ? &quot;bpbackup -i $0&quot; : &quot;$0\n&quot;); }' myFile.txt

#to do a real backup:
nawk '{printf(&quot;%s&quot;, (FNR % 2) ? &quot;bpbackup -i $0&quot; : &quot;$0\n&quot;); }' myFile.txt | sh


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
If vlad's code works then go for it! Otherwise try...

while read -r line
do
case $line in
*:*|*\\*) file_path=$line ;
bpbackup -i $server $file_path ;;
*) server=$line ;;
esac
done < file1
 
ooops - sorry about that. This is better:

nawk 'FNR % 2 {printf(&quot;%s%s&quot;, &quot;bpbackup -i &quot;, $0); next} 1' myFile.txt

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
or better yet:

nawk 'FNR % 2 {printf(&quot;bpbackup -i %s&quot;, $0); next} 1' server.txt

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

Part and Inventory Search

Sponsor

Back
Top