I'm not sure what your current question is (grin), but I'd like to focus on something you said earlier:
Quote:
At first I was trying with
Code:
awk '{print "mv " $1 $i}' > test2
but I couldnt figure out how to get $i to work within awk.
|
What's in your way is not awk. It's how the shell is interpreting $1 and $i.
What you want the shell to do is substitute a particular letter for the $i, but pass the $1 to awk just as it is.
Try this:
Code:
for xxx in A B C
do
echo literal: '$xxx'
echo expanded: "$xxx"
done
See the difference in the output when you tried that? The shell will leave everything enclosed in single quotes alone, but process the dollar signs in double quotes. In either case, the shell doesn't pass the surrounding quotation marks themselves to awk; what awk sees is everything between them (expanded by the shell or left alone, depending on what you tell the shell to do).
What you shouldn't do is this:
Code:
awk "{print "mv " $1 $i}" > test2
for two reasons. The first reason is that you want to pass just one string to awk, the string between the first and final " in that line. But you have two other " in there as well, and bash will match the first with the second, and the third with the fourth.
Try this:
Code:
echo "abc \"def\" ghi"
See what happend with the internal quotes when you tried that? So the following is an improvement ...
Code:
awk "{print \"mv \" $1 $i}" > test2
... but not quite good enough yet. The shell is gonna do the right thing with the $i, but it's gonna mess up the $1. You want the $1 to go to awk without the shell messing with it. What makes the shell spring into action, in this case unwanted action, is the dollar sign. Try this:
Code:
for xxx in A B C
do
echo expanded: "$xxx"
echo literal: "\$xxx"
done
See the difference in the output when you tried that? The shell will not process a dollar signs in double quotes if it's "escaped" with a backslash.
So what you want is this:
Code:
awk "{print \"mv \" \$1 $i}" > test2
As you've probably guessed up to now, in figuring out how things are quoted and expanded by the shell before being passed to other programs such as awk, the echo command is your friend.
Incidentally, I'd recommend that you stay away from single-letter variables such as i. A problem arises if later you use a text editor to look for each occurrence of that variable. You'll also find all other i's in the script, even if they're part of a comment or the i in "if", for example. That's why I used xxx in my prior post.
Hope this gets you closer to where you want to be.