[英]Using Grep or AWK command
我有以下文本信息文件:
http://=
en.domain.com/registration.html#/?doitoken=1D7f1ad404-f84b-4a3b-8931=
-4f40b619730e
http://=
en.domain.com/registration.html#/?doitoken=5D8172f6e6-240f-42e6-8512=
-6d7f6bd61c2d
http://=
en.domain.com/registration.html#/?doitoken=8D8172f6e6-240f-42e6-8512=
-6d7f6bd61c2d
我如何在Linux bash中使用grep或awk命令做到這一點:
http://en.domain.com/registration.html#/?doitoken=1D7f1ad404-f84b-4a3b-8931-4f40b619730e
http://en.domain.com/registration.html#/?doitoken=5D8172f6e6-240f-42e6-8512-6d7f6bd61c2d
http://en.domain.com/registration.html#/?doitoken=8D8172f6e6-240f-42e6-8512-6d7f6bd61c2d
感謝您的回答!
awk 'BEGIN{FS="=\n"; RS=""; OFS=""} {print $1, $2, $3}' input_file
您還可以擺脫OFS=""
並刪除print語句中的,
s
將程序另存為pr.awk ,然后運行awk -f pr.awk input.dat
NF {
n++
sub(/=$/, "")
ans = ans $0
}
n==3 { # flush
print ans
ans = ""; n = 0
}
$ awk '/=$/{sub(/=$/,""); printf "%s",$0;next} /./{print}' file
http://en.domain.com/registration.html#/?doitoken=1D7f1ad404-f84b-4a3b-8931-4f40b619730e
http://en.domain.com/registration.html#/?doitoken=5D8172f6e6-240f-42e6-8512-6d7f6bd61c2d
http://en.domain.com/registration.html#/?doitoken=8D8172f6e6-240f-42e6-8512-6d7f6bd61c2d
這個怎么運作:
/=$/{sub(/=$/,""); printf "%s",$0;next}
如果該行以=
結尾,則刪除尾隨=
,打印結果(不包含尾隨換行符)並跳至next
行。
/./{print}
如果到達此處,則此行不以=
結尾,而我們只是正常打印(帶有尾隨換行符)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.