awk print variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk print variable
# 1  
Old 05-07-2011
awk print variable

I have list of files:

Code:
ls
a.pdf
b.pdf
c.pdf

and so on...

and I have a file like this:

Code:
cat file1
apple
mango
pear

and so on...

I want to rename my file like this:

Code:
mv a.pdf a-apple.pdf
mv b.pdf b-mango.pdf
mv c.pdf c-pear.pdf

and so on...

So far I have in mind something like this:

Code:
for i in `ls`
do
mv $i $(echo $i|awk -F. '{print $1'$(awk {NR==$j} file1)'"."$2})'
done

but I know that has many problems which I need some help to solve.
Please help. Thanks.

Last edited by radoulov; 05-07-2011 at 04:00 AM.. Reason: Code tags!
# 2  
Old 05-07-2011
Try this

Code:
#!/bin/ksh

j=1
for i in *.pdf; do
prefix=$( echo $i | cut -f1 -d"." ); suffix=$( echo $i | cut -f2 -d"." )
newfile=$prefix"-"$( awk -F. -v j=$j 'NR==j {print $1}' file1 )"."$suffix
mv $i $newfile; ((j=j+1))
done

regards,
Ahamed
# 3  
Old 05-07-2011
Another one:

Code:
awk 'BEGIN {
  while ((getline < "file1") > 0) { 
    split(ARGV[ ++c ], x, ".")
    printf "mv -- \"%s\" \"%s-%s.%s\"\n", \
      ARGV[c], x[1], $1, x[2]
  }
}' *.pdf

An example:

Code:
zsh-4.3.11[t]% cat file1
apple
mango
pear
zsh-4.3.11[t]% ls *pdf
a.pdf  b.pdf  c.pdf
zsh-4.3.11[t]% awk 'BEGIN {
quote>   while ((getline < "file1") > 0) { 
quote>     split(ARGV[ ++c ], x, ".")
quote> printf "mv -- \"%s\" \"%s-%s.%s\"\n", \
quote>   ARGV[c], x[1], $1, x[2]
quote>   }
quote> }' *.pdf | bash -xv
mv -- "a.pdf" "a-apple.pdf"
+ mv -- a.pdf a-apple.pdf
mv -- "b.pdf" "b-mango.pdf"
+ mv -- b.pdf b-mango.pdf
mv -- "c.pdf" "c-pear.pdf"
+ mv -- c.pdf c-pear.pdf
zsh-4.3.11[t]% ls *pdf
a-apple.pdf  b-mango.pdf  c-pear.pdf

These 2 Users Gave Thanks to radoulov For This Post:
# 4  
Old 05-07-2011
Thank you to both of you. I am going to test it it just that I just realized that my pdf files order is all wrong. The files order is like this when I run ls:
Code:
filex-1.pdf
filex-11.pdf
filex-2.pdf
filex-23.pdf
filex-216.pdf
filex-3.pdf
filex-35.pdf

and so on.....

How can I sort it so it will be like this:
Code:
filex-1.pdf
filex-2.pdf
filex-3.pdf
filex-11.pdf
filex-23.pdf
filex-35.pdf
filex-216.pdf

I think may be I can add "0" before the number part of the file names, so if the total of digit is only 1 add 2 more "0"s in front of it and add 1 more "0" if the total of digit is 2. Any better ideas on how shell script may help?
# 5  
Old 05-07-2011
It's easy to fix,
how exactly the new file names should look like?
# 6  
Old 05-07-2011
The code below will rename filex-1.pdf to filex-1-apple.pdf:


Code:
printf '%s\n' *.pdf |
  sort -t- -k2n |
    awk -F. 'getline f1 < "file1" {
      printf "mv -- \"%s\" \"%s-%s\.%s\"\n", \
        $0, $1, f1, $2
      }' | bash -xv

