简体   繁体   English

如何在bash中的特定字符之前获取字符串

[英]How to get a string before a specific character in bash

I have some content in the file lists.txt as below:我在文件lists.txt有一些内容如下:

abc.com.                IN A        10.120.51.95    ;10.40.40.57 ;old 10.20.3.57    
;def-mytaxi.com.        IN A        10.12.4.9   ;10.40.3.43 ;test
xyz-mytaxi.com.     IN CNAME        10.12.4.8   ;10.40.3.53 ;test

So, I need to write these to another file so所以,我需要将这些写到另一个文件中

  1. It should avoid any row starting with ;它应该避免任何以;开头的行; - 2nd row is avoided - 2nd row is avoided

  2. It should only picks the rows with IN A - only the 1st row is seletced它应该只选择带有IN A的行 - only the 1st row is seletced

  3. It should remove the training .它应该删除训练. at the end of every first column - remove . after abc.com.在每一列的末尾 - remove . after abc.com. remove . after abc.com.

  4. It should avoid any values in the selected row/s after ;它应该避免在选定行中的任何值; - therefore only prints abc.com 10.120.51.95 - therefore only prints abc.com 10.120.51.95

and the final output should be written to a file and it should look like;最终输出应该写入一个文件,它应该看起来像;

abc.com 10.120.51.95

So I wrote this script but everything works fine except the 3rd and the 4th steps.所以我写了这个脚本,但除了第3rd步和第4th步外,一切正常。

I get the output as:我得到的输出为:

abc.com.   10.120.51.95   10.40.40.57 old 10.20.3.57  

here's what I tried, can someone help me?这是我尝试过的,有人可以帮助我吗?

awk '/IN A/ {$2=$3=""; print $0}' lists.txt  | sed '/^;/d;s/;//g;s/#//g' > updated_list.txt

Your conditions are very clear and well defined.您的条件非常明确且定义明确。 So the whole job can be done by an awk script:所以整个工作可以通过一个awk脚本来完成:

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.条件 1 到 4 由 OP 给出。
  • Condition 5 wants to remove IN A条件 5 要删除IN A
  • Condition 6 remove potential final .条件 6 删除潜在的 final . from IP来自IP

This awk should also work:这个awk也应该有效:

awk '!/^[[:blank:]]*;/ && / IN A / {
   sub(/\.$/, "", $1)
   print $1, $4
}' file
abc.com 10.120.51.95

Could you please try following, written and tested with shown samples only in GNU awk .您能否尝试仅在 GNU awk使用所示示例进行跟踪、编写和测试。

awk '
!/^;/ && /IN A/{
  sub(/\.+$/,"",$1)
  match($0,/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+ +;/)
  val=substr($0,RSTART,RLENGTH)
  sub(/ +;.*/,"",val)
  print $1,val
  val=""
}' Input_file

Explanation: Adding detailed explanation for above.说明:为以上添加详细说明。

awk '                                  ##Starting awk program from here.
!/^;/ && /IN A/{                       ##Checking condition if line DO NOT start from ; AND has IN A in it then do following.
  sub(/\.+$/,"",$1)                    ##Substituting all dots trailing in first field with NULL here.
  match($0,/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+ +;/)   ##Using match by mentioning regex to match IP address followed by space and followed by semi colon in here.
  val=substr($0,RSTART,RLENGTH)        ##Creating val variable which has sub string of matched regex in above statement.
  sub(/ +;.*/,"",val)                  ##Substituting all spaces and everything from semi colon to last of line in val here.
  print $1,val                         ##Printing first field and val here.
  val=""                               ##Nullifying val here.
}' Input_file                          ##Mentioning Input_file name here.
$ awk 'match($1,/^[^;].*/)&&$2=="IN"&&$3=="A"{print substr($1,RSTART,RLENGTH-1),$4}' file

Output:输出:

abc.com 10.120.51.95

"Explained": “解释”:

$ awk '
match($1,/^[^;].*/) && $2=="IN" && $3=="A" {     # match 
    print substr($1,RSTART,RLENGTH-1),$4         # and output
}' file

Edit : To remove the trailing .编辑:删除尾随 . from the ip:从ip:

awk '
match($1,/^[^;].*/) && $2=="IN" && $3=="A" {
    print substr($1,RSTART,RLENGTH-1),
        ($4~/\.$/?substr($4,1,length($4)-1):$4)  # remove extra . from ip
}' 

尝试这个:

awk '/^[a-z].*IN A/{sub(/\.$/, "", $1); print $1, $4}' file

替代:

awk '{ gsub(/;.*/,"")} / IN A / { gsub(/\.$/,"",$1); printf("%s %s\n",$1,$4)}' FILE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM