特定字段的长度,并以更简单的方式显示记录

发布于 2024-12-11 04:36:15 字数 405 浏览 0 评论 0原文

我的目标是找出第二个字段的长度,如果长度超过五个字符,那么我需要使用 shell 脚本/命令显示整个记录。

echo "From the csv file"
cat latency.csv  |
while read line
do
        latency=`echo $line | cut -d"," -f2 | tr -d " "`
        length=$(echo ${#latency})
        if [ $length -gt 5 ]
        then
                echo $line
        fi
done

我的代码没有任何问题,但作为 UNIX/Linux,我认为应该有一种更简单的方法来做这些事情。

有没有一种这样更简单的方法?

My goal is to find out the length of the second field and if the length is more than five characters, then I need to show the entire record using shell scripts/command.

echo "From the csv file"
cat latency.csv  |
while read line
do
        latency=`echo $line | cut -d"," -f2 | tr -d " "`
        length=$(echo ${#latency})
        if [ $length -gt 5 ]
        then
                echo $line
        fi
done

There is nothing wrong with my code, but being UNIX/Linux, I thought there should be a simpler way of doing such things.

Is there one such simpler method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怎会甘心 2024-12-18 04:36:15
awk -F, 'length($2)>5' file

这应该可以

更新

 awk -F, '{a=$0;gsub(/ /,"",$2);if(length($2)>5)print a}' file
awk -F, 'length($2)>5' file

this should work

updated

 awk -F, '{a=$0;gsub(/ /,"",$2);if(length($2)>5)print a}' file
彡翼 2024-12-18 04:36:15
awk -F, '{
  t = $2
  gsub(/ /, x, t)
  if (length(t) > 5)
    print
  }' latency.csv

或者:

perl -F, -ane'
  print if 
    $F[1] =~ tr/ //dc > 5
  ' latency.csv   
awk -F, '{
  t = $2
  gsub(/ /, x, t)
  if (length(t) > 5)
    print
  }' latency.csv

Or:

perl -F, -ane'
  print if 
    $F[1] =~ tr/ //dc > 5
  ' latency.csv   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文