Visit Our UNIX and Linux User Community


xargs command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting xargs command
# 8  
Old 06-09-2011
your code is missing {} after -I see below :-

Code:
echo archDP105144_1_702159963.dbf| xargs -I{} cp {} `echo {}|awk '{sub(/DP1/,"TML");print}'`

BR
# 9  
Old 06-09-2011
if there is only one file, using xargs is not optimal
you could just

Code:
cp archDP105144_1_702159963.dbf `echo archDP105144_1_702159963.dbf | awk '{sub(/DP1/,"TML");print}'`

or

Code:
f=archDP105144_1_702159963.dbf; cp $f "${f%%DP1*}TML${f##*DP1}"

# 10  
Old 06-09-2011
how about something simpler and w/o xargs...
Code:
ls -1 archDP1* | awk '{f=$0;sub("DP1", "TML");system("cp "f" "$0)}'

# 11  
Old 06-09-2011
Quote:
Originally Posted by zam
Hi

The command below does not work. what I am doing wrong ?
For some reason second part of the xargs command is not does what I expect
If I get this working I intend to use it for multiple file rename later.
Code:
echo archDP105144_1_702159963.dbf|xargs -i cp {} `echo {}|awk '{sub(/DP1/,"TML");print}'`

with set -x
Code:
echo archDP105144_1_702159963.dbf|xargs -i cp {} `echo {}|awk '{sub(/DP1/,"TML");print}'`
++ echo '{}'
+ echo archDP105144_1_702159963.dbf
++ awk '{sub(/DP1/,"TML");print}'
+ xargs -i cp '{}' '{}'

Thanks
Quote:
Originally Posted by ahmad.diab
your code is missing {} after -I see below :-

Code:
echo archDP105144_1_702159963.dbf| xargs -I{} cp {} `echo {}|awk '{sub(/DP1/,"TML");print}'`

BR

{} is not expanded by the shell. It is expanded by xargs after it starts executing. However, xargs cannot be exec'd until all of its command line arguments are known. This requires the shell to invoke a subshell to perform the command subsitution in backticks. Note that at this point xargs is not running. echo {} will always yield nothing but {}. In AWK, the sub() function has nothing to do since there's no DP1 to replace, and the output is the same as what's echoed. Then the shell, finally knowing the result of the command substitution, can execute xargs. xargs will then always be passed {} {} as its final two parameters.

In short, this approach cannot be made to work.

I suggest ctsgnb's suggestion:
Quote:
Originally Posted by ctsgnb
Code:
f=archDP105144_1_702159963.dbf; cp $f "${f%%DP1*}TML${f##*DP1}"



Regards,
Alister

Last edited by alister; 06-09-2011 at 01:35 PM..
This User Gave Thanks to alister For This Post:
# 12  
Old 06-10-2011
Thankyou ALL!Smilie

---------- Post updated at 05:07 PM ---------- Previous update was at 04:51 PM ----------

The code I ended up with!
Code:
#!/bin/ksh
for i in *.dbf
do
    echo
    echo "... renaming $i to ${i/DP1/TML}"  
    echo -e "y - continue / n - Skip / Ctrl+C - abort  --> \c"
    read ans
     if [ "$ans" = "y" ]; then
       mv $i ${i/DP1/TML}
     else
       echo skipping $i
     fi
done


Last edited by Franklin52; 06-10-2011 at 06:17 AM.. Reason: Please use code tags

Previous Thread | Next Thread
Test Your Knowledge in Computers #929
Difficulty: Medium
Vernor Vinge's novel A Deepness in the Sky describes a spacefaring trading civilization thousands of years in the future that still uses the Unix epoch.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Xargs -P command execution status

Hi, I am working on a file copier utility where I have written the copy commands to a batch file e.g. file_copier.bat which i pass to xargs command as follows. cat file_copier.bat | xargs -n 1 -P 40 I also want to record the copy command status of each file/command in the form "command... (1 Reply)
Discussion started by: ankur singh
1 Replies

2. Shell Programming and Scripting

Looking for XARGS command Silent options

ls | grep -E '^+$' | xargs --verbose -I{} rm -vfr "{}"; When i execute the command it works fine by removing the directories and its writing the output as below about which files are deleting.What i want know is,is there any XARGS command option that it should done silently in background with... (2 Replies)
Discussion started by: nareshreddy443
2 Replies

3. Shell Programming and Scripting

Pipe output a command to another using xargs

xargs work great when a command gives multiple line output which can be input to another. In my case it is not working coz the second command uses two words in it. $ scr.sh gives output like 193740 638102 375449 .. .. another command takes these number as inputs. it works great... (1 Reply)
Discussion started by: mahesh113
1 Replies

4. Shell Programming and Scripting

need help with xargs command..

ls -lrt | awk '$7==12{print $9}' | xargs -i mv {} $dir i executed this command but $dir does not exists....... and the files hv been moved but i dont know where ..... plz help(ASAP) thanks in advance. (8 Replies)
Discussion started by: guptam
8 Replies

5. Shell Programming and Scripting

Doubt in xargs command

Hi, What is the difference in capitalizing the option 'i' of xargs command, (i.e) xargs -i and xargs -I? Also, what is the difference between the below 2 commands? output_from_cmd | xargs -I {} grep '{}' file output_from_cmd | xargs -I grep '{}' file Any efficiency or performance... (4 Replies)
Discussion started by: royalibrahim
4 Replies

6. Shell Programming and Scripting

xargs command problem

Hi I am trying to use "xargs" command to loop through each file, modify it and overwrite the old file with the modification but with the same file name. I thought it is easy but I just can't get it to work I tried the following I thought {} would give me the current file name, but it... (1 Reply)
Discussion started by: tiger66
1 Replies

7. UNIX for Dummies Questions & Answers

querry about xargs command

what is the real use of xargs command ..? ls -tr |xargs -I{} rm -f {} ....can any one tell me what is the significance of {} curly brackets in this command (1 Reply)
Discussion started by: mobydick
1 Replies

8. AIX

command usage on find with xargs and tar

my task : tar up large bunch of files(about 10,000 files) in the current directories that created more than 30 days ago but it come with following error find ./ -ctime +30 | xargs tar rvf test1.tar tar: test1.tar: A file or directory in the path name does not exist. (3 Replies)
Discussion started by: darkrainbow
3 Replies

9. Shell Programming and Scripting

Xargs command outupt to file

Hello, I'm on a mac trying to have the follow cmd list the files after touch, but very unsuccessful. Please can you help. sort $BOTHFILE | uniq -u | xargs -I {} -t touch {} >> $LOGFILE ; BOTHFile contents in form of /directory/file.txt thanx (3 Replies)
Discussion started by: byblos
3 Replies

10. UNIX for Dummies Questions & Answers

use of xargs command

Hi Everybody, Can you explain the difference between the following commands: 1. find . -print|xargs grep -i dba_2pc_pending 2. find . -print|grep -i dba_2pc_pending (5 Replies)
Discussion started by: kishorebabu
5 Replies

Featured Tech Videos