0

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.

1
  • You should read the book Shell Scripting Recipes by Chris Johnson to learn the basics of sed, grep, awk and general shell scripting and then Effective Awk Programming, 4th Edition, by Arnold Robbins if you're going to do any other text manipulation in UNIX in future. Commented Apr 10, 2016 at 23:24

2 Answers 2

1

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

awk -F, -v OFS=, '{$2=$3;$3=""}1' file
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

While this code may answer the question, providing additional context regarding why and/or how it answers the question would significantly improve its long-term value. Please edit your answer to add some explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.