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
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


I want to achieve the following.

However the concatenation is not working

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

I tried

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
Sponsored Links
Old 03-01-2013
neutronscott's Avatar
neutronscott neutronscott is offline Forum Advisor  
script kiddie
Join Date: Jun 2011
Last Activity: 23 February 2015, 11:01 AM EST
Location: McMurdo Station, Antarctica
Posts: 822
Thanks: 28
Thanked 246 Times in 232 Posts
You're just trying to prepend the date to filename?

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

Sponsored Links
Old 03-01-2013
RudiC RudiC is offline Forum Advisor  
Registered User
Join Date: Jul 2012
Last Activity: 2 March 2015, 12:38 PM EST
Location: Aachen, Germany
Posts: 5,417
Thanks: 94
Thanked 1,410 Times in 1,328 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
$ awk '{"date +%d%m%y%k%M%S"|getline x; print x}' file

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 11:23 AM
Awk expressions working & not working vibhor_agarwali UNIX for Advanced & Expert Users 12 10-12-2011 12:52 PM
Awk concatenation in different lines posner Shell Programming and Scripting 4 06-29-2010 11:54 AM
String concatenation not working in a loop LostInTheWoods Shell Programming and Scripting 5 01-26-2010 05:09 AM
cannot get logic for concatenation awk user_prady Shell Programming and Scripting 7 12-10-2007 03:09 AM

All times are GMT -4. The time now is 11:09 PM.