如何使用awk

时间:2017-05-16 23:40:13

标签: string bash awk

目前我有一个包含2400个事件的json文件。我试图找到用随机的48个字符的字母数字字符串替换访客ID的最简单方法。

目前我有一个基本文件,其中visitorid看起来像这样

“visitorid”:“$ vids”

我希望使用带有随机48个字符的aplhanumeric字符串的awk替换“$ vids”,看起来像是这样的

“visitorid”:“777048146a3a00a43d480d5a52c00001e007b06600398z82”

{ "eventid" : "e67c0705-ff11-4510-b545-1642bdf7d7c7z82" , "visitorid" : "$vids" , "eventtime" : 1444628607000 , "useragent" : "Mozilla/5.0 (iPhone; CPU iPhone OS 9_0_2 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13A452" , "pageurl_full_url" : "https://tags.tiqcdn.com/utag/jasminetestpacific/mobile/prod/mobile.html" , "pageurl_query_params_platform" : "iOS" , "pageurl_query_params_ts" : "87" , "pageurl_query_params_library_version" : "4.0.5" , "pageurl_query_params_os_version" : "9.0.2" , "pageurl_querystring" : "platform=iOS&ts=87&library_version=4.0.5&os_version=9.0.2" , "pageurl_scheme" : "https" , "pageurl_domain" : "tags.tiqcdn.com" , "pageurl_path" : "/utag/jasminetestpacific/mobile/prod/mobile.html" , "referrerurl_full_url" : "https://tags.tiqcdn.com/utag/jasminetestpacific/mobile/prod/mobile.html?platform=iOS&os_version=9.0.2&library_version=4.0.5&ts=87" , "referrerurl_query_params_platform" : "iOS" , "referrerurl_query_params_os_version" : "9.0.2" , "referrerurl_query_params_library_version" : "4.0.5" , "referrerurl_query_params_ts" : "87" , "referrerurl_querystring" : "platform=iOS&os_version=9.0.2&library_version=4.0.5&ts=87" , "referrerurl_scheme" : "https" , "referrerurl_domain" : "tags.tiqcdn.com" , "referrerurl_path" : "/utag/jasminetestpacific/mobile/prod/mobile.html" , "dom_title" : " Mobile Webview" , "dom_domain" : "tags.tiqcdn.com" , "dom_pathname" : "/utag/jasminetestpacific/mobile/prod/mobile.html" , "dom_query_string" : "platform=iOS&os_version=9.0.2&library_version=4.0.5&ts=87" , "dom_url" : "https://tags.tiqcdn.com/utag/jasminetestpacific/mobile/prod/mobile.html?platform=iOS&os_version=9.0.2&library_version=4.0.5&ts=87" , "udo_app_rnds" : "com.mccann.CXMobile" , "udo_page_screen_view" : "true" , "udo_lifecycle_dayssincelastwake" : "0" , "udo__cquan" : "[ ]" , "udo_page_timestamp_local" : "2015-10-12T13:43:24+08:00" , "udo_carrier" : "SMC HK" , "udo_lifecycle_totalwakecount" : "3" , "udo_carrier_mnc" : "06" , "udo_platform" : "iOS" , "udo_device_architecture" : "64" , "udo_library_version" : "4.0.5" , "udo_page_page_type" : "gender and nationality" , "udo_carrier_mcc" : "454" , "udo_lifecycle_sleepcount" : "2" , "udo__cprice" : "[ ]" , "udo_platform_version" : "iOS 9.0.2" , "udo_application_version" : "3.0.10" , "udo_app_id" : "jasminetest Pacific 3.0.11" , "udo_uuid" : "AB854A5C-8FAB-4611-A4BC-6A3BF9982A52" , "udo_registration_type" : "GR" , "udo__csku" : "[ ]" , "udo_member_email" : "dhqatesting+1@gmail.com" , "udo_page_country" : "HK" , "udo_page_language" : "en" , "udo_lifecycle_totalsleepcount" : "2" , "udo_registration_age_group" : "36-50" , "udo_timestamp" : "2015-10-12T05:43:24Z" , "udo_timestamp_local" : "2015-10-12T13:43:24" , "udo__ccustid" : "1500257902" , "udo__cprod" : "[ ]" , "udo_device_resolution" : "320x568" , "udo_device_language" : "en-HK" , "udo_registration_membership_no" : "1500257902" , "udo_lifecycle_dayssinceupdate" : "0" , "udo_lifecycle_hourofday_local" : "13" , "udo_page_page_id_slashified" : "APP/Check in/Gender and Nationality" , "udo__cpdisc" : "[ ]" , "udo__cprodname" : "[ ]" , "udo_registration_asia_miles" : "40723" , "udo_timestamp_unix" : "1444628604" , "udo_lifecycle_totalsecondsawake" : "2" , "udo_device_battery_percent" : "-100" , "udo_page_page_code" : "frmOlciSelectNationality" , "udo_lifecycle_firstlaunchdate" : "2015-10-08 15:31:50 +0000" , "udo_application_maid" : "8F876208-4D23-4B3E-B6DC-085315C565DA" , "udo_app_version" : "3.0.11" , "udo_page_brand" : "CX" , "udo_application_env" : "prod" , "udo_connection_type" : "cellular" , "udo_event_event_label" : "MMB LOGIN::SUCCESS" , "udo__ccat" : "[ ]" , "udo_lifecycle_terminatecount" : "0" , "udo_timestamp_offset" : "8" , "udo_registration_logged_in" : "true" , "udo_page_page_name" : "Gender and Nationality" , "udo_carrier_iso" : "hk" , "udo_status" : "appeared" , "udo_application_uuid" : "e11f36e6-c754-4bb6-ac9b-f466c859ec77" , "udo_call_eventtype" : "action" , "udo_page_locale" : "en_HK" , "udo_event_event_id" : "MMB LOGIN::SUCCESS" , "udo_lifecycle_secondsawake" : "0" , "udo__cbrand" : "[ ]" , "udo_lifecycle_firstlaunchdate_mmddyyyy" : "10/08/2015" , "udo_registration_registration_id" : "1500257902" , "udo_application_name" : "APP" , "udo_lifecycle_dayssincelaunch" : "3" , "udo_app_name" : "jasminetest Pacific" , "udo_lifecycle_launchcount" : "2" , "udo_autotracked" : "false" , "udo_page_page_category" : "booking" , "udo_page_page_id" : "APP::Check in::Gender and Nationality" , "udo_device_ischarging" : "false" , "udo_lifecycle_totallaunchcount" : "2" , "udo_registration_ip_address" : "121.202.253.157" , "udo__ccat2" : "[ ]" , "udo_origin" : "mobile" , "udo_event_event_category" : "MMB LOGIN" , "udo_os_version" : "9.0.2" , "udo_lifecycle_wakecount" : "3" , "udo_call_type" : "view" , "udo_lifecycle_dayofweek_local" : "Monday" , "udo_device" : "iPhone" , "firstpartycookies_utag_main_dc_event" : "4" , "firstpartycookies_utag_main__ss" : "1" , "firstpartycookies_90227263_clogin" : "l=1444628590&v=1&e=1444630394665" , "firstpartycookies_utag_main__st" : "1444630382114" , "firstpartycookies_utag_main_dc_visit" : "2" , "firstpartycookies_utag_main__pn" : "1" , "firstpartycookies_coreid6" : "41742214526514443183888&ci=90227263" , "firstpartycookies__gat__0" : "1" , "firstpartycookies_utag_main_v_id" : "$vids" , "firstpartycookies_cmtpset" : "Y" , "firstpartycookies_utag_main_ses_id" : "1444628582114" , "firstpartycookies__ga" : "GA1.2.785751299.1444318322" , "firstpartycookies_utag_main__sn" : "2"}

2 个答案:

答案 0 :(得分:0)

您可以使用sha哈希创建一个随机字符串并剪切到所需的大小。

r=$(echo $RANDOM | sha256sum); echo ${r:0:48} 

9f295ada2ad453e54ea93b377178e1a151081a3887dfae6

答案 1 :(得分:0)

OpenSSL是生成随机字符串的最佳工具。

vids=$(openssl rand -base64 36); echo $vids
lTAyBbCNRu5nOQfmzXtbyWGh5YS2hbiarUdcB3kRJ+sFAcSP

如果一个随机字符串足以替换所有2400个访客ID,那么您可以通过awk选项轻松将随机字符串传递给-v vids="$vids"

但是,如果您需要为每个访问者ID提供唯一的随机字符串,那么awk并不是完成此任务的最佳工具。