shell - 每行文件unix中的字节数

标签 shell unix ksh

我想计算文件中的所有行,行中的字节数大于一个值(比如 10)。我该怎么做?

我尝试使用 cat file | awk'length($0)>10'但这给了我所有字符数大于 10 的行。我想计算行中的字节数。

我写了下面的代码,但它不起作用。它返回一些乱码输出:

#!/bin/ksh
file="a.txt"
while read line
do
    a=`wc -c "${line}"|awk {'print $1'}`
    if [ $a -ne 493]; then
    echo "${line}"
    fi
done <"$file"

最佳答案

你的做法很好,只是你要做a=$(wc -c <<< "$line")a=$(echo "$line" | wc -w) , 无需管道到 awk .另外,请注意在 493 之后需要一个额外的空间在 if健康)状况。

全部一起:

#!/bin/ksh
file="a.txt"
while read line
do
    a=$( echo -n "$line" | wc -c) # echo -n to prevent counting new line
    if [ "$a" -ne 493 ]; then
      echo "${line}"
    fi
done <"$file"

关于shell - 每行文件unix中的字节数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27800440/

相关文章:

linux - KornShell 脚本中的 if 语句

shell - 将双引号中的命令行参数传递给 curl

linux - 需要检查我的机器上一直以来最常用的 Linux 应用程序

linux - 尝试在 sed 中打印每行的一部分

json - 使用 bash 找到匹配的字符串键并在 json 文件中增加其值

bash - 在Linux脚本中,是否可以在同一个进程中执行多个命令?

linux - Bash脚本运行顺序,为什么它会对读取命令进行排队?

c - 在 shell 中显示环境变量时出现段错误

linux - 为什么 bash 在通过 make 执行时不支持 !(...) 模式(w/SHELL :=/bin/bash)?

python - 使用 Python 通过 ssh 在远程主机上执行本地 shell 函数