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!

Can I shorten/improve this script(grep/cut/touch/mv) 2

Status
Not open for further replies.

baggetta

Technical User
Feb 27, 2003
116
US
I have this script below that allows me to rename a file based on the word I find in a file. Is there an easier way of doing this? I will be executing this script remotely from another server after it ftp's the file to another server.

#/bin/ksh
grep "*IUNB" t.edw > r.out
cut -c 6-10 r.out > r.in
touch a-riv`cat r.in`.edw
mv t.edw a-riv`cat r.in`.edw

t.edw = source file
r.out = stores the grep line
r.in = stores the name I was looking for
a-riv`cat r.in`.edw will be the new name of the file keeping the original contents of t.edw
 
Well, if your string you are looking for in the [tt]grep[/tt] appears more than once in [tt]t.edw[/tt], then these commands won't work. If it does appear only once, then this should work...
Code:
    #/bin/ksh

    mv t.edw a-riv$(grep "*IUNB" t.edw | cut -c 6-10).edw
The touch isn't really needed since you are moving [tt]t.edw[/tt] to that name in the next step.

The only problem you might have is if there is a character at position 6 to 10 in that line that is invalid for a filename. Such as a space or an asterisk (*).

Hope this helps.

 
Sweet...thanks this is much nicer...
question, what does the "$" do before the (grep...)
 
The $(grep "*IUNB" t.edw | cut -c 6-10) will be replaced with the output of those two commands.

Try this and you'll get an idea of how it works:

[tt]echo The date is $(date)[/tt]

Annihilannic.
 
In the Korn shell, using [tt]$(somecommand)[/tt] is the same as [tt]`somecommand`[/tt]. It's a Korn shell equivalent of back ticks.

Hope this helps.

 
One more problem, how can I cut ahead and then back from the output of grep, can it be done with 1 command?

This is the output of my grep:
*IUNBCHERA ZZ FASJRIV Z
Z UNOA22003090911521002 '+:

Now I need to cut -c 49-51 | cut -c 6-10 from this grep.
Output should now be r-RIVCHERA.edw
 
Try something like this:
Code:
mv t.edw r-$(awk '/^\*IUNB/{
printf "%s%s",substr($0,49,3),substr($0,6,5);exit
}' t.edw).edw

Hope This Help
PH.
 
nawk '/IUNB/ { printf("%s%s\n", substr($0, 49,51-49+1), substr($0, 6,10-6+1) )}' t.edw

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
When I get the new file name written to disk, how can set an environment variable to use this new file in an ftp command that uses an existing file name of &quot;EDI_JOB_NUMBER&quot; which I don't want to change. Here's part of my script:

mv *${EDI_JOB_NUMBER}* a-$(grep &quot;DESADV&quot; *${EDI_JOB_NUMBER}* | cut -c 6-14).edw
#The command above will rename the EDI_JOB_NUMBER file to this format &quot;a-SITEVENDOR.edw&quot;
#Copy new file name to variable EDI_JOB_NUMBER
#FTP the new file
#
echo open ${SYSTEM} >> ${FTPFILE}
echo user ${USER} ${PASSWORD} >> ${FTPFILE}
echo type ascii >> ${FTPFILE}
echo cd ${PATH} >> ${FTPFILE}
echo mput *${EDI_JOB_NUMBER}* >> ${FTPFILE}

 
IMHO your script can't do what you want.
Do a
Code:
 man mv
to see what I mean.
> mv *${EDI_JOB_NUMBER}* a-$(grep &quot;DESADV&quot; *${EDI_JOB_NUMBER}* | cut -c 6-14).edw
The wild chars * in the above command will put your script in trouble.
Try something like this:
Code:
echo open ${SYSTEM} >> ${FTPFILE}
echo user ${USER} ${PASSWORD} >> ${FTPFILE}
echo type ascii >> ${FTPFILE} 
echo cd ${PATH} >> ${FTPFILE} 
#         ^^^^ Are you sure ?
for old in $(ls *${EDI_JOB_NUMBER}*); do
  new=a-$(grep &quot;DESADV&quot; $old | cut -c 6-14).edw
  mv $old $new
  echo put $new >> ${FTPFILE}
done

Hope This Help
PH.
 
