The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
jar command not being recognized orahi001 UNIX for Dummies Questions & Answers 1 05-06-2008 07:29 AM
Retrieve 5th Field to Last Field !! jobbyjoseph UNIX for Dummies Questions & Answers 3 05-16-2007 12:20 AM
Moving Part of a field to another field using AWK rjsha1 Shell Programming and Scripting 5 08-04-2006 02:39 AM
I am not being recognized Help Forum Support Area for Unregistered Users & Account Problems 0 01-10-2006 12:30 AM
Argument not recognized as integer scmay Shell Programming and Scripting 1 05-13-2004 11:41 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-07-2007
Registered User
 

Join Date: Oct 2007
Posts: 3
Post awk field not recognized in backquotes

Hi,

I am writing a script to use awk to generate a set of cp commands from an input file abc.

file abc:
/data/a.dbf
/data/june/b.dbf

desired output:
cp -pr a.dbf /data/a.dbf
cp -pr b.dbf /data/june/b.dbf

script:
$ cat abc | awk '{ print "cp -pr '`basename $1`' " $1 }'

I tried to use awk with basename command, but it seems that the field $1 in backquotes is not recognized by awk. How to fix it?

Many thanks,
voa2mp3
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 10-07-2007
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
A few solutions

awk:
Code:
$ awk '{s=$0;gsub( /^.*\//, "" );printf("cp -pr %s %s\n", $0, s )}' abc
cp -pr a.dbf /data/a.dbf
cp -pr b.dbf /data/june/b.dbf
sed:
Code:
$ sed 's!^\(.*\)/\(.*\)!cp -pr \2 \1/\2!' abc 
cp -pr a.dbf /data/a.dbf
cp -pr b.dbf /data/june/b.dbf
bash/ksh:
Code:
$ while read file; do echo "cp -pr ${file##*/} ${file}"; done < abc 
cp -pr a.dbf /data/a.dbf
cp -pr b.dbf /data/june/b.dbf
Reply With Quote
  #3 (permalink)  
Old 10-07-2007
Registered User
 

Join Date: Oct 2007
Posts: 3
Hi zazzybob,

Thanks for you help.

voa2mp3
Reply With Quote
  #4 (permalink)  
Old 10-07-2007
drl's Avatar
drl drl is offline
Registered User
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 497
Hi.
Quote:
Originally Posted by voa2mp3 View Post
...
file abc:
/data/a.dbf
/data/june/b.dbf

desired output:
cp -pr a.dbf /data/a.dbf
cp -pr b.dbf /data/june/b.dbf
...
Placing the input on fille "data1", and changing your script slightly to:
Code:
#!/usr/bin/env sh

# @(#) a1       Demonstrate awk feature "command | getline".

set -o nounset
echo

## Use local command version for the commands in this demonstration.

echo "(Versions of codes used in this script -- local code \"version\")"
version bash awk

echo

awk '
        { command = "basename " $1
        command | getline file
        print "cp -pr " file " " $1 }
' data1

exit 0
produces:
Code:
% ./a1

(Versions of codes used in this script -- local code "version")
GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)
GNU Awk 3.1.4

cp -pr a.dbf /data/a.dbf
cp -pr b.dbf /data/june/b.dbf
cheers, drl
Reply With Quote
  #5 (permalink)  
Old 10-07-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,432
Some other ways

Code:
% set -- $(<file)                                                   
% paste -d" " <(printf "cp -pr %s\n" "${@##*/}") <(printf "%s\n" "$@")
cp -pr a.dbf /data/a.dbf
cp -pr b.dbf /data/june/b.dbf
and with zsh:

Code:
zsh 4.3.4% <file while IFS= read;do print -r "cp -pr $REPLY:t $REPLY";done 
cp -pr a.dbf /data/a.dbf
cp -pr b.dbf /data/june/b.dbf
or awk:

Code:
zsh 4.3.4% awk '$0="cp -pr "$NF" "$0' FS="/" file
cp -pr a.dbf /data/a.dbf
cp -pr b.dbf /data/june/b.dbf
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:31 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0