用于读取文件的 Bash 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3267252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 19:28:59  来源:igfitidea点击:

Bash script to read a file

linuxbash

提问by hfranco

Not sure why the last line does not cut the " from the script:

不知道为什么最后一行没有从脚本中删除“:

#!/bin/bash

FILENAME=
while read line
do
cut -d '"' -f2
echo $line
done < $FILENAME

$ cat file

"1" test
"2" test
"3" test
"4" test
"5" test

If I run this script with the following command:

如果我使用以下命令运行此脚本:

$ ./test file

2
3
4
5
"1" test

回答by Jonathan Leffler

The loop executes once.

循环执行一次。

  • It reads "1" testinto variable $line.
  • It executes cut -d '"' -f2which reads lines 2-5 of the file (because that is the standard input at the time) and prints the number.
  • It echoes what it read as the first line.
  • 它读"1" test入变量$line
  • 它执行cut -d '"' -f2读取文件的第 2-5 行(因为这是当时的标准输入)并打印数字。
  • 它呼应了它作为第一行读取的内容。

Fix:

使固定:

cut -d '"' -f2 $FILENAME

If, on the other hand, you want to get the numbers into a variable, you could do this in a variety of ways, including:

另一方面,如果您想将数字放入变量中,您可以通过多种方式实现,包括:

cut -d '"' -f2 $FILENAME |
while read number
do  # What you want
    echo $number
done

or:

或者:

while read line
do
    number=$(echo "$line" | cut -d '"' -f2)
    echo $number
done

回答by Paused until further notice.

Bash can do all the work for you. There's no need for cut:

Bash 可以为您完成所有工作。没有必要cut

#!/bin/bash

FILENAME=
while read -r -a line
do
    echo ${line//\"}
done < "$FILENAME"

That reads the line into an array, then treats the array as a scalar which gives you the first element. Then the brace expansion in the echostrips out the quotation marks.

这会将行读入一个数组,然后将该数组视为一个标量,该标量为您提供第一个元素。然后括号中的扩展echo去掉引号。

Or you can let cutdo all the work and give Bash a long coffee break:

或者你可以让cut所有的工作做完,给 Bash 一个长时间的咖啡休息时间:

FILENAME=
cut -d '"' -f2 "$FILENAME"

Always quote variables that contain filenames.

始终引用包含文件名的变量。

回答by ghostdog74

like dennis mentioned, there's no need to use external commands

就像丹尼斯提到的,没有必要使用外部命令

$ while read -r line; do  set -- $line;  echo ${1//\"/}; done<file
1
2
3
4
5

But external commands runs faster if you have very large files.

但是如果您有非常大的文件,外部命令运行速度会更快。

$ cut -d'"' -f2 file
1
2
3
4
5
$ awk -F'"' '{print }' file
1
2
3
4
5
$ sed 's/^"//;s/".*//' file
1
2
3
4
5

回答by Chris J

Jonathan Leffler's given you a simpler method (which will also be more efficient), but in case this is a simplification for something you're going to expand on (where just calling cut won't do what you want), and just to demonstrate the principle anyway, your code would need to be fixed up to feed each line to stdin explicitly as follows:

乔纳森莱夫勒给了你一个更简单的方法(这也会更有效),但如果这是你要扩展的东西的简化(只调用 cut 不会做你想做的事),只是为了演示无论如何,您的代码需要修复以将每一行明确地提供给标准输入,如下所示:

#!/bin/bash

FILENAME=
while read line
do
    echo $line | cut -d '"' -f2
done < $FILENAME