Pravej Khan
Pravej Khan

Reputation: 152

Search for a pattern and replace it in shell script

Hello I have a file with multiple lines. I need to search for a pattern ("") and replace it with another one (/"") with condition that its suffix with any string.
Ex:

cat myfile

27;"";"firstName"";"lastName";"user1";6;"Change!1"
28;"[email protected]";"Pravej0001";"Khan001";"test_u_001";7;"Change!1"

I need to replace firstName"" with firstName/"" in line number one.

I have already tried below code in sed :

sed 's/[A-Za-z0-9]""/\/""/g' myfile 

but it is giving output as

27;"";"firstNam/"";"lastName";"user1";6;"Change!1"
28;"[email protected]";"Pravej0001";"Khan001";"test_u_001";7;"Change!1"

where e is replaced with / . I dont want to disturb the firstName.

Expected output should be like this:

27;"";"firstName/"";"lastName";"user1";6;"Change!1"
28;"[email protected]";"Pravej0001";"Khan001";"test_u_001";7;"Change!1"

Any help in sed/awk/shell will work.

Upvotes: 0

Views: 160

Answers (1)

Ed Morton
Ed Morton

Reputation: 204578

sed 's/\([A-Za-z0-9]\)""/\1\/""/g' myfile

or more robustly across locales:

sed 's/\([[:alnum:]]\)""/\1\/""/g' myfile

Upvotes: 3

Related Questions