Operating System - HP-UX
1825801 Members
2223 Online
109687 Solutions
New Discussion

Re: parse out last bit of file name in scripts

 
SOLVED
Go to solution
Ratzie
Super Advisor

parse out last bit of file name in scripts

I have a script that runs a for loop.

for i in `cat /tmp/file`
do
yada
send output to file.
done

The /tmp/file contains...
pass.change.some_server1
pass.change.some_server2
pass.change.some_server3
pass.change.some_server4

I am trying to add mailing of the output file, but would like to add to the subject line the end part of the pass.change.some_server1.
so just some_server1.

I thought I could do it with an awk script but I have not been able to get it to work.

I want to take $i and parse it down to the last part of some_server1 and store that is a variable then use it in the subject line.
9 REPLIES 9
Sridhar Bhaskarla
Honored Contributor
Solution

Re: parse out last bit of file name in scripts

Hi,

I would do something like this (if I understood your requirement correctly)

//
EMAIL=your_id@your_domain.com
for FILE in $(cat /tmp/file)
do
SERVER=$(echo $FILE|awk '{FS=".";print $3}')
mailx -s "Something on $SERVER" $EMAIL < $FILE
done
//

SERVER=$(echo $FILE|awk '{FS=".";print $3}')

Is the key.

-Sri

You may be disappointed if you fail, but you are doomed if you don't try
A. Clay Stephenson
Acclaimed Contributor

Re: parse out last bit of file name in scripts

for i in $(cat /tmpfile)
do
XX=$(echo "${i}" | awk -F '.' '{print $NF}')
echo "XX = ${XX}"
done


The trick is to define '.' as awk's field separator and then print the last field ($NF) on each line.

If it ain't broke, I can fix that.
curt larson_1
Honored Contributor

Re: parse out last bit of file name in scripts

or you could do something like this

server="pass.change.some_server3"
file=${server##*.}
print $file
Michael Schulte zur Sur
Honored Contributor

Re: parse out last bit of file name in scripts

Hi,

you could also extract it with
expr "$LINE" : ".*\(some_server.*\).*"

Michael
Jean-Luc Oudart
Honored Contributor

Re: parse out last bit of file name in scripts

if this is always the 3rd field (separator=".") then you can write :
for fln in $(cat /tmp/file)
do
server=$(echo $fln | cut -f3 -d".")

send output to file.
done


Regards,
Jean-Luc
fiat lux
Kenneth Platz
Esteemed Contributor

Re: parse out last bit of file name in scripts

for i in `cat /tmp/file`
do
server=$( print $i | sed -e 's/^.*\.//' )
print $server
done

Should give you:

some_server1
some_server2
some_server3
some_server4
I think, therefore I am... I think!
Mark Grant
Honored Contributor

Re: parse out last bit of file name in scripts

I am assuming that you only want the server name from the first entry in the file. I hope I am reading this correctly.

so my effort is this

mailx -s `awk -F"." '{ print $3;exit }' /tmp/file' someone@some.domain
Never preceed any demonstration with anything more predictive than "watch this"
Mark Grant
Honored Contributor

Re: parse out last bit of file name in scripts

Sorry, got one quote wrong! Should read

mailx -s `awk -F"." '{ print $3;exit }' /tmp/file` someone@some.domain
Never preceed any demonstration with anything more predictive than "watch this"
Bill Hassell
Honored Contributor

Re: parse out last bit of file name in scripts

Here's an even simpler way, let the shell do it for you:

set -u
PATH=/usr/bin
MYEMAIL=somebody@someplace.com
cat /tmp/file | read MYFILE
do
SERVER=${MYFILE##*.}
mailx -s "$SERVER output file" < $MYEMAIL
done

The funny incantation ##*. says to return anything following the last appearance of the . character. For this construct, any number of dots may appear and only the text following the last dot will be returned. This is similar to ${some_full_pathname##*/} which returns just the name at the end of a bunch of slashes, like the basename command.


Bill Hassell, sysadmin