说我们有一个 tsv 文件
1 2 3
1 2 3
Run Code Online (Sandbox Code Playgroud)
我们想echo $1 $2 $3为每个 tsv 文件行做一些操作。
如何在 bash 中做这样的事情?
这可以做到:
while read -r a b c
do
echo "first = $a second = $b third = $c"
done < file
Run Code Online (Sandbox Code Playgroud)
$ while read -r a b c; do echo "first=$a second=$b third=$c"; done < file
first=1 second=2 third=3
first=1 second=2 third=3
Run Code Online (Sandbox Code Playgroud)
由于分隔符是制表符,因此您无需使用IFS. 如果它是例如 a |,你可以这样做:
$ cat file
1|2|3
1|2|3
$ while IFS='|' read -r a b c; do echo "first=$a second=$b third=$c"; done < file
first=1 second=2 third=3
first=1 second=2 third=3
Run Code Online (Sandbox Code Playgroud)