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

Protecting spaces in FOR loop - not working with AWK

Status
Not open for further replies.

Exie

Programmer
Sep 3, 2003
156
AU
Hi,

I'm trying to process a log file to extract the file names created. The problem I have is some of the filenames contain spaces. This line process the log:
[blue]cat /somewhere/file.log | awk 'BEGIN { FS="location " } /Processing file/ { print "\""$2"\"" }'[/blue]

This produces:
"/somepath/file 001.zip"
"/somepath/file 002.zip"


I've tried putting this in my for loop like so:
Code:
for i in `cat /somewhere/file.log | awk 'BEGIN { FS="location " } /Processing file/ { print "\""$2"\"" }' `
do
	echo "Copying file $i ...."
done

But I allways get:
Copying file "/somepath/file ....
Copying file 001.zip" ....
Copying file "/somepath/file ....
Copying file 002.zip" ....


Any idea how I can protect the filenames ? I know spaces in unix are a bad idea, but we dont have much choice.

Any hints/tips/thoughts welcome.
 
... Just an update, I tried the following code:
Code:
for i in "$(cat /somewhere/file.log | awk 'BEGIN { FS="location " } /Processing file/ { print $2 }')"
do
	echo "Copying file $i ...."
done

This NEARLY worked, it did protect the full path & filename, BUT, it joined both files together. The cat & awk line is supposed to produce 2 separate strings causing 2 iterations of the for loop.

Output was like so:
Copying file /apps/cognos/cubes/fieldlink/notices current.mdc
/apps/cognos/cubes/fieldlink/fieldlink current.mdc ....


Notice only one "Copying File" output was produced. :(
 
somehow this can be made more compact, but I don't like to create the same folderstructure as you to test it.

To write and read to a file looks like a pipe-pattern:
-pseudocode-
(cat ...) | (while .... done)
or
(while .... done) < (cat ...)

Code:
cat /somewhere/file.log | awk 'BEGIN { FS="location " } /Processing file/ { print "\""$2"\"" }' > log.file
while read INPUTLINE
	do
	echo "copying file " $INPUTLINE
done < log.file


seeking a job as java-programmer in Berlin:
 
Thanks Stefan,

That worked a treat!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top