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 11:29 AM
Retrieve 5th Field to Last Field !! jobbyjoseph UNIX for Dummies Questions & Answers 3 05-16-2007 04:20 AM
Moving Part of a field to another field using AWK rjsha1 Shell Programming and Scripting 5 08-04-2006 06:39 AM
I am not being recognized Help Forum Support Area for Unregistered Users & Account Problems 0 01-10-2006 04:30 AM
Argument not recognized as integer scmay Shell Programming and Scripting 1 05-14-2004 03:41 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-07-2007
voa2mp3 voa2mp3 is offline
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
  #2 (permalink)  
Old 10-07-2007
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
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
  #3 (permalink)  
Old 10-07-2007
voa2mp3 voa2mp3 is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 3
Hi zazzybob,

Thanks for you help.

voa2mp3
  #4 (permalink)  
Old 10-07-2007
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 712
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
  #5 (permalink)  
Old 10-07-2007
radoulov's Avatar
radoulov radoulov is online now Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,892
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
Closed Thread

Bookmarks

Tags
linux commands

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:18 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0