Help with sed, please

Hello. I've got a sed question that I'd appreciate your help with.

I have a file containing the following line.

.
.
FilePointName = "File1Name";
.
.

I'd like to use sed to change the above line throughout the file to

.
.
FilePointName = "MyFileName";
.
.

I don't know the name of File1Name in advance, so I just want to replace whatever
appears there with MyFileName.

I know this must be something simple with sed but not quite sure how to do it. Any
help you can provide would be much appreciated.

Rob

Hi, try:

sed 's/\(FilePointName *= *"\)[^"]*"/\1MyFileName"/' file

or

sed 's/\(FilePointName *= *\)[^;]*/\1"MyFileName"/' file

Two other options. Change MyFileName for whatever you wish:

sed 's/\(FilePointName[^[:alpha:]]*\)[[:alnum:]]*/\1MyFileName/' rob_rice.example
perl -pe 's/(FilePointName\W+)\w+/$1MyFileName/' rob_rice.example

Assuming FilePointName appears on every line you want to change; and the filename always appears in double-quotes:

sed '/FilePointName/ s/".*"/"MyFilename"/'

or alternatively

sed '/FilePointName/ s/=.*;/= "MyFilename";/'

I think this has the advantage on not relying on GNU or other extensions.

Andrew

@Aia, the preceding * match should be the negated following * match, otherwise it can consume the not covered characters (here: leading digits).
Better is

sed 's/\(FilePointName[^-_/.[:alnum:]]*\)[-_/.[:alnum:]]*/\1MyFileName/' rob_rice.example

I have added some more characters to the character sets. If one does not want restrictions on the allowed value characters, it is better to name the not allowed ones

sed 's/\(FilePointName[=";[:space:]]*\)[^=";[:space:]]*/\1MyFileName/' rob_rice.example

a bit more generic:

echo 'FilePointName = "File1Name";' | sed '/FilePointName/s/\([^"]*\)"\(.*\)/\1"My\2/'

As a generic solution I would not search the key in the entire line, because the key (especially a short key) can appear in a value.

fair enough:

echo 'FilePointName = "File1Name";' | sed '/^FilePointName/s/\([^"]*\)"\(.*\)/\1"My\2/'