英文:
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:
- take the second part of this pipe delimited file and
- produce a file of
${char_count}|${message}
- 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='|' read -r _ data _; do
printf '%d|%s\n' "${#data}" "$data"
done <cksum.psv
That way we're invoking no external tools at all and keeping all the logic inside your shell.
-
IFS='|' 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 intodata
. UsingIFS='|'
makes it split on pipes; using-r
prevents backslashes from being treated as syntax rather than data duringread
's operation. -
printf '%d|%s\n' "${#data}" "$data"
has several moving parts:${#data}
expands to the length of the content ofdata
"$data"
expands to the precise content of the data variable (being in double quotes, suppressing word-splitting and glob expansion).
-
<cksum.psv
redirects stdin for the duration of thewhile 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'|' -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|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..
' |
xargs -0 -n 1 sh -c 'echo "${#1}|"' _ | sort -t '|' -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..
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论