Concatenation in awk not working | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Concatenation in awk not working

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-01-2013
Chetanz Chetanz is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 3 July 2013, 11:09 AM EDT
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Concatenation in awk not working

Hello

I want to achieve the following.

However the concatenation is not working

Code:
mv `ls -ltr *myfile*.log|awk '{print $9}'`  `ls -ltr *myfile*.log|awk '{print `date +'%d%m%y%k%M%S'` $9}'`

I tried

Code:
awk '{x=`date +'%d%m%y%k%M%S'` print $x "" $9}'
awk '{x=`date +'%d%m%y%k%M%S'` print x "" $9}'

But nothing is working

Please help me on this

Thanks and Regards
Chetanz
Sponsored Links
    #2  
Old 03-01-2013
neutronscott's Avatar
neutronscott neutronscott is offline Forum Advisor  
script kiddie
 
Join Date: Jun 2011
Last Activity: 29 August 2014, 3:43 AM EDT
Location: McMurdo Station, Antarctica
Posts: 789
Thanks: 28
Thanked 234 Times in 221 Posts
You're just trying to prepend the date to filename?


Code:
date=$(date +'%d%m%y%k%M%S')
for file in *myfile*.log; do
  mv "$file" "${date}${file}"
done

Sponsored Links
    #3  
Old 03-01-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 30 August 2014, 5:16 PM EDT
Location: Aachen, Germany
Posts: 4,145
Thanks: 67
Thanked 988 Times in 936 Posts
NO backtics in awk, please! And, using nested backtics like in your mv command needs escaping them. That's why backtics are deprecated and users are encouraged to use the $(...) form of command substitution.
In order to get the output of a shell command into an awk variable, you need to use command, pipe, and getline like in
Code:
$ awk '{"date +%d%m%y%k%M%S"|getline x; print x}' file
010313114139

BTW - the way you use the ls -ltr construct is far from optimal and would not fit mv usage, as both invocations may yield multiple files that mv does not handle that way. What's the reason to use it the way you do? Maybe there's other, improved ways to achieve the desired result.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk for concatenation of column values Gussifinknottle UNIX for Dummies Questions & Answers 10 01-01-2013 10:23 AM
Awk expressions working & not working vibhor_agarwali UNIX for Advanced & Expert Users 12 10-12-2011 11:52 AM
Awk concatenation in different lines posner Shell Programming and Scripting 4 06-29-2010 10:54 AM
String concatenation not working in a loop LostInTheWoods Shell Programming and Scripting 5 01-26-2010 04:09 AM
cannot get logic for concatenation awk user_prady Shell Programming and Scripting 7 12-10-2007 02:09 AM



All times are GMT -4. The time now is 10:16 PM.