xargs 有时会生成 {},为什么?

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

why is xargs producing {} sometimes?

问题

It seems like you're encountering occasional {} in your output when using the xargs command. This issue is likely due to the presence of empty lines in your input data. To address this, you can modify your command to filter out empty lines before processing with xargs. Here's an updated command:

cut -d'|' -f2 cksum.psv | tr '\n' '
cut -d'|' -f2 cksum.psv | tr '\n' '\0' | grep -v '^$' | xargs -0 -I {} sh -c 'printf "%d|%s\n" $(echo "{}" | wc -c) "{}"'
'
| grep -v '^$' | xargs -0 -I {} sh -c 'printf "%d|%s\n" $(echo "{}" | wc -c) "{}"'

By adding grep -v '^$', you filter out empty lines before passing the data to xargs, which should prevent the occasional {} in your output.

英文:

I have a file that looks like this:

3839440388|Execution error for request {.request_id}. Reason: {.ess_code} Job logic indicated a system error occurred while executing an asynchronous java job for request {.request_id}. Job error is: {.job_code} Failed to add JDBC connection to the connection pool, connection pool is full..
126216303|Execution error for request {.request_id}. Reason: {.ess_code} Job logic indicated a business error occurred while executing an asynchronous java job for request {.request_id}. Job error is: BIP job failed
2651334677|Execution error for request {.request_id}. Reason: {.ess_code} Job logic indicated a system error occurred while executing an asynchronous java job for request {.request_id}. Job error is: Failure during Schedule Item Import job..
2952592408|Execution error for request {.request_id}. Reason: {.ess_code} Job logic indicated a system error occurred while executing an asynchronous java job for request {.request_id}. Job error is: javax.xml.ws.WebServiceException
719205600|Execution error for request {.request_id}. Reason: {.ess_code} Job logic indicated a business error occurred while executing an asynchronous java job for request {.request_id}. Job error is: oracle.xdo.servlet.CreateException
1495326131|Execution error for request {.request_id}. Reason: Transfered zip file failed validation
3069376900|Execution error for request {.request_id}. Reason: Spawned job for request {.request_id} produced the business error exit code: BIZ_ERROR
2503345575|Execution error for request {.request_id}. Reason: Could not update entity indexing state for entity with uuid: '{.uuid}' in meta model version with uuid '{.uuid}': method [PUT], host
2184383281|Execution error for request {.request_id}. Reason: {.ess_code} Job logic indicated a system error occurred while executing an asynchronous java job for request {.request_id}. Job error is: oracle.j2ee.ws.client.jaxws.JRFSOAPFaultException: Client received SOAP Fault from server
3847951072|Execution error for request {.request_id}. Reason: {.ess_code} Job logic indicated a business error occurred while executing an asynchronous java job for request {.request_id}. Job error is: <env:Body xmlns:env=http://schemas.xmlsoap.org/soap/envelope/>

I am trying to do the following:

  1. take the second part of this pipe delimited file and
  2. produce a file of ${char_count}|${message}
  3. Then find the largest message with sort -nr | tail -1

Here is the command I ran:

cut -d'|' -f2 cksum.psv | tr '\n' '
cut -d'|' -f2 cksum.psv | tr '\n' '\0' | xargs -0 -I {} sh -c 'printf "%d|%s\n" $(echo "${1}" | wc -c) "${1}"' _ {}
' | xargs -0 -I {} sh -c 'printf "%d|%s\n" $(echo "" | wc -c) ""' _ {}

But, it occasionally produces a {} which I don't understand:

Output:

3|{}
206|Execution error for request {.request_id}. Reason: {.ess_code} Job logic indicated a business error occurred while executing an asynchronous java job for request {.request_id}. Job error is: BIP job failed
231|Execution error for request {.request_id}. Reason: {.ess_code} Job logic indicated a system error occurred while executing an asynchronous java job for request {.request_id}. Job error is: Failure during Schedule Item Import job..
222|Execution error for request {.request_id}. Reason: {.ess_code} Job logic indicated a system error occurred while executing an asynchronous java job for request {.request_id}. Job error is: javax.xml.ws.WebServiceException
226|Execution error for request {.request_id}. Reason: {.ess_code} Job logic indicated a business error occurred while executing an asynchronous java job for request {.request_id}. Job error is: oracle.xdo.servlet.CreateException
89|Execution error for request {.request_id}. Reason: Transfered zip file failed validation
138|Execution error for request {.request_id}. Reason: Spawned job for request {.request_id} produced the business error exit code: BIZ_ERROR
184|Execution error for request {.request_id}. Reason: Could not update entity indexing state for entity with uuid: '{.uuid}' in meta model version with uuid '{.uuid}': method [PUT], host
3|{}
254|Execution error for request {.request_id}. Reason: {.ess_code} Job logic indicated a business error occurred while executing an asynchronous java job for request {.request_id}. Job error is: <env:Body xmlns:env=http://schemas.xmlsoap.org/soap/envelope/>

