Replace a space with underscore in a file

i have a file with following data.

{
EqName        "Tan 1"
....
....
}

{

EqName        "Sin 2"
...
...
}

I have to replace the value of EqName to Tan_1 and Sin_2 in file.Can i use sed or awk ?

cat file|grep EqName|awk '{print $2 $3}'|sed -i 's//_/g' 

I tried with this but it shows

sed: no input files

Can anyone help me?

There is (as is always the case) a number of ways of doing this. Here's one with awk:

awk -F'"' '/EqName/ { sub(" ", "_", $2) }1' file
{
EqName         Tan_1
....
....
}

{

EqName         Sin_2
...
...
}

And one way with sed:

sed -i '/EqName/{s/\(.*\) /\1_/}' file
1 Like

Thanks a ton !!!.It worked