Unix/Linux Go Back    

Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Concatenation in awk not working

Shell Programming and Scripting

Closed Linux or Unix Question    
Thread Tools Search this Thread Display Modes
Old Unix and Linux 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 Unix and Linux 03-01-2013
neutronscott's Unix or Linux Image
neutronscott neutronscott is offline Forum Advisor  
script kiddie
Join Date: Jun 2011
Last Activity: 7 October 2015, 3:59 PM EDT
Location: South Carolina, USA
Posts: 922
Thanks: 30
Thanked 295 Times in 275 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 Unix and Linux 03-01-2013
RudiC RudiC is offline Forum Advisor  
Registered User
Join Date: Jul 2012
Last Activity: 10 October 2015, 7:02 AM EDT
Location: Aachen, Germany
Posts: 6,902
Thanks: 116
Thanked 1,903 Times in 1,785 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 08:29 AM.