0

I have the following lines in my input file and I would like to replace all the text after "=" sign with a new string, say "good".

Again, I would like to do this only for certain strings in the line.

Input file:

$myvar1=var1
$myvar2=var2
$myname=name
$myage=age
$mycity=city

Output file:

$myvar1=var1
$myvar2=var2
$myname=good
$myage=good
$mycity=good
4
  • 2
    What "certain strings" are these?
    – Jeff Schaller
    Sep 20, 2019 at 19:31
  • In the above Input File, I would like to keep the 1st 2 lines [$myvar1 and $myvar2..] as-is.. That is what i call "certain string' as i dont want to touch those lines
    – veekay
    Sep 20, 2019 at 19:33
  • 1
    Why? Because they're the first two lines or because they contain the string "myvar", or what?
    – Jeff Schaller
    Sep 20, 2019 at 23:06
  • B'Cos they contain strings $myvar1 and $myvar2
    – veekay
    Sep 23, 2019 at 18:16

4 Answers 4

2

I think sed is the proper tool for what you want to do, here is my implementation - instead of looking for something, I negate what you are protecting:

sed -r '/^((\$myvar1)|(\$myvar2))/!s/=.*$/="good"/g'

7
  • I get an error message : not a recognized flag r
    – veekay
    Sep 23, 2019 at 18:21
  • @veekay - That's probably because you don't have that option. Try it with -E.
    – Adam D.
    Sep 24, 2019 at 20:06
  • that does not work either..
    – veekay
    Sep 24, 2019 at 20:07
  • What does it say? What system and sed version are you using?
    – Adam D.
    Sep 24, 2019 at 20:09
  • am a novice here. how to find it..
    – veekay
    Sep 24, 2019 at 20:39
1

To skip the first two lines, you could use:

sed '3,$s/=.*/=good/' file

This does the substitution from line 3 to the last line ($).

1

Is this what you are looking for? With GNU sed.

sed -E '/var[0-9]/!s/(=).*/\1good/' file
2
  • I get an error message : not a recognized flag E
    – veekay
    Sep 23, 2019 at 18:27
  • Could it be that you the dash - is missing? Sep 24, 2019 at 11:10
-1

Assuming you know ahead of time the variable names (e.g. $myname, $myage, and $mycity) that you would like to change, you can use sed:

sed -i '/^\$myname=/s/=.*$/=good/g' input_file_name
sed -i '/^\$myage=/s/=.*$/=good/g' input_file_name
sed -i '/^\$mycity/s/=.*$/=good/g' input_file_name

Basically, this just says to only run a regular expression on lines that start with a certain match (lines with ^\$myname=, ^\$myage=, or ^\$mycity=), and replace the equal sign plus everything following (=.*$) with an equal sign and the appropriate string (=good).

NOTE: If the -i option is not available with your version of sed, consider the following instead for the same effect:

sed '/^\$myname=/s/=.*$/=good/g' input_file_name > output_file_name
sed '/^\$myage=/s/=.*$/=good/g' input_file_name > output_file_name
sed '/^\$mycity/s/=.*$/=good/g' input_file_name > output_file_name
6
  • sed -i does not seem to be working for me
    – veekay
    Sep 20, 2019 at 19:31
  • Are you using BSD/macOS? Does sed -i '' … work?
    – Freddy
    Sep 20, 2019 at 19:51
  • Why -i at all? OP writes about input file and output file, not modifying the input file. And there is no reason for calling sed three times. Finally, your second line contains an error.
    – Philippos
    Sep 23, 2019 at 6:55
  • Thanks for catching the mistake on the second line. I also understand your points, but because OP was being so vague, I do not planning on deleting this answer. The other answers assumed that OP always wanted to substitute good into the lines. I separated it into three separate calls so that it's easier if OP actually wanted to change each specific line to something different. Also, based on the content being substituted, I interpreted OP's intentions to be bulk modifying BASH/perl/etc. code, which would make the -i option potentially useful under certain circumstances. Sep 23, 2019 at 16:36
  • I get an error message : not a recognized flag -i
    – veekay
    Sep 23, 2019 at 18:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .