1835064 Members
2540 Online
110073 Solutions
New Discussion

Re: scripting - awk

 
SOLVED
Go to solution
Rudeboy_1
Occasional Advisor

scripting - awk

Hi guys,

I'm new to scripting and have simple question about a script I'm trying to do, wonder if someone can help?

The script creates a temporary file containing a list of files I'm interested in, what I now want the script to do is mv those files to a different directory. I'm sure it's easy but I can't get my head around it!

I've been trying with the following loop but no result:

for i in `cat $LISTFILE | awk `{print $1}``
do cp $i $OUTDIR
done

Can anyone see the problem or suggest something more elegant (but simple please!)

Thanks in advance!



8 REPLIES 8
Ramkumar Devanathan
Honored Contributor

Re: scripting - awk

I see a couple of mistakes in the script - but i wonder if those are causing the problem. here's my corrected version of your script anyhow -

# define LISTFILE and OUTDIR
LISTFILE=/tmp/abc.txt
OUTDIR=/target_dir

for file in `cat $LISTFILE | awk '{print $1}'` # notice presence of both grave accent and single quotes here
do
cp $i $OUTDIR
done

<<<<<<<<<<

should work fine now.

- ramd.
HPE Software Rocks!
John Poff
Honored Contributor
Solution

Re: scripting - awk

Hi,

Here is one way to do it:

for i in $(awk '{print $1}' $LISTFILE);
do
cp $i $OUTDIR
done


The $() executes a command just like the back ticks do.

If your LISTFILE just contains the file names and no other fields, you could do the for line like this:

for i in $(<$LISTFILE)

JP
Rodney Hills
Honored Contributor

Re: scripting - awk

A command does exist to copy a set of files from a given list to another directory. That would be "cpio". Assuming your list of filenames are not absolute paths (ie begin with a "/"), then you can do the following-

cd fromdirectory
cat $LISTFILE | cpio -pd $OUTDIR

Do a "man cpio" to see if it can be used for your task.

HTH

-- Rod Hills
There be dragons...
Ramkumar Devanathan
Honored Contributor

Re: scripting - awk

And Rudeboy that's the difference between royalty and grads... see their really elegant solutions.

- ramd.
HPE Software Rocks!
Dario_1
Trusted Contributor

Re: scripting - awk

Hi!!

Make sure LISTFILE and OUTDIR are defined. Also if the filenames are single names I will take the awk command out.

DR
Rudeboy_1
Occasional Advisor

Re: scripting - awk

Cool, job done thanks for the help.
Donny Jekels
Respected Contributor

Re: scripting - awk

#!/usr/bin/ksh

safe_copy()
{
for i in $(awk '{print $1}' $LISTFILE);
do
cp $i $OUTDIR
if [ $? = 0 ]
then
printf "$i copied succesfully to $OUTDIR\n"
else
printf "$i failed to copy\n"
fi
done
}

email_results()
{
SUBJECT=$*
USERS="youremailaddress@where-ever.net"
mailx -s "${SUBJECT}" $USERS <${OUTFILE}
}
cleanup()
{
rm /tmp/$$.tmp
}

#
# Main
#

trap 'cleanup' 0

OUTDIR=
LISTFILE=
OUTFILE=$$.tmp

save_copy | tee -a $OUTFILE
email_results

exit 0



___END___

enjoy the freedom of ITRC

peace
Donny
"Vision, is the art of seeing the invisible"
Zafar A. Mohammed_1
Trusted Contributor

Re: scripting - awk

Wow, by the time I am posting you got the answers. This group is very fast & furious(all are like Bullet Monk).

Thanks
Zafar