This User Gave Thanks to radoulov For This Post:
# 7  
Old 05-07-2011
Your first codes seem to be very advance to a newbie like me. but thank you radoulov. it works. Can you explain your codes:
Code:
awk 'BEGIN {
  while ((getline < "file1") > 0) { 
    split(ARGV[ ++c ], x, ".")
    printf "mv -- \"%s\" \"%s-%s.%s\"\n", \
      ARGV[c], x[1], $1, x[2]
  }
}' *.pdf

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk print columns and variable

Hi, Can anyone help with the below please? I have written some code which takes an input file, and and prints the contents out to a new file - it then loops round and prints the same columns, but increments the ID column by 1 each time. Input file; NAME,1,15-Dec-15, NAME,1,21-Dec-15,... (9 Replies)
Discussion started by: Ads89
9 Replies

2. Shell Programming and Scripting

Variable won't print out - awk

done. (1 Reply)
Discussion started by: myaa02
1 Replies

3. Shell Programming and Scripting

awk print variable then fields in variable

i have this variable: varT="1--2--3--5" i want to use awk to print field 3 from this variable. i dont want to do the "echo $varT". but here's my awk code: awk -v valA="$varT" "BEGIN {print valA}" this prints the entire line. i feel like i'm so close to getting what i want. i... (4 Replies)
Discussion started by: SkySmart
4 Replies

4. Shell Programming and Scripting

awk print using a variable

hey, just want to ask how to do this. ex. g="hi i am john" h=`echo $g | awk '{print $2}'` echo $h OUTPUT is 'i' What if I want to use a variable instead of using '2', how do I do that? Because this one does not work: a=2 h=`echo $g | awk '{print ${$a}}'` this one also does not... (3 Replies)
Discussion started by: h0ujun
3 Replies

5. Shell Programming and Scripting

awk print redirection to variable file name

Hello, i need to redirect the output of print to a variable file name: #This is normal awk '{ print $17 > "output.txt" }' input #I need something like this awk '{ print $17 > "output_${25}.txt" }' input how to format the output file name to contain a variable? (6 Replies)
Discussion started by: nazeeb
6 Replies

6. Shell Programming and Scripting

How to print a value in the variable using awk ?

:b:Hi All, I have a part of a script below: var1="value1" awk 'BEGIN {printf("%36s \n ","value1")}' Instead of directly giving the "value1" , I need to give using "var1" in the above awk statement. Is this possible? If so, what is the modified awk command? Thanks in advance JS (1 Reply)
Discussion started by: jisha
1 Replies

7. Shell Programming and Scripting

print variable in file using awk

hi, i have store variable in $var i want to print it in $3 of file using awk. how can i do it? file : var1,var2,var3 var5,var6,var7 var8,var9,var10 . . . i want to print $var in $3 ( for example var3 or var7 or var10) thanks (3 Replies)
Discussion started by: kamel.seg
3 Replies

8. Shell Programming and Scripting

variable inside awk '{print $c}'

i'm trying to do this (in bash darwin); echo "give me some words: " read a c=2 # this is get by other ways echo $a | awk '{print $c}' # i want to print the column given # by de $c variable if there is someone understand what i pretend... (3 Replies)
Discussion started by: Tártaro
3 Replies

9. Shell Programming and Scripting

can awk print column using a variable ??

i want to print the column file using awk or cut in dynamic manner like trmp=2;temp1=1;temp3=2 awk 'BEGIN{OFS=IFS="\t"} {print $temp,$temp1,$temp3}' client_data.txt or cut -f $temp1,$temp2,$temp3 -d"\t" file_name . but it is showing error , In awk can i use variable as in printing... (36 Replies)
Discussion started by: jambesh
36 Replies

10. Shell Programming and Scripting

print variable in awk

i read the variable ph from file and i wanna to print it in awk. example ph=`cat tmpbatch` tail h.txt|grep "| |"|awk -F"|" '{ print "@unpdx.sql",$5 }'"$ph" i try this but it does not work (8 Replies)
Discussion started by: kazanoova2
8 Replies
Login or Register to Ask a Question