英文:
Fetch value from column name in unix
问题
val=$(curl https://www.geeksforgeeks.org | tr '\n' ' ' | tr -s [[:space::]] | grep -E client_id -e publication_id)
我有一个要求,需要从curl命令中提取特定的值:
示例输入:
client_id:"15647",publication_id:["151","152","153"]
输出:
publication_id:["151,"152","153"]
我想使用grep命令从Unix中的curl命令中提取publication_id的值并存储在变量中。
英文:
val=$(curl https://www.geeksforgeeks.org | tr '\n' ' ' | tr -s [[:space::]] | grep -E client_id -e publication_id)
I have a requirement to fetch the particular value from curl command:
Sample I/P:
client_id:"15647",publication_id:["151","152","153"]
Output:
publication_id:["151,"152","153"]
I want to use grep command to fetch publication_id values from curl command in unix and store in variable
答案1
得分: 1
代码示例中提供了使用[nodejs]、[xidel]、[xpath]和[puppeteer]的解决方案。如评论所述,您需要一个具有[js]意识的工具:
Javascript wget.js
代码(这只是模仿wget
,只输出HTML,由[js]生成):
const puppeteer = require('puppeteer');
(async () => {
var url = process.argv[2];
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
await page.goto(url, { waitUntil: 'networkidle2' });
const html = await page.evaluate(() => document.documentElement.outerHTML);
console.log(html);
browser.close();
})()
代码:
node wget.js 'https://www.geeksforgeeks.org' |
xidel -e '//div[@id="g_id_onload"]/@data-client_id'
输出:
388036620207-3uolk1hv6ta7p3r9l6s3bobifh086qe1.apps.googleusercontent.com
英文:
One solution with [tag:nodejs], [tag:xidel], [tag:xpath] and [tag:puppeteer], as said in comments, you need a [tag:js] aware tool:
Javascript wget.js
code (this is only to mimic wget
, it's output only HTML, and the one generated by [tag:js] too):
const puppeteer = require('puppeteer');
(async () => {
var url = process.argv[2];
const browser = await puppeteer.launch({headless: true});
const page = await browser.newPage();
await page.goto(url, { waitUntil: 'networkidle2' });
const html = await page.evaluate(() => document.documentElement.outerHTML);
console.log(html);
browser.close();
})()
</br>
code:
node wget.js 'https://www.geeksforgeeks.org' |
xidel -e '//div[@id="g_id_onload"]/@data-client_id'
Output:
388036620207-3uolk1hv6ta7p3r9l6s3bobifh086qe1.apps.googleusercontent.com
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论