在Unix Shell脚本中逐行(带空格)读取文件-问题 [英] Reading file line by line (with space) in Unix Shell scripting - Issue

查看:2267
本文介绍了在Unix Shell脚本中逐行(带空格)读取文件-问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Unix shell脚本中逐行读取文件.行可以包含前导和尾随空格,我也想在行中读取这些空格. 我尝试使用"while read line",但read命令正在从行:(删除空格字符 例如文件中的行是:-

I want to read a file line by line in Unix shell scripting. Line can contain leading and trailing spaces and i want to read those spaces also in the line. I tried with "while read line" but read command is removing space characters from line :( Example if line in file are:-

abcd efghijk
 abcdefg hijk

行应理解为:- 1) "abcd efghijk" 2) " abcdefg hijk"

line should be read as:- 1) "abcd efghijk" 2) " abcdefg hijk"

我尝试过的是这样(不起作用):-

What I tried is this (which not worked):-

while read line
do
   echo $line
done < file.txt

我要在其中包含空格和制表符的行. 请提出一种方法.

I want line including space and tab characters in it. Please suggest a way.

推荐答案

尝试一下,

IFS=''
while read line
do
echo $line
done < file.txt

来自man bash

IFS - The Internal Field Separator that is used for word
splitting after expansion and to split lines into words
with  the  read  builtin  command. The default value is
``<space><tab><newline>''

这篇关于在Unix Shell脚本中逐行(带空格)读取文件-问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