Unix/Linux Go Back    

Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Concatenation in awk not working

Shell Programming and Scripting

Thread Tools Search this Thread Display Modes
Old Unix and Linux 03-01-2013   -   Original Discussion by Chetanz
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   -   Original Discussion by Chetanz
neutronscott's Unix or Linux Image
neutronscott neutronscott is offline Forum Advisor  
script kiddie
Join Date: Jun 2011
Last Activity: 21 September 2017, 10:02 PM EDT
Location: South Carolina, USA
Posts: 941
Thanks: 31
Thanked 303 Times in 281 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   -   Original Discussion by Chetanz
RudiC RudiC is offline Forum Staff  
Join Date: Jul 2012
Last Activity: 18 November 2017, 12:12 PM EST
Location: Aachen, Germany
Posts: 11,622
Thanks: 320
Thanked 3,616 Times in 3,321 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

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 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 06:33 AM.