how do you get a string before a specific character in bash?

Your conditions are very clear and well defined. So the whole job can be done by an awk script:

awk '/^;/ || (!/IN A/) {next}          # condition 1 and condition 2
     {sub(/IN A/,"",$0);$1=$1;$0=$0}   # condition 5, FS>OFS, recompute fields
     {sub(/[.]$/,"",$1)}               # condition 3
     {sub(/;.*$/,"",$0)}               # condition 4
     {sub(/[.]$/,"",$NF)}              # condition 6 (IP is now in last col)
     { print }' file
  • Condition 1 to 4 are given by the OP.
  • Condition 5 wants to remove IN A
  • Condition 6 remove potential final . from IP

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top