I’m trying to replace a number that is in a couple different strings in a text file. Basically it would take the form of
tableNameNUMBER
carNUMBER
I’m pretty new to bash and scripting and I wasn’t sure how to replace NUMBER with what I pass in. So I’ve tried this:
#! /usr/bin/env bash
sed "s/NUMBER/$1/" myScript.txt > test.txt
then at the command line:
sh test.sh 123456
This only works if NUMBER is on its own, without tableName or car preceding it. How can I replace NUMBER in those cases. Is it better to have ${NUMBER}? Sorry if these are totally noob questions.
This should work just fine:
The
gon the end allows set to replace NUMBER if it appears multiple times on one line.In fact, a quick test:
foo.txt
Isn’t that what your
sedcommand is doing?If you want to make sure you don’t change NUMBER unless it’s by itself, use
\baroundNUMBER:If you don’t care about the case of the string
NUMBER, put anion the end of the sed command: