user2461794
user2461794

Reputation: 21

Find and Replace Inside a Text File from a ash Command

I need to modify href URLs in several html files on a fairly small embedded system that run ash shell and limited busybox. Sole functions available are:

ash, brctl, chmod, cp, dnsd, free, halt, ifconfig, init, kill,ls, mkdir, mknod, mount, mv, ping, poweroff, printf, ps, reboot, rm, route, sh, sleep, syslogd, telnetd, umount, vconfig, wc

So, no sed, no echo, etc... I need to find a pattern matching solution!...

I found this ksh trick:

alpha='This is a test string in which the word "test" is replaced.' beta="${alpha//test/replace}"

but it does not seem to work on ash: syntax error: Bad substitution

Any help appreciated! Thanks

Upvotes: 2

Views: 1444

Answers (1)

Brad
Brad

Reputation: 3530

The substitution is bad because you are using two forward slashes immediate after alpha. The pattern you want is ${VARIABLE/FINDSTR/REPLACEMENT}. For example:

A="foobar" && echo ${A/foo/bar}

However, there appears to be no command for reading a string from the file into a variable. That is, no cat, head, tail.

So, without some way to get the data from the HTML file into the script I think you're stuck.

Upvotes: 0

Related Questions