1

File1.txt

1.1.1.1

File2.txt

tcp=ipv4
netmask=255.255.255.0
ip=

Desired Output:

tcp=ipv4
netmask=255.255.255.0
ip=1.1.1.1

I want to replace complete line in File2.txt from File1.txt value searching by "ip=" string.

i try:

sed '/ip=/r File1.txt' File2.txt | sed '/ip=/d'

Output:

tcp=ipv4
netmask=255.255.255.0
1.1.1.1

How to add "ip=" string value to get desired output?

5
  • 2
    Please show us the code for your latest attempt and where you got stuck. See also: How to Ask and help center. Commented Jan 13, 2021 at 17:21
  • 1
    sed '/ip/r File1.txt' File2.txt Commented Jan 13, 2021 at 17:28
  • sed '/ip/r File1.txt' File2.txt | sed '/^ip=/{N; s/\n//}'? Commented Jan 13, 2021 at 18:00
  • sed: 1: "/^ip=/{N; s/\n//}": bad flag in substitute command: '}' Commented Jan 14, 2021 at 17:01
  • @DarvinRiveraAguilar, Changing questions later on is NOT encouraged on SO, so please revert it to your actual question else it will make no sense for all given answers to future users, thank you. Commented Jan 14, 2021 at 17:12

2 Answers 2

3

Could you please try following, written and tested with shown samples in GNU awk. This will handle multiple occurrence/values in file1.txt and fil2.txt too.

awk -F'=' '
BEGIN{ FS=OFS="=" }
FNR==NR{
  arr[++count]=$0
  next
}
/^ip/{
  print $1 OFS arr[++count1]
  next
}
1' file1.txt file2.txt

Explanation: Adding detailed explanation for above.

awk  '                      ##Starting awk program from here.
BEGIN{ FS=OFS="=" }
FNR==NR{                   ##Checking condition FNR==NR which will be TRUE when file1.txt is being read.
  arr[++count]=$0          ##Creating arr with index of increasing value of count and its value is current line.
  next                     ##next will skip all further statements from here.
}
/^ip/{                     ##Checking condition if line starts from ip then do following.
  print $1 OFS arr[++count1]   ##Printing current line and arr with index of increasing value of count1.
  next                     ##next will skip all further statements from here.
}
1                          ##1 will print all lines here.
' file1.txt file2.txt      ##Mentioning Input_file names here.
Sign up to request clarification or add additional context in comments.

4 Comments

What happens if i have separate by space in file2.txt , for example: ip 8.8.8.8?
@DarvinRiveraAguilar, you need to change delimiter then to FS=OFS="\t" cheers
with \t add File1.txt line at end ip 8.8.8.8 of File2.txt: ip 8.8.8.8 1.1.1.1 I need replace always last ip value in File2.txt
@DarvinRiveraAguilar, Please revert your question to your actual one, for changing from ip file1_value to ip file2_value change -F'=' TO -F'\t' and that should do the trick then.
2
IP=`cat File1.txt`
sed "/^ip=$/s/$/$IP/" File2.txt

Comments

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.