PH, I'm getting a Variable syntax. Maybe I should have mentioned this script is being executed in a c shell env.
So if I were try this under straight AIX using ./file, it shouldn't work.
r
 
The first line of your script should read #!/bin/ksh not #/bin/ksh.

If you correct that it will force your script to be interpreted by the Korn shell and the variable syntax error should go away.

Annihilannic.
 
Good eye Annihilannic! I'll give you my star because I missed that on the first reply! [thumbsup]

You don't really need the temporary file $FTPFILE for all the commands. You could just do this...
Code:
#!/bin/ksh

# Need to set SYSTEM, USER, PASSWORD, REMOTEPATH
# and EDI_JOB_NUMBER before the rest will work

( print user ${USER} ${PASSWORD}
print ascii
print cd ${REMOTEPATH}
for OLDNAME in $(ls *${EDI_JOB_NUMBER}*)
do
    NEWNAME=a-$(grep &quot;DESADV&quot; ${OLDNAME} | cut -c 6-14).edw
    mv ${OLDNAME} ${NEWNAME}
    print put ${NEWNAME}
done
) | ftp -n ${SYSTEM}
Just enclose the whole thing in parenthesis to make it a subprocess, and pipe it's output to [tt]ftp[/tt].

Hope this helps.

 
A bit more help. I got the for loop to work as I need it, but is it possible to make the new file name into a variable name so I can ftp the variable name? Since I don't know what these new files will be called, and I don't want to ftp each individual file one by one.

foreach i (`ls ${dd_VLT_IN}*${EDI_JOB_NUMBER}.*`)
setenv rnew $i
echo &quot;EDI SNF File: ${rnew}&quot;
grep &quot;DESADV&quot; ${rnew} | cut -c 6-14 > r_temp
cat r_temp
*** cp ${rnew} a-`cat r_temp`.${EDI_JOB_NUMBER}.${ext}
rm a-`cat r_temp`.${EDI_JOB_NUMBER}.${ext}
rm r_temp
end

*** In this cp command, can the new file become a variable that I can use to ftp? Or do I have to ftp each file 1 by 1 since I know what the r_temp file will be?
 
I'm not confident with C-shell syntax, as I'm haird that csh was broken an many flavors of *nix...
This is my guess:
Code:
foreach i (`ls ${dd_VLT_IN}*${EDI_JOB_NUMBER}.*`)
 setenv rnew $i
 echo &quot;EDI SNF File:      ${rnew}&quot;
 setenv anew a-`grep &quot;DESADV&quot; ${rnew} | cut -c 6-14`.${EDI_JOB_NUMBER}.${ext}
 echo &quot; DESADV File:      ${anew}&quot;
end
Anyway, I don't understand why you insist to mput files instead to do multiple put in the same ftp-command file.
If you want to remove the $(anew) files after ftp-ing them, you can store the names within the loop in an another variable, or sed-ing the ftp-command file to retrieve the put statements.


Hope This Help
PH.
 
PHV, sorry but that did work. I guess you cannot do a setenv statement followed by the &quot;a-grep command&quot;. Came back with Ambigious command. So me being the trail and error person, tried &quot;sentevn anew $a-grep...came back with underfined variable.
Thanks for the help anyways. My script works perfectly when it receives one file. My other problem now is I'm testing when 2 files are received, the script concatenates them before creating 1 file. So my r_temp will have 2 lines in it:
RIV-ABCD
RIV-EFGH
Do you know a command to extract only the first line and save it to file or variable? I figure if I can do this, then were there are multiple files in my r_temp, I can go down the list and pick up the new name to save to my new file.
 
I guess you forgot the 2 ` in the setenv command, or the
Code:
 cut -c 6-14
returns trailing space(s).
Yet another guess:
Code:
foreach i (`ls ${dd_VLT_IN}*${EDI_JOB_NUMBER}.*`)
 setenv rnew $i
 echo &quot;EDI SNF File:      ${rnew}&quot;
 set x=`grep &quot;DESADV&quot; ${rnew} | cut -c 6-14`
 setenv anew a-${x}.${EDI_JOB_NUMBER}.${ext}
 echo &quot; DESADV File:      ${anew}&quot;
end

Hope This Help
PH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top