Sponsored Content
Homework and Emergencies Homework & Coursework Questions Shell Script to sed with Multiple Files Post 302878375 by Lycopene on Friday 6th of December 2013 12:13:05 AM
Old 12-06-2013
Okay, I changed the code to
Code:
#!/bin/sh

p1="$1"
shift
p2="$1"
shift

for file in *
do
      echo $p1
      echo $p2

      sed "s/$p1/$p2/g" $file >temp.out
      mv temp.out $file
done

And received the error
Code:
sub3 appears to be treating its first parameter as the name of a file to be edited.

for test 28.

So then I changed my script to -
Code:
#!/bin/sh

p1="$1"
shift
p2="$1"
shift
p3="$1"
shift


for file in *
do
      echo $p1
      echo $p2
      echo $p3

      sed "s/$p1/$p2/g" "$p3" > temp.out
      mv temp.out "$p3"
done

Which passes test 28, but fails test 29 and does not address what you were saying.

How would I go about referring to file? Setting $p3=file while inside the loop? The one example in the slides we have is
Code:
for file in *
do
  echo Looking at $file.
  count=`expr $count + 1`
  echo Count is $count.
done

And it uses that snippet repeatedly.

And the failed test output is -
Code:
sub3 produced incorrect output on test 29: /directory/sub3 'e' 'x' '_bongo.dat' '_cat.dat'

I think I see what is wrong here, having 3 argument parameters and it giving four, but how do I account for X number of files?
 

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Sed inside bash script - accessing multiple files

I have a file (email) containing email addresses. I have a second file (terms) that contains simple regular expressions and words/characters. Here are some examples: \.trainee \.group \.web I want to go through email and delete lines containing the expressions/words from terms and write... (1 Reply)
Discussion started by: manouche
1 Replies

3. 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

4. Shell Programming and Scripting

Split line to multiple files Awk/Sed/Shell Script help

Hi, I need help to split lines from a file into multiple files. my input look like this: 13 23 45 45 6 7 33 44 55 66 7 13 34 5 6 7 87 45 7 8 8 9 13 44 55 66 77 8 44 66 88 99 6 I want to split every 3 lines from this file to be written to individual files. (3 Replies)
Discussion started by: saint2006
3 Replies

5. 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

6. 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

7. 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

8. Shell Programming and Scripting

Help with Shell Scripts Using sed in multiple files.

Hi, I was hoping that someone could help me. I have a problem that i am trying to work on and it requires me to change text within multiple files using sed. I use the program to change an occurance of a word throughout different files that are being tested. At first i had to Create a new script,... (1 Reply)
Discussion started by: Johnny2518
1 Replies

9. Homework & Coursework Questions

Help with Shell Scripts Using sed in multiple files.

Hi, I was hoping that someone could help me. I have a problem that i am trying to work on and it requires me to change text within multiple files using sed. What i have so far is !/bin/sh File1="$3" File2="$4" File3="$5" testNum="$File1" while test "$testNum" <= "$File3"; do echo... (12 Replies)
Discussion started by: Johnny2518
12 Replies

10. Shell Programming and Scripting

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: #!/bin/sh Word1="$1" Replace1="$2" File1="$3" arg=$( echo "$Word1" | sed 's:\:\\&:g' )... (3 Replies)
Discussion started by: rebmonk
3 Replies
All times are GMT -4. The time now is 09:00 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy