逐行读取文件,将值分配给变量

本文翻译自:Read a file line by line assigning the value to a variable

I have the following .txt file: 我有以下.txt文件:

Marco
Paolo
Antonio

I want to read it line-by-line, and for each line I want to assign a .txt line value to a variable. 我想逐行阅读它,并且我想为每行分配一个.txt行值给一个变量。 Supposing my variable is $name , the flow is: 假设我的变量是$name ,流程是:

  • Read first line from file 从文件中读取第一行
  • Assign $name = "Marco" 分配$name =“ Marco”
  • Do some tasks with $name $name做一些任务
  • Read second line from file 从文件中读取第二行
  • Assign $name = "Paolo" 分配$name =“ Paolo”

#1楼

参考:https://stackoom.com/question/jrFV/逐行读取文件-将值分配给变量


#2楼

The following reads a file passed as an argument line by line: 以下内容逐行读取作为参数传递的文件:

while IFS= read -r line; do
    echo "Text read from file: $line"
done < my_filename.txt

This is the standard form for reading lines from a file in a loop. 这是在循环中从文件读取行的标准格式 Explanation: 说明:

  • IFS= (or IFS='' ) prevents leading/trailing whitespace from being trimmed. IFS= (或IFS='' )可防止对前导/尾随空格进行裁剪。
  • -r prevents backslash escapes from being interpreted. -r防止反斜杠转义被解释。

Or you can put it in a bash file helper script, example contents: 或者,您可以将其放在bash文件帮助程序脚本中,示例内容:

#!/bin/bash
while IFS= read -r line; do
    echo "Text read from file: $line"
done < "$1"

If the above is saved to a script with filename readfile , it can be run as follows: 如果以上内容保存到文件名为readfile的脚本中,则可以按以下方式运行:

chmod +x readfile
./readfile filename.txt

If the file isn't a standard POSIX text file (= not terminated by a newline character), the loop can be modified to handle trailing partial lines: 如果该文件不是标准的POSIX文本文件 (=不以换行符终止),则可以修改循环以处理尾随的分行:

while IFS= read -r line || [[ -n "$line" ]]; do
    echo "Text read from file: $line"
done < "$1"

Here, || [[ -n $line ]] 在这里, || [[ -n $line ]] || [[ -n $line ]] prevents the last line from being ignored if it doesn't end with a \\n (since read returns a non-zero exit code when it encounters EOF). || [[ -n $line ]]防止最后一行不以\\n结尾的情况被忽略(因为read遇到EOF时返回非零退出代码)。

If the commands inside the loop also read from standard input, the file descriptor used by read can be chanced to something else (avoid the standard file descriptors ), eg: 如果循环内的命令也从标准输入中读取,则read所使用的文件描述符可能会出现其他情况(避免使用标准文件描述符 ),例如:

while IFS= read -r -u3 line; do
    echo "Text read from file: $line"
done 3< "$1"

(Non-Bash shells might not know read -u3 ; use read <&3 instead.) (非Bash shell可能不知道read -u3 ;而应使用read <&3 。)


#3楼

Using the following Bash template should allow you to read one value at a time from a file and process it. 使用以下Bash模板应该可以让您一次从文件中读取一个值并进行处理。

while read name; do
    # Do what you want to $name
done < filename

#4楼

I encourage you to use the -r flag for read which stands for: 我鼓励您使用-r标志进行read ,它代表:

-r  Do not treat a backslash character in any special way. Consider each
    backslash to be part of the input line.

I am citing from man 1 read . 我从第man 1 read书中引用。

Another thing is to take a filename as an argument. 另一件事是将文件名作为参数。

Here is updated code: 这是更新的代码:

#!/usr/bin/bash
filename="$1"
while read -r line; do
    name="$line"
    echo "Name read from file - $name"
done < "$filename"

#5楼

Use: 采用:

filename=$1
IFS=$'\n'
for next in `cat $filename`; do
    echo "$next read from $filename" 
done
exit 0

If you have set IFS differently you will get odd results. 如果您对IFS不同的设置,您将得到奇怪的结果。


#6楼

#! /bin/bash
cat filename | while read LINE; do
    echo $LINE
done
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值