簡體   English   中英

grep 單 awk 列

[英]grep a single awk column

這是我的示例數據:

XYZ Domain|agent756|allegr8732|2022-01-12 15:36:24.0|0|{"username":"sample_usernme","login":"jack777","groups":[],"emailAddress":"sample@sample.com","company":null,"phone":null,"bindedKeys":["sample_usernme"],"owner":null,"expirationDate":null,"uploadsQuota":null,"downloadsQuota":null,"transfersQuota":null,"maxTransferRateBytesPerSecond":null,"resources":[{"type":"VirtualRemoteFileDescriptor","path":"","accessPermissions":{"fileDownloadingAllowed":true,"fileUploadingAllowed":true,"fileOverwritingAllowed":true,"fileDeletionAllowed":true,"fileAppendingAllowed":true,"fileListingAllowed":true,"fileRenamingAllowed":true,"directoriesListingAllowed":true,"directoryMakingAllowed":true,"directoryDeletionAllowed":true,"subdirectoriesBrowsingAllowed":true},"secured":false,"denied":false,"indexable":false,"name":"ref_proxy"}],"administration":null,"secured":false,"enabled":true,"passwordChangingAllowed":true,"emailFileTransferAllowed":true,"usePhoneAuthentication":false,"ignorePasswordAgingRules":false,"passwordResetRequired":false,"loginRedirection":{"type":"DirectoryRedirection","directory":""},"lastLoginDate":null,"ipAccessVerifier":{"type":"NullVerifier"},"maxUploadsPerSession":null,"maxDownloadsPerSession":null,"webPublicKeyAuthenticationAvailable":true,"webOpenPgpEncryptionAvailable":true,"webQuotasAvailable":true,"webContactsAvailable":true,"webAdHocActivityAvailable":true,"webDropZonesAvailable":true,"webAccountLinkAvailable":true,"webPersonalInformationAvailable":true,"passwordExpirationNotification":null,"otpSharedSecret":null,"notes":"","options":{},"tags":[],"creationDate":1602502466555,"version":3,"passwordHash":"","expired":false,"passwordDate":1602501463354,"passwordHistory":[],"memberOfAnyGroup":false}

我想得到結果:

myh.com|XYZ Domain|agent756|allegr8732|0|true

是的-它是正則表達式的結果: (?<="enabled":).*?(?=,")

為此,我嘗試了這樣的事情:

cat users.txt | awk -F"|" -vhostname="$(hostname)" '{ print hostname"|"$1"|"$2"|"$3"|"$5"|"... }'

如何使用示例正則表達式額外打印 $6 以獲取值“true”?

使用 JSON 解析器解析 JSON 數據:

data='XYZ Domain|agent756|allegr8732|2022-01-12 15:36:24.0|0|{"username":"sample_usernme","login":"jack777","groups":[],"emailAddress":"sample@sample.com","company":null,"phone":null,"bindedKeys":["sample_usernme"],"owner":null,"expirationDate":null,"uploadsQuota":null,"downloadsQuota":null,"transfersQuota":null,"maxTransferRateBytesPerSecond":null,"resources":[{"type":"VirtualRemoteFileDescriptor","path":"","accessPermissions":{"fileDownloadingAllowed":true,"fileUploadingAllowed":true,"fileOverwritingAllowed":true,"fileDeletionAllowed":true,"fileAppendingAllowed":true,"fileListingAllowed":true,"fileRenamingAllowed":true,"directoriesListingAllowed":true,"directoryMakingAllowed":true,"directoryDeletionAllowed":true,"subdirectoriesBrowsingAllowed":true},"secured":false,"denied":false,"indexable":false,"name":"ref_proxy"}],"administration":null,"secured":false,"enabled":true,"passwordChangingAllowed":true,"emailFileTransferAllowed":true,"usePhoneAuthentication":false,"ignorePasswordAgingRules":false,"passwordResetRequired":false,"loginRedirection":{"type":"DirectoryRedirection","directory":""},"lastLoginDate":null,"ipAccessVerifier":{"type":"NullVerifier"},"maxUploadsPerSession":null,"maxDownloadsPerSession":null,"webPublicKeyAuthenticationAvailable":true,"webOpenPgpEncryptionAvailable":true,"webQuotasAvailable":true,"webContactsAvailable":true,"webAdHocActivityAvailable":true,"webDropZonesAvailable":true,"webAccountLinkAvailable":true,"webPersonalInformationAvailable":true,"passwordExpirationNotification":null,"otpSharedSecret":null,"notes":"","options":{},"tags":[],"creationDate":1602502466555,"version":3,"passwordHash":"","expired":false,"passwordDate":1602501463354,"passwordHistory":[],"memberOfAnyGroup":false}'

IFS='|' read -ra fields <<<"$data"
wanted=(
    "$(hostname)"
    "${fields[@]:0:3}"
    "${fields[4]}"
    "$(jq -r .enabled <<<"${fields[5]}")"
)

(IFS='|'; echo "${wanted[*]}")
myh.com|XYZ Domain|agent756|allegr8732|0|true

實際上,json 數據可能包含 pipe 字符:這樣更好

while IFS='|' read -r a b c d e f; do
  printf '%s|%s|%s|%s|%s|%s\n' "$(hostname)" "$a" "$b" "$c" "$e" "$(jq -r .enabled <<<"$f")"
done < users.txt

其他語言包括 JSON 庫。 例如,ruby

ruby -rjson -e '
  File.new(ARGV.shift).each do |line|
    f = line.split /[|]/, 6
    json = JSON.parse f[5]
    wanted = [%x(hostname).chomp, f[0], f[1], f[2], f[4], json["enabled"]]
    puts wanted.join "|"
  end
' users.txt

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM