【问题标题】:Read every line of file in bash and awk读取 bash 和 awk 中的每一行文件
【发布时间】:2014-09-10 22:28:10
【问题描述】:

我有一个文件(test.txt),其内容类似于

/the/path/foo.txt  foo.txt 
/the/paths/doo.txt  doo.txt 
/the/path/foo.pl  foo.pl 
/the/paths/doo.pl  doo.pl 
/the/path1/soo.csv  soo.csv
/the/path2/moo.csv  moo.csv

我想在 bash 脚本中将 awk {print $1} 保存到变量 1 中,并将 awk {print $2} 保存到变量 2 中,即变量 1 获取 /the/path/foo.txt ,变量 2 获取 foo.txt 等等

谢谢

【问题讨论】:

  • 定义“等等”。您在 line2 上做什么以及在哪里做事?
  • 检查 bash 手册页中的“read”命令:read variable1 variable2

标签: bash shell awk


【解决方案1】:

您可以使用内置的read 来捕获字段并将其分配给变量(假设您的路径中没有空格):

while read -r var1 var2; do
    # do something with variables
done < text.txt
猜你喜欢
  • 2013-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-07
  • 2015-02-04
  • 1970-01-01
  • 2015-02-11
  • 2016-06-16
相关资源
最近更新 更多