我有一个数据文件,内容如下:
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
e.g。原始行是
department: marketing section: A
虽然程序将此行视为:
department: marketing section: A
如何在不修剪所有冗余空间的情况下读取线路?
非常感谢。
答案 0 :(得分:2)
cut
不会修改空格。如果查看输出,您会看到空格在被输入cut
之前已经被修剪过。
无需while
和read
,只需将文件名直接传递给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