[英]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所以,我需要将这些写到另一个文件中
It should avoid any row starting with ;
它应该避免任何以;
开头的行;
- 2nd row is avoided
- 2nd row is avoided
It should only picks the rows with IN A
- only the 1st row is seletced
它应该只选择带有IN A
的行 - only the 1st row is seletced
It should remove the training .
它应该删除训练.
at the end of every first column - remove . after abc.com.
在每一列的末尾 - remove . after abc.com.
remove . after abc.com.
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
IN A
条件 5 要删除IN A
.
条件 6 删除潜在的 final .
from IP来自IPThis 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.