我有一个json结果,我想提取一个没有双引号的字符串
{"value1":5.0,"value2":2.5,"value3":"2019-10-24T15:26:00.000Z","modifier":[]}
使用此正则表达式,我可以正确提取value3(019-10-24T15:26:00.000Z)
sed -e 's/^.*"endTime":"\([^"]*\)".*$/\1/'
如何提取没有双引号的字符串“ value2”结果?
我需要使用sed,因此无法安装jq。那是我的问题
答案 0 :(得分:3)
使用bundle install
的GNU sed来启用ERE:
gem 'sqlite3', git: "https://github.com/larskanis/sqlite3-ruby", branch: "add-gemspec"
使用任何POSIX sed:
-.html -.css -.png -.jpg -.jpeg -.ttf -.woff -.svg -.eot -.gif
以上假设您在引号中没有逗号。
答案 1 :(得分:1)
只需运行// 3. file download from s3
{
string strTargetPath = hThis->m_strTargetPath;
Aws::SDKOptions options;
options.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Trace;
Aws::InitAPI(options);
{
// Download from s3 using GetObject
char *bucket_name = "mybucket";
std::string key_name = strTargetPath;
Aws::Client::ClientConfiguration clientConfig;
clientConfig.region = "ap-northeast-2";
//Aws::S3::S3Client s3_client;
std::unique_ptr< Aws::S3::S3Client > s3_client(new Aws::S3::S3Client(clientConfig));
Aws::S3::Model::GetObjectRequest object_request;
object_request.WithBucket(bucket_name).WithKey(key_name.c_str());
// parse file name from path
string str_arr[1000];
int str_cnt = 0;
char *str_buff = new char[1000];
strcpy(str_buff, strTargetPath.c_str());
char *tok = strtok(str_buff, "/");
while (tok != nullptr) {
str_arr[str_cnt++] = string(tok);
tok = strtok(nullptr, "/");
}
string fileName = str_arr[str_cnt - 1];
auto get_object_outcome = s3_client.get()->GetObject(object_request);
if (get_object_outcome.IsSuccess())
{
Aws::OFStream local_file;
std::string strFileName = fileName;
hThis->m_origFileNameString = strFileName;
hThis->m_origFileName = strFileName.c_str();
// Writing file downloaded
local_file.open(hThis->m_origFileName, std::ios::out | std::ios::binary);
local_file << get_object_outcome.GetResult().GetBody().rdbuf();
hThis->Logger(CPrePackagerDlg::currentDateTime() + "download is done\n");
TCHAR programpath[_MAX_PATH];
GetCurrentDirectory(_MAX_PATH, programpath);
hThis->m_valOriginFolderPath.Format(_T("%s\\"), programpath);
hThis->m_valOriginFolderPath += hThis->m_origFileName;
}
else
{
hThis->Logger(CPrePackagerDlg::currentDateTime() + "s3 download error: " +
get_object_outcome.GetError().GetExceptionName() + " " +
get_object_outcome.GetError().GetMessage() + "\n");
hThis->runSignal = CPrePackagerDlg::RunSignal::STAT_RUN_STOP;
}
}
Aws::ShutdownAPI(options);
}
一个命令行JSON进程 r
jq
使用键$ json_data='{"value1":5.0,"value2":2.5,"value3":"2019-10-24T15:26:00.000Z","modifier":[]}'
$ jq '.value2' <(echo "$json_data")
2.5
访问您感兴趣的值。
此链接总结了为什么您应该不使用正则表达式解析json的原因 (XML / HTML和其他数据结构也是如此 理论可以无限嵌套)
Regex for parsing single key: values out of JSON in Javascript
如果您没有.value2
可用:
您可以使用以下GNU jq
命令:
grep
使用此处详述的正则表达式:
$ echo '{"value1":5.0,"value2":2.5,"value3":"2019-10-24T15:26:00.000Z","modifier":[]}' | grep -zoP '"value2":\s*\K[^\s,]*(?=\s*,)'
2.5
演示: https://regex101.com/r/82J6Cb/1/
如果json没有线性化,这甚至可以工作!!!!
使用"value2":\s*\K[^\s,]*(?=\s*,)
也很直接,即使不是python3,它也应该默认安装在机器上
python
答案 2 :(得分:0)
如果您的数据位于“ d”文件中,请尝试使用gnu sed
sed -E 's/[{,]"\w+":([^,"]+)/\1\n/g ;s/(.*\n).*".*\n/\1/' d
答案 3 :(得分:0)
你可以试试这个:
creds=$(eval aws secretsmanager get-secret-value --region us-east-1 --secret-id dpi/dev/hivemetastore --query SecretString --output text )
passwd=$(/bin/echo "${creds}" | /bin/sed -n 's/.*"password":"\(.*\)",/\1/p' | awk -F"\"" '{print $1}')
当然可以删除 AWK 部分...