Why is it producing an occassional {}.
If I leaved off -I {} then I only get 1 line of output:

281|Execution error for request {.request_id}. Reason: {.ess_code} Job logic indicated a system error occurred while executing an asynchronous java job for request {.request_id}. Job error is: {.job_code} Failed to add JDBC connection to the connection pool, connection pool is full..

答案1

得分: 1

考虑将整个流程替换为以下内容:

while IFS='|' read -r _ data _; do
  printf '%d|%s\n' "${#data}" "$data"
done < cksum.psv

这样我们不会调用任何外部工具,将所有逻辑都保持在你的 shell 内部。

  • IFS='|' read -r _ data _ 会丢弃第一列(将其放入变量 _),以及第三列及以后的内容(将它们放入 _),并将第二列放入 data 中。使用 IFS='|' 会根据竖线进行拆分;使用 -r 会阻止反斜杠被视为语法而不是数据在 read 操作期间。

  • printf '%d|%s\n' "${#data}" "$data" 有几个组成部分:

    • ${#data} 展开为 data 的内容长度
    • "$data" 展开为数据变量的确切内容(由于在双引号中,会禁止单词拆分和通配符扩展)。
  • < cksum.psv 会在 while read 循环的持续时间内重定向标准输入。

英文:

Consider replacing the entirety of the pipeline with the following:

while IFS=&#39;|&#39; read -r _ data _; do
  printf &#39;%d|%s\n&#39; &quot;${#data}&quot; &quot;$data&quot;
done &lt;cksum.psv

That way we're invoking no external tools at all and keeping all the logic inside your shell.

  • IFS=&#39;|&#39; read -r _ data _ discards the first column (by putting it into the variable _) and the third column and onward (by putting those into _), and puts the second column into data. Using IFS=&#39;|&#39; makes it split on pipes; using -r prevents backslashes from being treated as syntax rather than data during read's operation.

  • printf &#39;%d|%s\n&#39; &quot;${#data}&quot; &quot;$data&quot; has several moving parts:

    • ${#data} expands to the length of the content of data
    • &quot;$data&quot; expands to the precise content of the data variable (being in double quotes, suppressing word-splitting and glob expansion).
  • &lt;cksum.psv redirects stdin for the duration of the while read loop.

答案2

得分: 0

不回答问题,只提供翻译:

这并没有回答我最初的问题,即为什么有时会得到 {},但它确实产生了期望的结果:

cut -d '|' -f2 cksum.psv | tr '\n' '
cut -d '|' -f2 cksum.psv | tr '\n' '\0' | 
xargs -0 -n 1 sh -c 'echo "${#1}|${1}"' _ | sort -t '|' -n  | tail -1
280|执行请求 {.request_id} 时发生执行错误。原因:{.ess_code} 作业逻辑指示在执行异步 Java 请求时发生系统错误。作业错误是:{.job_code} 无法将 JDBC 连接添加到连接池,连接池已满。
'
|
xargs -0 -n 1 sh -c 'echo "${#1}|"' _ | sort -t '|' -n | tail -1 280|执行请求 {.request_id} 时发生执行错误。原因:{.ess_code} 作业逻辑指示在执行异步 Java 请求时发生系统错误。作业错误是:{.job_code} 无法将 JDBC 连接添加到连接池,连接池已满。
英文:

It doesn't answer my original question as to why I get {} sometimes, but it does produced the desired results:

cut -d&#39;|&#39; -f2 cksum.psv | tr &#39;\n&#39; &#39;
cut -d&#39;|&#39; -f2 cksum.psv | tr &#39;\n&#39; &#39;\0&#39; | 
xargs -0 -n 1 sh -c &#39;echo &quot;${#1}|${1}&quot;&#39; _ | sort -t &#39;|&#39; -n  | tail -1
280|Execution error for request {.request_id}. Reason: {.ess_code} Job logic indicated a system error occurred while executing an asynchronous java job for request {.request_id}. Job error is: {.job_code} Failed to add JDBC connection to the connection pool, connection pool is full..
&#39; | xargs -0 -n 1 sh -c &#39;echo &quot;${#1}|&quot;&#39; _ | sort -t &#39;|&#39; -n | tail -1 280|Execution error for request {.request_id}. Reason: {.ess_code} Job logic indicated a system error occurred while executing an asynchronous java job for request {.request_id}. Job error is: {.job_code} Failed to add JDBC connection to the connection pool, connection pool is full..

huangapple
  • 本文由 发表于 2023年6月15日 08:14:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76478325.html
匿名

发表评论

匿名网友

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

确定