-1

I have a very lengthy file(more than 4000 lines generated per day)in the following format.

yqiemmtcveihai

test-trans

10.227.30.66

<----------->

14.192.17.143

<----------->

peuddnbtmzdptw

Ttest-trans1

10.227.30.67

<----------->

14.192.17.142

<----------->

cqykfavuxpuqiq

Med

202.21.32.218

<----------->

hziuqbvuncwkie

Myubun

202.21.32.230

<----------->

I want to remove the <-----------> entry only between the IP's. So the result should be in this format.

yqiemmtcveihai

test-trans

10.227.30.66

14.192.17.143

<----------->

peuddnbtmzdptw

Ttest-trans1

10.227.30.67

14.192.17.142

<----------->

cqykfavuxpuqiq

Med

202.21.32.218

<----------->

hziuqbvuncwkie

Myubun

202.21.32.230

<----------->

Can anyone suggest to me a script, preferably in bash, to achieve this? I was breaking my head, but could not figure it out.

1

I want to remove the <-----------> entry only between the IP's.

Assuming your problem can be restated as 'I want to remove the lines containing <----------->, but only if followed by an IP address', then the examples below may work for you.

Most of these are adaptations of the excellent answers over at this SO question.

sed

sed -E '$!N;s/<----------->\n(([0-9]{1,3}\.){3}[0-9]{1,3})/\1/;P;D' input.txt

awk

awk 'NR==1 {l=$0; next}
     /^([0-9]{1,3}\.){3}[0-9]{1,3}$/ {
       if(l == "<----------->") { l=$0; next }
     }
     {print l; l=$0}
     END { print l }' input.txt

perl

perl -00 -pe 's/<----------->\n((\d{1,3}\.){3}\d{1,3})/$1/g' input.txt

python

python -c \
  "import re; print re.sub(r'^<----------->\n((\d{1,3}\.){3}\d{1,3})$',
                           r'\1', open('input.txt').read(), flags=re.M),"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.