Help getting a UNIX shell script to look at multiple files.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help getting a UNIX shell script to look at multiple files.
# 1  
Old 12-05-2013
Help getting a UNIX shell script to look at multiple files.

Hey everyone! I made a shell script that would go through a file and replace any phrase or letter with another phrase or letter. Helps update variable names or values. The following code is this:

Code:
#!/bin/sh

Word1="$1"
Replace1="$2"
File1="$3"

arg=$( echo "$Word1" | sed 's:[]\[\^\$\.\*\/]:\\&:g' )
sed -e "s/$arg/$Replace1/g" "$File1" > "$File1.updated"
mv "File1.updated" "$File1"

This works. Now I am trying to get it to do the same thing but for up to three files, and am running into problems.

I tried adding:
Code:
File2="$4"
File3="$5"

to cover the two additional files. However, how would I create a loop where it uses the above and then does it on the other two files?

The other thing I tried was adding the code after the first part:
Code:
while ["$File2" > 0];
do
sed -e "s/$arg/$Replace1/g" "$File2" > "$File2.updated"
mv "File2.updated" "$File2"
done

but that isn't working either. Any help?

Last edited by Don Cragun; 12-05-2013 at 11:15 PM.. Reason: Add CODE tags.
# 2  
Old 12-05-2013
Try using a while loop with shift to move the arguments down one:

Code:
#!/bin/bash

arg=$( echo "$1" | sed 's:[]\[\^\$\.\*\/]:\\&:g' )
Replace1="$2"

while [ $# -ge 3 ]
do
    File1="$3"
    sed -e "s/$arg/$Replace1/g" "$File1" > "$File1.updated"
    mv "$File1.updated" "$File1"
    shift
done

# 3  
Old 12-06-2013
You could use a for loop, again using shift to bypass the first two values. Reasonably keeping to your original design:-
Code:
#!/bin/sh

Word1="$1"
Replace1="$2"
arg=$( echo "$Word1" | sed 's:[]\[\^\$\.\*\/]:\\&:g' )

shift 2             #  Drop first two arguments and move the others along

for File1 in $*     # Loop for all remaining arguments, i.e. the file names
do
   sed -e "s/$arg/$Replace1/g" "$File1" > "$File1.updated"
   mv "$File1.updated" "$File1"
done

This should run for however many files you list.



I hope that this helps,
Robin
Liverpool/Blackburn
UK
# 4  
Old 12-06-2013
Quote:
Originally Posted by rbatte1
You could use a [ICODE][..]

for File1 in $* # Loop for all remaining arguments, i.e. the file names
do
[..][/CODE][..]
That should be "$@", or use for File1 do
Compare:
Code:
set -- 1 2 "3 4" 5 "6 7 8"
$ for i in $*; do
> echo "$i"
> done
1
2
3
4
5
6
7
8
$ for i in "$@"; do echo "$i"; done
1
2
3 4
5
6 7 8
$ for i ; do echo "$i"; done
1
2
3 4
5
6 7 8

# or one can even use:
$ for i do echo "$i"; done
1
2
3 4
5
6 7 8

This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Export Oracle multiple tables to multiple csv files using UNIX shell scripting

Hello All, just wanted to export multiple tables from oracle sql using unix shell script to csv file and the below code is exporting only the first table. Can you please suggest why? or any better idea? export FILE="/abc/autom/file/geo_JOB.csv" Export= `sqlplus -s dev01/password@dEV3... (16 Replies)
Discussion started by: Hope
16 Replies

2. Homework & Coursework Questions

Shell Script to sed with Multiple Files

I am not sure what I am doing wrong here. The code should work fine. I have been making small changes insuring that each new bit works. Now running my sed through multiple files I am getting incorrect output. Any help and instruction would be greatly appreciated. The problem - Generalize... (10 Replies)
Discussion started by: Lycopene
10 Replies

3. Shell Programming and Scripting

shell script to ftp multiple files

Hi, i use the below script to send a single file to remote server from linux. ftp -nvi <<!EOF open $Host_name user $USER_ID $PWD binary mput $file_name quit !EOF (where i... (2 Replies)
Discussion started by: pradebban
2 Replies

4. Shell Programming and Scripting

Renaming multiple files with a shell script

Hey guys, I'm really new to UNIX and shell scripting in general. For my internship I need to rename a bunch of files. Specifically, I need to change the first letter of each of the files to lowercase and I have to change the endings so they all basically look like "file_cone.jpg". I know I... (4 Replies)
Discussion started by: jjzieve
4 Replies

5. UNIX for Advanced & Expert Users

Multiple Instance of Unix Shell Script

Hi All, I have a problem mentioned below. I have a script which performs line by line operations on several files. I have a temp_file storing the list of names of the file to be validated. Right not in while loop i validate these files one by one. Is there anyway that i can modify... (1 Reply)
Discussion started by: amitaryans
1 Replies

6. Shell Programming and Scripting

Need shell script for ftpying multiple files

Hi All, I am a beginner for shell programming. I have a requirement to ftp multiple files. Here are the details. I have around thiry files in one directory, I want a shell script which selects 5 files at a time and does ftp them to another host . After the transfer for first files is... (0 Replies)
Discussion started by: srikanthn
0 Replies

7. Shell Programming and Scripting

How to Read Multiple files in a Shell Script

Hi, Can any one tell me if i can read two files in a shell script... My actual requirement is to read the 1st text file and parse it to get the file code and use this file code to retrieve data from database and print the fetched data in the 2nd text file (I have parsed it and printed the... (2 Replies)
Discussion started by: funonnet
2 Replies

8. Shell Programming and Scripting

Need help for a Shell script to rename multiple files

Hi! I need help to create a shell script to search inside a file and then copy a portion of the search result as the new file name. Basically I was hacked over the weekend and the genius wipe out my drive from my server. I was able to recover alot of files, but biggest problem Is now the... (15 Replies)
Discussion started by: kidney514
15 Replies

9. Shell Programming and Scripting

shell script to join multiple files

I am a new to Linux and try to write a script to join three multiple files. For example, there are three files file1 # comment a Kevin b Vin c Sam file 2 # comment a 10 b 20 c 40 file 3 # comment a blue b yellow (7 Replies)
Discussion started by: bonosungho
7 Replies

10. Shell Programming and Scripting

how to send multiple files from the shell script

hi, how to send multiple files from the shell script eg : i have /home/adm/file1 /home/adm/file2 /home/adm/cfg how can i attach these files in the mail ? (1 Reply)
Discussion started by: mail2sant
1 Replies
Login or Register to Ask a Question