统计文件中的行数并将其保存在一个bash文件中

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

Counting number of lines in file and saving it in a bash file

问题

for FILE in *.txt; do
  if [ $(wc -l < "$FILE") -eq 10 ]; then
    echo "$FILE" >> saved_names.txt
  fi
done
英文:

I am trying to loop through all the files in a folder and add the file name of those files with 10 lines to a txt file but I don't know how to write the if statement.

As of right now, what I have is:

for FILE in *.txt do if wc $FILE == 10; then &quot;$FILE&quot; &gt;&gt; saved_names.txt fi done

I am getting stuck in how to format the statement that will evaluate to a boolean for the if statement.

I have already tried the if statement as:

if [ wc $FILE != 10 ]

if &quot;wc $FILE&quot; != 10

if &quot;wc $FILE != 10&quot;

as well as other ways but I don't seem to get it right. I know I am new to Bash but I can't seem to find a solution to this question.

答案1

得分: 2

你的代码存在一些问题。以下是翻译好的部分:

  • 要统计文件中的行数,你应该运行"wc -l"命令。然而,该命令会返回行数和文件的名称(例如 - 10 a.txt - 你可以在终端上运行该命令来测试)。要仅获得行数,你需要将文件的名称传递给该命令的标准输入。

  • 在Bash中使用"=="来比较字符串。要比较整数,你应该使用"-eq"(参考这里 https://tldp.org/LDP/abs/html/comparison-ops.html)。

  • 关于括号:要获取"wc"命令的结果,你需要在终端中运行它,并将命令切换到代码中的结果。为此,你需要正确的括号 - $(wc -l)。要将比较的结果作为布尔值接收,你需要使用带有空格的方括号 [ 1 -eq 1 ]。

  • 要使用">>"将文件名保存到另一个文件中,你需要首先将文件名放到标准输出(因为">>"会将标准输出重定向到所选位置)。你可以使用echo命令来实现这一点。

代码应该如下所示:

#!/bin/bash

for FILE in *.txt
do
  if [ "$(wc -l < "$FILE")" -eq 10 ]
  then
    echo "$FILE" >> saved_names.txt
  fi
done
英文:

There are a few problems in your code.

  • To count the number of lines in the file you should run "wc -l" command. However, that command will result in the number of lines and the name of the file (so for example - 10 a.txt - you can test it by running the command on a file in your terminal). To receive only the number of lines you need to pass the file's name to the standard input of that command
  • "==" is used in bash to compare strings. To compare integers as in that case, you should use "-eq" (take a look here https://tldp.org/LDP/abs/html/comparison-ops.html)
  • In terms of brackets: To get the wc command result you need to run it in a terminal and switch the command in the code to the result. To do that, you need correct brackets - $(wc -l). To receive a result of the comparison as a bool, you need to use square brackets with spaces [ 1 -eq 1 ].
  • To save the name of the file in another file using >> you need to first put the name to the standard output (as >> redirect the standard output to the chosen place). To do that you can just use the echo command.

The code should look like this:

#!/bin/bash

for FILE in *.txt
do
  if [ &quot;$(wc -l &lt; &quot;$FILE&quot;)&quot; -eq 10 ]
  then
    echo &quot;$FILE&quot; &gt;&gt; saved_names.txt
  fi
done

答案2

得分: 1

尝试:

for file in *.txt; do
    if [[ $(wc -l < "$file") -eq 10 ]]; then
        printf '%s\n' "$file"
    fi
done >> saved_names.txt

如果要追加文件名,请将 >> 替换为 >

相关文档:

英文:

Try:

for file in *.txt; do
    if [[ $(wc -l &lt; &quot;$file&quot;) -eq 10 ]]; then
        printf &#39;%s\n&#39; &quot;$file&quot;
    fi
done &gt; saved_names.txt

Change &gt; to &gt;&gt; if you want to append the filenames.

Related docs:

答案3

得分: 0

从文件中提取实际行数使用 wc -l $FILE | cut -f1 -d' ',并使用 -eq 运算符:

for FILE in *.txt; do if [ "$(wc -l $FILE | cut -f1 -d' ')" -eq 10 ]; then "$FILE" >> saved_names.txt; fi; done
英文:

Extract the actual number of lines from a file with wc -l $FILE | cut -f1 -d&#39; &#39; and use -eq operator:

for FILE in *.txt; do if [ &quot;$(wc -l $FILE | cut -f1 -d&#39; &#39;)&quot; -eq 10 ]; then &quot;$FILE&quot; &gt;&gt; saved_names.txt; fi; done 

huangapple
  • 本文由 发表于 2023年2月10日 04:04:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403878.html
匿名

发表评论

匿名网友

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

确定