Replace a string in files in all dir and sub dirs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace a string in files in all dir and sub dirs
# 1  
Old 03-16-2018
Replace a string in files in all dir and sub dirs

Hello,

I need to replace xml version='1.1' with xml version='1.0' in all xml files under /app/jenkins/ in all dir and sub dirs in my CentOS VM, I tried below command but it didn't help, looks like I'm missing a character somewhere.

Code:
grep -rl "xml version='1.1'" . | xargs sed -i 's/"xml version=\'1\.1\'"/"xml version=\'1\.0\'"/g'

# 2  
Old 03-16-2018
Code:
grep -rl "xml version='1.1'" . | xargs sed -i 's/xml version='\'1\.1\''/xml version='\'1\.0\''/g'

Note that if no files are returned from the grep, sed will print: sed: no input files
# 3  
Old 03-16-2018
Except for the recursivity with the -r option, I presume you use grep for the benefit of reducing file I/O as
Quote:
The scanning will stop on the first match.
(c.f. man grep) with the -l option. While for large files with the search pattern towards the top, this is worthwhile, it is not if you have many small files, which you then have to open twice, once for grep, and once for sed.
Try

Code:
sed "/xml version='1\.1'/ s//xml version='1.0'/g" *

as within double quotes, single quotes lose their special meaning.

Last edited by RudiC; 03-16-2018 at 10:30 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find all .sh files in file system and need to replace the string inside .sh files

Hi All, I need to write a script to find all "*.sh" files in /home file system and if any string find "*.sh" files with the name vijay@gmail.com need to replace with vijay.bhaskar@gmail.com. I just understood about the find the command to search .sh files. Please help me on this. find / -name... (3 Replies)
Discussion started by: bhas85
3 Replies

2. AIX

Assign read write permission to the user for specific dir and it's sub dir and files in AIX

I have searched this quite a long time but couldn't find the right method for me to use. I need to assign read write permission to the user for specific directories and it's sub directories and files. I do not want to use ACL. I do not want to assign user the same group of that directories too.... (0 Replies)
Discussion started by: blinkingdan
0 Replies

3. Shell Programming and Scripting

KSH - Find paths of multiple files in CC (dir and sub-dir))

Dear Members, I have a list of xml files like abc.xml.table prq.xml.table ... .. . in a txt file. Now I have to search the file(s) in all directories and sub-directories and print the full path of file in a output txt file. Please help me with the script or command to do so. ... (11 Replies)
Discussion started by: Yoodit
11 Replies

4. OS X (Apple)

how to find a string in file under multiple level dirs

Hi, i am asking a command to find a string in file(s) from multiple level directory structures. help would be really appreciated. (4 Replies)
Discussion started by: ywu081006
4 Replies

5. UNIX for Dummies Questions & Answers

How to list all files in dir and sub-dir's recursively along with file size?

I am very new to unix as well as shell scripting. I have to write a script for the following requirement. In have to list all the files in directory and its sub directories along with file path and size of the file Please help me in this regard and many thanks in advance. (3 Replies)
Discussion started by: nmakkena
3 Replies

6. Shell Programming and Scripting

find string from multiple dir and redirect to new files

Hi, I am new to script and I want find one string from multiple files in diff directories and put that out put to new file. Like I have A,B & C directories and each has multiple files but one file is unic in all the directories like COMM.txt Now I want write script to find the string... (8 Replies)
Discussion started by: Mahessh123
8 Replies

7. Shell Programming and Scripting

A script to find dir, delete files in, and then del dir?

Hello!! I have directories from 2008, with files in them. I want to create a script that will find the directoried from 2008 (example directory: drwxr-xr-x 2 isplan users 1024 Nov 21 2008 FILES_112108), delete the files within those directories and then delete the directories... (3 Replies)
Discussion started by: bigben1220
3 Replies

8. Shell Programming and Scripting

replace string in multiple files, dir and subdir

Hello, I have a directory www with multiple directories. Every directory has site name with .htm, .html, .php files or sub directories with .htm, .php, .html file as example - www - sitename 1 - site 1 - sitename 2 - sitename 3 What I'm looking for is a... (7 Replies)
Discussion started by: andyjill
7 Replies

9. Shell Programming and Scripting

replacing a string in all files in a dir

Hello guys, I need help in globally replacing a string 'string1' with 'string2' in several files in a directory. In fact, also in all directories under it. Can anyone help me... Thanks (3 Replies)
Discussion started by: chiru_h
3 Replies

10. UNIX for Dummies Questions & Answers

using grep to find a value in current dir and sub dirs?

Hey guys, I would like to find all files which contain "client1.dat". I would like to search from the current directory and all subs and print out all the files that have this. Any help would be greatly appreciated. Thanks much. (10 Replies)
Discussion started by: ecupirate1998
10 Replies
Login or Register to Ask a Question