sed X amount of times - X is dynamic

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
# 1  
sed X amount of times - X is dynamic

I'm trying to make a bash shell script that will allow a user to modify another file based on input they give. Maybe someone can see what I'm doing wrong here. I'm still pretty new at this...

Let's say my temp file contains this:
0 1 HELLO 3 4

And here's the code:
old=(0 1 3 4)
new=(zero one two three four five)

cat temp | for i in ${old[@]}; do sed "s=$i=${new[$i]}=g"; let i++; done

The output would then be redirected to a new file. The problem is, that only the first sed is ever applied. For instance, if I change the last command to allow it to echo $i:
cat temp | for i in ${old[@]}; do echo $i; sed "s=$i=${new[$i]}=g"; let i++; done

then the output looks like this:
zero 1 HELLO 3 4

The reason I'm trying to do a for/while loop instead of simply piping to sed each time, is that the 'new' array is populated by user input, and can have as many or few elements as the user desires. Any help would be appreciated.
# 2  
The first sed completely reads the input file. The second sed has no data to operate on. And there is no way to rewind a pipe. You will need to do something like:
sed < temp >output
mv output temp
each time in the loop.
# 3  
I spent all day yesterday looking for the answer in Google groups...and in one simple reply here, I got the answer! Just goes to show that the simplest answer is the hardest one to get if you don't know how to ask the right question (and in the right place). Thanks a bunch!
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #814
Difficulty: Easy
Cascading Style Sheets (CSS) is a style sheet language used for describing the presentation of a document written in a markup language like HTML.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command to replace one value which occurs multiple times

Hi, My Input File : "MN." := "MN_13_TM_4" ( 000000110110100100110001111110110110101110101001100111110100011010110111001 ) "MOS." := "MOS_13_TM_4" ( 000000110110100100110001111110110110101110101001100111110100011010110111001 ) Like above template,I have... (4 Replies)
Discussion started by: Preeti Chandra
4 Replies

2. Shell Programming and Scripting

sed add space X times

Pattern: Mary walks at the park every day with her children sed 's/$/ /' will make it add 1 space at the end (trailing) I want to add X ($VARIABLE) number of spaces (which comes from RANDOM) i.e. VARIABLE='14' then it will do: sed 's/$/ /' = 14 spaces added at the... (10 Replies)
Discussion started by: holyearth
10 Replies

3. Shell Programming and Scripting

Sed replace using same pattern repeating multiple times in a line

Sed replace using same pattern repeating multiple times in a line I have text like below in a file: I am trying to replace the above line to following How can I acheive this? I am able to do it if the occurrence is for 1 time: But If I try like below I am getting like this: I have to... (4 Replies)
Discussion started by: sol_nov
4 Replies

4. Shell Programming and Scripting

Embed cmd into sed replacement to have dynamic substitution

Hello, Is it possible to embed the command output to the replacement of sed? Say I need to replace all the blank lines with random strings: input: ACCTTCGTCTTCTGG GCTTGAGATGGTCCA GCAGGGCTAGTGACG GACGAGTCTCTTGAC ACCAAATCAAAGATCand output is: >26aa36d934d44f06d15b3aab4645a602 $(date |... (9 Replies)
Discussion started by: yifangt
9 Replies

5. UNIX for Dummies Questions & Answers

[Solved] Count amount of times of appearing of character before a word?

Hello Is there a way to calculate how many times a particular symbol appeared in a string before a particular word. Desktop/Myfiles/pet/dog/puppy So, I want to count number of occurence of"/" in this directory before the word dog lets say. Cheers, Bob (3 Replies)
Discussion started by: FUTURE_EINSTEIN
3 Replies

6. Shell Programming and Scripting

Sed or Awk for lines between two strings multiple times and keep the last one

Hi, I am trying to get lines between the last occurrences of two patterns. I have files that have several occurrences of “Standard” and “Visual”. I will like to get the lines between “Standard” and “Visual” but I only want to retain only the last one e.g. Standard Some words Some words Some... (4 Replies)
Discussion started by: damanidada
4 Replies

7. Shell Programming and Scripting

setting variable value to dynamic sed match - escaping hell

Hello All, I'm trying to write a script that will perform a dynamic match (of a dynamic variable) and set a variable to have the resulting (match) value. The idea is that the environment variable to check ($1) and the regular expression to use ($2) are given as parameters. For example,... (5 Replies)
Discussion started by: aedgar
5 Replies

8. Shell Programming and Scripting

How to use a dynamic filename with sed?

I have a line that works for static filename cat /directorypath/filename | sed '//d;//d' > filename This approach when used in a script works well. Then i need a list of filenames to give this line. I can get the list into a file by filelist1='ls -m' then use filelist2=${filelist1##ls... (4 Replies)
Discussion started by: ericonanson
4 Replies

9. Shell Programming and Scripting

How to print first two words two times using SED...

I have string as and I want to print first two words as output. (3 Replies)
Discussion started by: Diggi
3 Replies

10. Shell Programming and Scripting

sed - dynamic search and replace

Hi all, I have a data file formatted as in the following line: Achadd 0:35 1:35 2:35 3:40 4:40 5:40 I need the minutes converted to seconds; I wrote a script, min2sec, to do so for one datapoint. I was hoping to use sed as in the following code to call this script and... (4 Replies)
Discussion started by: x-375HK-x
4 Replies

Featured Tech Videos