Script to replace a string with pattern read from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to replace a string with pattern read from a file
# 1  
Old 03-08-2019
Script to replace a string with pattern read from a file

I have two files blocks.txt and rules.txt. In blocks.txt i have the following entries
Code:
Linux1
Linux2
Linux3
.....
Linux10

In rules.txt i have the lines where a filename pattern starts like
Code:
'blk-name.*'

I want to replace 'blk-name' with the names read from blocks.txt file
I tried with following code.But it replaces with "$blocks".Could anyone pl. help me correct this
Code:
#!/usr/bin/ksh

for blocks in $(cat blocks.txt)
do
BlktoAdd=$blocks
cat rules.txt|while read line
do
sed -i 's/blk_name/$blocks/'
done
done

# 2  
Old 03-08-2019
Try double quotes in lieu of single quotes to enable shell's "variable expansion".


EDIT: But, there's more to comment on. Your sed command doesn't have a file to operate on, the goal of the script is not quite clear, and in general there are some coding improvement opportunities.
Do you want to operate on the files listed in "blocks.txt", or do you just want the single file "rules.txt" to be modified?

Last edited by RudiC; 03-08-2019 at 07:15 AM..
# 3  
Old 03-08-2019
That is a useless use of cat, also. while read ... done < inputfile is more efficient and has fewer side-effects.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read a file and replace values in a script

Hi , I have a property file placed in folder /usr/opt/temp/aorc.prop which has values given below . I need to read this file content and replace the node with actual values in a shell script . Each time the script shall replace the node value from the poperty file and execute a cfsend command and... (10 Replies)
Discussion started by: samrat dutta
10 Replies

2. Shell Programming and Scripting

Perl script to read string from file#1 and find/replace in file#2

Hello Forum. I have a file called abc.sed with the following commands; s/1/one/g s/2/two/g ... I also have a second file called abc.dat and would like to substitute all occurrences of "1 with one", "2 with two", etc and create a new file called abc_new.dat sed -f abc.sed abc.dat >... (10 Replies)
Discussion started by: pchang
10 Replies

3. Shell Programming and Scripting

How to read file, and replace certain string with another string?

Hi all, the value in the following file is just an example. It could be a different value/network addresses. Here is my example of initial output in a file name net.txt Initial Output, net.txt The goal is to produce the following format which is to convert from CIDR to Netmask... (6 Replies)
Discussion started by: type8code0
6 Replies

4. Shell Programming and Scripting

Read from one file-Replace a pattern in another with the current one

Hi Friends, I have a text file like this cat main.txt I like this website cat website > new_website grep website > hello_website cat replace.txt hello unix apple Now, for each line read in 2.txt, I want the pattern "website" in 1.txt to be replaced with it. Basically, I... (9 Replies)
Discussion started by: jacobs.smith
9 Replies

5. Shell Programming and Scripting

perl- read search and replace string from the file

Dear all, I have a number of files and each file has two sections separated by a blank line. At the top section, I have lines which describes the values of the alphabetical characters, # s #; 0.123 # p #; 12.3 # d #; -2.33 # f #; 5.68 <blank line> sssssss spfdffff sdfffffff Now I... (4 Replies)
Discussion started by: sasharma
4 Replies

6. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

7. Shell Programming and Scripting

Script to read file and extract data by matching pattern

Hello, I have a file ( say file1) which has lines like below. xxxx:xxxx,yyyy,1234,efgh zzzz:zzzz,kkkk,pppp,1234,xxxx,uuuu,oooo dddd:dddd here the word before ":" ( ie: xxxx) is the file name and the string after : are also file names, but each file name separated by "," In case of... (20 Replies)
Discussion started by: pradeepmacha
20 Replies

8. Shell Programming and Scripting

Help needed :Search and Replace a string pattern with empty in an xml file in unix

Search and Replace a string pattern with empty in an xml file in unix: My xml file would be like this : <Accounts><Name>Harish</Name><mobile>90844444444444445999 </mobile><TRIG>srcujim-1</TRIG></Accounts><Accounts><Name>Satish</Name><mobile>908999</mobile><TRIG>ettertrtt-1</TRIG></Accounts> ... (1 Reply)
Discussion started by: harish_s_ampeo
1 Replies

9. Shell Programming and Scripting

replace character in a string pattern and save the change in same file

I am facing one problem, can any one please suggest me the command for the same in unix. I am using Ksh. I have a large file with the data that looks like below. "ROTO2-2007f","_US01","9/15/2007","9/21/2007",346492,"NICK, LCD WATCH"97,1,"NAPOLITJ ","BERGER,M Z & CO INC",0.01, ... (2 Replies)
Discussion started by: mihir0011
2 Replies
Login or Register to Ask a Question