Home > Blockchain >  How to use sed command to replace text between two single quotation marks in a file?
How to use sed command to replace text between two single quotation marks in a file?

Time:10-18

Let's say I have the file: Address.txt

Address.txt

Name = John
Initial = 'J'
Phone = 123-456-789
Home = '1234 North Street'
Work = '5678 South Street'

How can I replace the text between specific two ' marks at specific using sed command or any other terminal command?

For example, changing Address.txt to:

Name = John
Initial = 'J'
Phone = 123-456-789
Home = '147 East Avenue'
Work = '5678 South Street'

Update 1: I would like to change the information in the line that starts with Home = The information after Home = is not fixed. It could be anything. For example, 'Hello', None', or even '' (nothing).

CodePudding user response:

In the above example you could do this:

sed "/^Home/s/'[^']*'/'147 East Avenue'/" Address.txt

The /Home/ matches on the line of interest, and s// command replaces everything between the two single quotes including the quotes themselves. If you want to edit the file change sed to sed -i.

  • Related