从Unix中提取列名中的值

huangapple go评论59阅读模式
英文:

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 &#39;https://www.geeksforgeeks.org&#39; |
    xidel -e &#39;//div[@id=&quot;g_id_onload&quot;]/@data-client_id&#39; 

Output:

388036620207-3uolk1hv6ta7p3r9l6s3bobifh086qe1.apps.googleusercontent.com

huangapple
  • 本文由 发表于 2023年2月16日 15:37:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469116.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定