Changing a string in multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Changing a string in multiple files
# 1  
Old 11-16-2010
Changing a string in multiple files

New to scripting and unix. Would like to know how to change a string in a file. eg:
From
network=/usr/spool/progs/
to
network=/spool/progs/

In multiple files.
# 2  
Old 11-16-2010
Code:
 
find <dir_path> -type f -print | while read file
do
       sed -i 's,/usr/spool/progs/,/spool/progs/,g' $file
done

This User Gave Thanks to anurag.singh For This Post:
# 3  
Old 11-16-2010
Considering the input files are "*.txt" and the output will be generated with a new file as "filename.txt.new".

The below code will search for files from current position recursively. if you want to searchthe full mounts, please change the "." to "/" in the find command.


Code:
for files in `find . -name "*.txt" | xargs grep -l "network=/usr/spool/progs/"`
do
        echo "$files"
        sed 's,network=/usr/spool/progs/,network=/spool/progs/,g' $files > $files.new
done

This User Gave Thanks to palsevlohit_123 For This Post:
# 4  
Old 11-16-2010
alternative with sed
Code:
echo ${network}|sed 's/^\/[^/]*\(.*\)/\1/'
or 
netw=$(echo ${network}|sed 's/^\/[^/]*\(.*\)/\1/')

This User Gave Thanks to michaelrozar17 For This Post:
# 5  
Old 11-16-2010
Thank you guys for the quick replies.

It worked. I used all methods. I first copied files to test environment, to play? Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Changing cases of multiple files in different folders

I am a TA for a computer science course and we have a program that flags code that appears to the same as other code that is supplied. In order for the program to check all of the student's programs, the programs all need to be named exactly the same. However, students don't like to follow... (12 Replies)
Discussion started by: bpmawhin
12 Replies

2. UNIX for Dummies Questions & Answers

Rename multiple files in shell bash, changing elements order.

Hi, I want to rename several files like this: example: A0805120817.BHN A0805120818.BHN ..... to: 20120817.0805.N 20120818.0805.N ...... How can i do this via terminal or in shell bash script ? thanks, (6 Replies)
Discussion started by: pintolcv
6 Replies

3. Shell Programming and Scripting

Retreiving multiple files by changing a parameter with one program

Hi all, I am using following command: perl program.pl input.txt output.txt CUTOFF 3 > groups_3.txt containing program.pl, two files (input.txt, output.txt) and getting output in groups_3.txt: But, I wish to have 30 files corresponding to each CUTOFF ranging from 0 to 30 using the same... (1 Reply)
Discussion started by: bioinfo
1 Replies

4. UNIX for Dummies Questions & Answers

Rename multiple files, changing prefix, extension and dropping characters

I'm currently only able to perform some very basic functions, so hope this makes sense... I have a set of about 27 files that need to be renamed from something like this: 000012ABCDEFGHIJ.XXX.YYY.ZZZ 000078KLMNO.XXX.YYY.ZZZ 000099PQ.XXX.YYY.ZZZ to something like this: newa012.abc... (11 Replies)
Discussion started by: bbmcg
11 Replies

5. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 Replies

6. Shell Programming and Scripting

Renaming multiple files, specifically changing their case

Hey all, I'm running a BASH shell and I was wondering if anyone knows a code to rename all the files in a directory. All I need to do is change the first character in the file from uppercase to lowercase and some of the files are already lowercase so they don't need to change. -Thanks (4 Replies)
Discussion started by: jjzieve
4 Replies

7. Shell Programming and Scripting

How to find particular string in multiple files

Hello friends, I have find a paticular string from the files present in my user for example: a username and password is hardcoded in multiple files which present in the my user.so I have to search about username in which files it is available.there are several dirctories are there,so... (5 Replies)
Discussion started by: sivaranga001
5 Replies

8. Shell Programming and Scripting

Create Multiple files by reading a input file and changing the contents

Being new to this area .I have been assigned a task which i am unable to do . Can any one please help me . Hi I have requirement where i have input file XYZ_111_999_YYYYMMDD_1.TXT and with header and series of Numbers and Footer. I want to create a mutiple output files with each file having a... (2 Replies)
Discussion started by: bhargavkr
2 Replies

9. Shell Programming and Scripting

Multiple search string in multiple files using awk

Hi, filenames: contains name of list of files to search in. placelist contains the names of places to be searched in all files in "filenames" for i in $(<filenames) do egrep -f placelist $i if ] then echo $i fi done >> outputfile Output i am getting: (0 Replies)
Discussion started by: pinnacle
0 Replies

10. UNIX for Dummies Questions & Answers

changing extensions for multiple files at once

I copied some files to another folder, and I want to change them from .doc extensions to .txt extensions. I tried using the cp and mv commands, but it didn't work. Is it possible to change file extensions with these commands, and if so how do I do it? I tried using the * wildcard (say cp *.doc... (1 Reply)
Discussion started by: Straitsfan
1 Replies
Login or Register to Ask a Question