简体   繁体   English

grep 单 awk 列

[英]grep a single awk column

Here is my sample 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}

I would like to get result:我想得到结果:

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

true- it is a result of regex: (?<="enabled":).*?(?=,")是的-它是正则表达式的结果: (?<="enabled":).*?(?=,")

to do this I tried something like this:为此,我尝试了这样的事情:

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

How can I print in addition $6 using sample regex to get value "true"?如何使用示例正则表达式额外打印 $6 以获取值“true”?

Parse JSON data with a JSON parser:使用 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

Actually, the json data might contain the pipe character: this is better实际上,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

Other languages have JSON libraries included.其他语言包括 JSON 库。 For example, ruby例如,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