r/ScriptSwap • • Jan 04 '13

[Request] [X-Post AskScriptSwap] Help with find-replace for multiple files in Linux

Hi, I'm new to bash scripting and am trying to do the following:

Create 25 files with template content

Modify part of the content in the file to change a common value

Modify each file to change one value in each file with another value from a list

I've been able to get #1 and #2 done.

3 is failing.

I've tried scouring the internet, but am unable to find out how to do #3. My script is pasted below:

	#!/bin/bash
	set change='cat change.list'
	#Creating 5 files with template content

	for I in {1..5}
	do
	touch $I
	cat randomContent > $I
	done

	#Some of the common content in the template is changed
	grep -rl '120456' ./ | xargs sed -i 's;120456;test;g'

	#Some of the content in the template is file specific
	#Here $change is a list of file specific content
	#Trying to find replace the common content with file specfic content

	echo $change
	for x in $change
	do
	echo $x
	grep -rl 'testing' ./ | xargs sed -i 's;testing;$x;g'
	done

Could someone help me with this?

3 Upvotes

1 comment sorted by

View all comments

2

u/[deleted] Feb 27 '13

$x isn't evaluated inside single quotes. Try double quotes.