Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何在读取.dat文件时消除除所需数字以外的所有内容?_Linux_Unix_Sh - Fatal编程技术网

Linux 如何在读取.dat文件时消除除所需数字以外的所有内容?

Linux 如何在读取.dat文件时消除除所需数字以外的所有内容?,linux,unix,sh,Linux,Unix,Sh,我有一个.dat文件,它包含以下数据: count ------- 234 (1 row) 我只希望将234作为整数检索,并希望将其存储在Shell脚本文件中的变量中 我使用了以下命令: WORKFLOW_STATUS_COUNT=`cat status_count.dat | tr -d " "` 但它的输出是: count ------- 234 (1 row) 但预期值为234整数值。您可以使用: WORKFLOW_STATUS_COUNT=`cat status_count.da

我有一个.dat文件,它包含以下数据:

 count ------- 234 (1 row)
我只希望将234作为整数检索,并希望将其存储在Shell脚本文件中的变量中

我使用了以下命令:

WORKFLOW_STATUS_COUNT=`cat status_count.dat | tr -d " "`
但它的输出是:

count ------- 234 (1 row)
但预期值为234整数值。

您可以使用:

WORKFLOW_STATUS_COUNT=`cat status_count.dat | cut -d" " -f 3 `
cut
使用
spacebar
delimeter拆分行,然后仅选择第三列

如果文件来自
Windows
source,您可以尝试使用
dos2unix

WORKFLOW_STATUS_COUNT=`cat status_count.dat | dos2unix | cut -d" " -f 3 `
您可以使用:

WORKFLOW_STATUS_COUNT=`cat status_count.dat | cut -d" " -f 3 `
cut
使用
spacebar
delimeter拆分行,然后仅选择第三列

如果文件来自
Windows
source,您可以尝试使用
dos2unix

WORKFLOW_STATUS_COUNT=`cat status_count.dat | dos2unix | cut -d" " -f 3 `

您可以使用
while read
子句来获得结果。这只适用于数据列始终一致的情况。例如,如果您的数据列是这样的
count 258(1行)'(count和258之间的连续空格)
,则不起作用

While read count_var hyphen your_var other; do
    echo $your_var
    Do_something
Done < filename.dat
读取计数变量时,将您的变量与其他变量连字符;做
echo$your_var
做点什么
完成
您可以在阅读时使用
子句来获得结果。这只适用于数据列始终一致的情况。例如,如果您的数据列是这样的
count 258(1行)'(count和258之间的连续空格)
,则不起作用

While read count_var hyphen your_var other; do
    echo $your_var
    Do_something
Done < filename.dat
读取计数变量时,将您的变量与其他变量连字符;做
echo$your_var
做点什么
完成
我使用了上面的命令,但得到的输出是------intead of number..@pavansing将
3
更改为
4
,也许您有一个额外的
空格键
当我将3更改为4时,我得到的输出是--------234您是否为我们复制了准确的输入?@OhadEytan:-
cut
无法识别多字符分隔符。您需要使用
awk
我已经使用了上述命令,但是得到的输出是------intead of number..@pavansing将
3
更改为
4
,也许您有一个额外的
空格键
当我将3更改为4时,我得到的输出是--------234您是否为我们复制了准确的输入?@OhadEytan:-
cut
无法识别多字符分隔符。您需要使用
awk