How to make an editing script work for multiple files?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to make an editing script work for multiple files?
# 1  
Old 05-04-2010
How to make an editing script work for multiple files?

Hey everybody, I have a script for making a string substitution in a file. I am trying to modify it in order to make the same modifcation to multiples files. here is what I have so far.

Code:
#!/bin/csh
set p1="$1"
shift
set p2="$1"
shift
foreach x ($*)
if ( { grep -w -c "$p1" $x } ) then
mv $x $x.bak
sed "s/$p1/$p2/g" $x.bak > $x
else
endif
end

any help would be great. Thanks in advance!

---------- Post updated 05-04-10 at 12:17 PM ---------- Previous update was 05-03-10 at 04:40 PM ----------

help bump

Last edited by Scott; 05-03-2010 at 05:47 PM.. Reason: Code tags, please...
# 2  
Old 05-04-2010
Provide the input and the output you are expecting ..would help us to understand this.
# 3  
Old 05-04-2010
surely, say for example I wanted to replace "hi guys" with "hello everyone" in a bunch of files to sound more proper, I should be able to use the command

~/Unix/script/subst "hi guys" "hello everyone" myFile1.txt myFile2.txt myFile3.txt

and I want it to make that replacement in everyfile. my code works for just one file, but I am having some trouble manipulating the loop to make it work for multiple files.
# 4  
Old 05-04-2010
Something like this:

Code:
 
perl -i -pe 's/hi guys/hello everyone/i ;' myFile*

# 5  
Old 05-04-2010
well I'm trying to not use any perl because I am not too familiar with it. Thanks for the help though. the script should work for any target and replacement though with this line

Code:
 
sed "s/$p1/$p2/g" $x.bak > $x

I am trying to get it to work for multiple "x" files but am still stuck.
# 6  
Old 05-05-2010
Ok , a bit modification to your script will work i hope.
Code:
 
for file_name in `ls`
do
sed "s/$p1/$p2/g" $file_name > $file_name".new"
mv $file_name".new" $file_name
done

# 7  
Old 05-06-2010
Still not getting it to work 8 ( , this is driving me crazy lol
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. SuSE

How To make bootable USB with multiple ISO Files?

Hi All, I would need your assistance to make a bootable USB with SUSE LINUX Enterprise Server I have already downloaded relevant OS (Trail Version) packages @ 1) SLES-11-SP4-DVD-i586-GM-DVD1 2) SLES-11-SP4-DVD-i586-GM-DVD2 when I tried to open these packages with PowerISO one of the... (7 Replies)
Discussion started by: Leaner_963
7 Replies

2. Shell Programming and Scripting

Bash script deleting my files, and editing files in subdirectories question

#!/bin/bash # name=$1 type=$2 number=1 for file in ./** do if then filenumber=00$number elif then filenumber=0$number fi tempname="$name""$filenumber"."$type" if (4 Replies)
Discussion started by: TheGreatGizmo
4 Replies

3. UNIX for Dummies Questions & Answers

Editing multiple files with vi, why :p is not working

Hi, Can any gurus advise why :p is not working on my vi? When editing multiple files, :n works and it takes me the next file, but :p which is supposed to take me back to the previous file does not work. Please advise. Thanks in advance. (2 Replies)
Discussion started by: newbie_01
2 Replies

4. Shell Programming and Scripting

Make multiple awk files into an executable

Hello everyone, The following are my input files. The following are my sequence of steps. Can someone please let me know about how to make these bunch of steps into a single script so that I start the script with 1.txt and 2.txt, after execution gives me the final... (11 Replies)
Discussion started by: jacobs.smith
11 Replies

5. Shell Programming and Scripting

Make multiple files of equal length

I have 150 files with 4 columns each but variable row lengths that I need to combine by column. I do not have any common column. I want to use "paste " command in unix to do it but before that I have to get all my files to be of equal length. Is there a way using awk or sed to fill up n no. of... (7 Replies)
Discussion started by: manishabh
7 Replies

6. Shell Programming and Scripting

editing names of files in multiple folder

I have 1000's of directories which is named as numbers. Each directory contains multiple files. Each of these directories have a file named "att". I need to rename all the att files by adding the directory name followed by "_" then att for each of the directories. Directories 120 att... (2 Replies)
Discussion started by: Lucky Ali
2 Replies

7. Shell Programming and Scripting

How to get this script work on multiple input files

Hello Gyues! I would like to use awk to perform data extraction from several files. The data files look like this: DWT26R 1 PEP1 CA 1 OH2 SKIPPED: 0 STEP: 1 0.29000E+01 0.55005E-02 0.60012E-03 0.30000E+01 0.11149E+00 0.13603E-01 0.31000E+01 0.39719E+00 0.63013E-01 0.32000E+01... (9 Replies)
Discussion started by: Daniel8472
9 Replies

8. Shell Programming and Scripting

for loop, calling and editing multiple files inside

hey guys, I'm trying to call and modify multiple files inside the for loop, i can't get it to work... ------------------------ AFILE=/dir/a_file.txt BFILE=/dir/b_file.txt CFILE=/dir/c_file.txt ADESTFILE=/dir/a_dest_file.txt BDESTFILE=/dir/b_dest_file.txt... (6 Replies)
Discussion started by: DeuceLee
6 Replies

9. Solaris

editing files with script

hi guys, We have to implement new local (/etc/default/login) USER security policy on almost 50 stations. so editing /etc/default/login and /etc/default/passwd will be way too long work. Can we do the same using some script, I mean editing the above files and putting variables as RETRIES=3, ... (5 Replies)
Discussion started by: Asteroid
5 Replies

10. UNIX for Dummies Questions & Answers

Editing one string in multiple files

I am trying to edit multiple files from one directory and including all the files in all the sub directories. My string opens each file, puts the text on my screen and does not save the new information to the file. I am using a variable in my script, and wondering if that is what is choking it. ... (1 Reply)
Discussion started by: Skoshi
1 Replies
Login or Register to Ask a Question