Script to replace a string with pattern read from a file


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 2 Weeks Ago
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 2 Weeks Ago
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; 2 Weeks Ago at 06:15 AM..
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Replace String matching wildcard pattern mohtashims Shell Programming and Scripting 3 12-04-2018 03:01 PM
Read a file and replace values in a script samrat dutta Shell Programming and Scripting 10 05-18-2016 11:16 AM
Perl script to read string from file#1 and find/replace in file#2 pchang Shell Programming and Scripting 10 08-10-2015 08:58 AM
How to read file, and replace certain string with another string? type8code0 Shell Programming and Scripting 6 12-05-2014 01:29 PM
Replace string in line below specific pattern? TECK Shell Programming and Scripting 1 11-10-2013 04:04 AM
Replace a string pattern bhupinder08 Shell Programming and Scripting 10 10-07-2013 12:03 PM
Read from one file-Replace a pattern in another with the current one jacobs.smith Shell Programming and Scripting 9 12-07-2012 02:16 AM
perl- read search and replace string from the file sasharma Shell Programming and Scripting 4 08-05-2012 01:12 PM
sed or awk command to replace a string pattern with another string based on position of this string vivek d r Shell Programming and Scripting 10 06-19-2012 09:35 AM
script to grep a pattern from file compare contents with another file and replace namitai Shell Programming and Scripting 2 08-30-2011 01:31 PM
Script to read file and extract data by matching pattern pradeepmacha Shell Programming and Scripting 20 06-09-2011 07:07 AM
Help needed :Search and Replace a string pattern with empty in an xml file in unix harish_s_ampeo Shell Programming and Scripting 1 03-11-2011 10:40 PM
Match pattern and replace with string sol_nov Shell Programming and Scripting 6 09-04-2009 07:56 PM
replace character in a string pattern and save the change in same file mihir0011 Shell Programming and Scripting 2 09-26-2007 05:31 PM