Valentin S
Valentin S

Reputation: 1

How to replace value from one column with value from another column in the same file

I have a file with thousands of lines and columns, two of the columns are IP1 and IP2. IP1 is always the same 192.168.100.1

*example.com,192.168.100.1,10.10.1.1,,5effd70e9d99b1acf,10,63,58,42,0,21,84055280,0 example2.com,192.168.100.1,10.10.1.50,,255b2l429c8f23ee,10,63,37,42,1,21,1451066297,0 example3.com,192.168.100.1,10.10.1.58,,589b7a5f8677b,11,68,37,42,1,20,1451066297,0

.............*

I want to replace the value of IP1 with value of IP2, and delete the value of IP2.

I tried this:

sed -i 's/192\.168\.100\.1/$(grep 192\.168\.100\.1 file | awk -F',' '{print $2}')/' file

The following error occured: sed: -e expression #1, char 68: unterminated `s' command

Please help.

Upvotes: 0

Views: 1442

Answers (2)

oliv
oliv

Reputation: 13249

sed to the rescue! eliminate awk, this should do...

sed -i 's/192\.168\.100\.1,//' file

This sed command finds the string "192.168.100.1," and replace it by nothing.

. has a special meaning in a sed command, so it requires backslash. More info on the man pages.

Upvotes: 0

karakfa
karakfa

Reputation: 67497

awk to the rescue! eliminate sed, this should do...

awk -F, -v OFS=, '{$2=$3;$3=""}1' file

Upvotes: 1

Related Questions