2013-08-18 151 views
0

行的原始内容我有一个数据文件,内容如下:Unix的:如何从文件中读取

department: customer service section: A 
department: marketing   section: A 
department: finance   section: A 

当我读每一行,我会使用提取命令cut部门名称。

不幸的是,该程序会自动trim所有多余的空间和

因此我剪的部门名称不正确。

cat dept.dat | while read line 
do 
    echo $line 
    echo $line | cut -c 12-29 
done 

例如原线路

department: marketing   section: A 

而程序会将此行:

department: marketing section: A 

我怎样才能读取的行不修整所有多余的空间?

非常感谢。

+0

监守我需要实现一些业务逻辑,就需要读取线 – Newbiee

回答

2

cut不会修改空格。如果你看看输出,你会发现在输入cut之前,空白字符已经被修剪过了。

无需whileread,只需直接通过文件名来cut

/tmp >cat dept.dat 
department: customer service section: A 
department: marketing   section: A 
department: finance   section: A  
/tmp >cut -c 12-29 dept.dat 
customer service 
marketing   
finance 
+0

文件中的行,如果由于其他原因,OP需要使用循环,引用引用像'“$ line”'。祝你们好运。 – shellter

+0

啊,是的,我误解了他的帖子。 'echo“$ line”'是* real *解决方案。 – sshaw

+0

感谢所有提供建议的人。它现在解决了我的问题。 – Newbiee