Pattern Replacement


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Pattern Replacement
# 8  
Old 04-25-2008
I can't understand why "find ... | xargs ..." is so widespread. "find" has a "-exec" clause as long as i can think (and that is pretty long), so why not use it?

Code:
find / -type f -exec fTgt={} ; sed 's/calcuta/kolkata/g' $fTgt > ${fTgt}.tmp ; mv ${fTgt}.tmp ${fTgt} \;

I hope this helps.

bakunin
# 9  
Old 04-25-2008
Very good point, but -exec does have an issue, better explained in this extract taken from softpanorama.org:

Quote:
Feeding find output to pipes with xargs

One of the biggest limitations of the -exec option (or predicate with the side effect to be more correct) is that it can only run the specified command on one file at a time. The xargs command solves this problem by enabling users to run a single command on many files at one time. In general, it is much faster to run one command on many files, because this cuts down on the number of invocations of particular command/utility.
Link: softpanorama.org/Tools/Find/find_mini_tutorial.shtml


Also unixreview.com has a nice article on this regard ( Examining the "Too Many Arguments" Problem ), and on xargs in general, as a powerful unix tool.

Link: unixreview.com/documents/s=8274/sam0306g/


Hope you'll find them useful.
# 10  
Old 04-25-2008
The same result can be achieved without the pipe or xargs if the find command on a systems supports the
Code:
-exec <command> +

syntax.
# 11  
Old 04-26-2008
In this particular context you extrapolate the individual file name into the command anyway, so the multiple files at a time argument doesn't hold.

Personally, I simply find xargs easier to develop and debug. Also find's -exec syntax is kind of awkward. Constructing a command line and piping it to sh is also kind of a lazy man's solution, but often easy to write and understand.
# 12  
Old 04-26-2008
Are you sure about that, wouldn't that depend what exactly you do for the command?

Code:
       -exec command {} +
              This variant of the -exec option runs the specified  command  on
              the  selected  files, but the command line is built by appending
              each selected file name at the end; the total number of  invoca-
              tions  of  the  command  will  be  much  less than the number of
              matched files.  The command line is built in much the  same  way
              that  xargs builds its command lines.  Only one instance of {}
              is allowed within the command.  The command is executed  in  the
              starting directory.

of example:
Code:
find / -type f -exec perl -pi -e 's/calcuta/kolkata/g' {} +

or with gnu sed:

Code:
find / -type f -exec sed -i 's/calcuta/kolkata/g' {} +

# 13  
Old 04-26-2008
Fair enough, you win (and this was news to me; hadn't stumbled over -exec + before -- thanks for the tip!)
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

GUI for multiple pattern replacement

I'm trying to change a few programs in our environment. Basically We have hardcoded some server names and stuff, So I want some one to suggest me some UNIX gui tools that can be used to replace these.. I really don't want to deal doing this through the command line. I want to transfer the files... (3 Replies)
Discussion started by: sudden
3 Replies

2. Shell Programming and Scripting

Help with Pattern Matching and replacement in Gz files

Hi Techies, I need a help in finding junk characters and remove them from a Datafile. we have a file and it had crores of records like below SGSN_MCC_MNC=01150 but sometime due to the issue with sending server we are getting some junk characters in the middle of data like below ... (6 Replies)
Discussion started by: mahi_mayu069
6 Replies

3. Shell Programming and Scripting

Pattern Matching and replacement

Hello Everybody, I need a help in the below pattern matching and replacement issue I have a file : emp.txt 21356 suresh 12/12/2012 23511 ramesh 11/06/2011 31456 biswajit 09/08/2013 53134 archan 06/02/2009 first field:- employee id, 2nd field is name and third field is date of joining ... (10 Replies)
Discussion started by: shellscripting
10 Replies

4. Shell Programming and Scripting

Unix file pattern check and replacement

HI Guys , Using UNIX ,I intend to check with correct file pattern Access_file_Record.YYYYMM in path /tmp If the file exist in correct format come out from code . If not found check with different file patterns for same month period YYYYMM ( Like Access_file_Record_YYYYMM.txt or... (8 Replies)
Discussion started by: Perlbaby
8 Replies

5. Shell Programming and Scripting

sed noob needs help with replacement pattern.

Hi there, i am absolutely new in shell programming and especially using sed. What i want to do is to replace every emailaddress suffix with another. In my Testfile there is: foo@bar.comMy attempt to replace every @<something>.<someotherthing> is: sed 's/@+\.+/REPLACE/g' test.txt... (5 Replies)
Discussion started by: Donngal
5 Replies

6. Shell Programming and Scripting

Sed for selective pattern replacement

Hi I am having a code snippet grant permission to all user sts|ln|uSe|PSG sajncht|se|Use|PPSPSG psg|ln|use|TSPSG sts_user.Me revoke I need to change all occurance of use (uSe,Use,use) with USE. I am using the following sed command for this sed 's//USE/g' s_sample.txt Output: (7 Replies)
Discussion started by: sudeep.id
7 Replies

7. Shell Programming and Scripting

String replacement when particular pattern matches in a file

I have a file file123.xml which looks like this xmlEntry="username"="josh" <property="never_back_down"> phone="<178652>" apn=property:address="wonderland" xmlEntry="username"="jessica" <property="never_back_down"> phone="<178653>" apn=property:address="wonderland"... (5 Replies)
Discussion started by: poga
5 Replies

8. Shell Programming and Scripting

Date Pattern Match (replacement string)

Hello, i am splitting files and sometimes the string of the pattern doesnt exist in the input file it starts for example with 00:00:01. So the output is completely desorganized, is there any way of putting a replacement string in the pattern so it will grab all the times from 00:**:** to first... (0 Replies)
Discussion started by: x-plicit78
0 Replies

9. UNIX for Dummies Questions & Answers

awk pattern replacement

Hi I'm a newbie in unix and I'm having trouble in creating a script. I want to search for a pattern '_good' and insert new lines that contains '_bad', '_med', '_fail' while also ensure that the line contains _good is removed here some of the data UPDATE SCHOOL SET GRADE =... (1 Reply)
Discussion started by: sexyTrojan
1 Replies

10. Linux

matching pattern and replacement

Hi I am trying to look for a view name in create view statement and then replace this view name with VW_ in grants line in my ddl file . cat dim_provider.sql | grep -i "create view" | while read f1 f2 f3 f4 f5 f6 f7 f8 f9 do new_vw=` echo "$f3" | cut -d "." -f2... (32 Replies)
Discussion started by: capri_drm
32 Replies
Login or Register to Ask a Question