Need help with sed command

Hi All,

I want to replace the following text "jai/1" to "jai/2" .

sed -i s/"jai/1"/"jai/2"/g fileName

not working.

Can some one help?

Use the following command:

sed -i 's/jai\/1/jai\/2/g' file.txt
1 Like

Hello,

Please try the following with sed .

echo "jai/1" | sed 's/\(...\)\/1/\1\/2/'

output will be as follows.

jai/2

Thanks,
R. Singh

Assuming that the double quotes in the first message are text to be matched and replaced (not just string delimiters), Krishna's suggested code might change some occurrences of jai/1 that are not between quotes.

The problem (as Krishna hinted) is that you can't use a character as a field delimiter if it appears in the RE you're trying to match nor if it appears in the replacement string unless you escape it. Krishna's suggestion was to escape the slash characters that are part of the RE and that are part of the replacement string. It is often easier to use a different delimiter as in:

sed -i s@"jai/1"@"jai/2"@g fileName

You can use any character in a sed substitute command as a field separator except backslash ( \ ) and the <newline> character.

1 Like

Thanks all for replys
thats Don Cragun for sharing knowledge :slight_smile:

Dear All,

Again I need help with sed command.
How can I change a value in file using its variable name.
example:

xValue oldNumber;

to

xValue newNumber;

in file name file.txt
I want to sed a line begin with xValue, because xValue is not always 10.
so what ever the line begin with xValue i want to replace complete line with xValue newNumber;

@linuxUser: The questions is not clear to me. Can you please tell us what exactly you want to achieve?

Thanks & Regards
Krishna Yadav