读取文件并写入行长度和字符位置。为什么替代线路会被处理?

时间:2016-06-10 11:53:34

标签: bash line

我的temp.txt文件包含: -

 x [deleted]         (none)     -> origin/redeem_deal0
 x [deleted]         (none)     -> origin/redeem_deal1
 x [deleted]         (none)     -> origin/redeem_deal2
 x [deleted]         (none)     -> origin/redeem_deal3

编写代码以获取每行的/和行长度的位置。

cat temp.txt | while read line
do
    echo $line    
    LINE_LEN="$(line | wc -c)"
    echo $LINE_LEN
    POS_FWD_SLASH=$(line | grep -b -o / | awk 'BEGIN {FS=":"}{print$
    echo $POS_FWD_SLASH
done

输出: -

manish@rigved:~/beawel/wwwnew$ /bin/sh deleted.sh 
 x [deleted]         (none)     -> origin/redeem_deal0

55
41
 x [deleted]         (none)     -> origin/redeem_deal3

1

如果我将LINE_LEN中的代码注释掉,并且只留下" echo $ line"它打印所有4行。但不是上面的代码。请帮助我处理所有4条线。

1 个答案:

答案 0 :(得分:0)

${#str} $str的长度不需要wc -c

每当你对文件进行while循环时,你应该停下来考虑awk是否适合你的工具:

% awk '{print $0 RS length($0) RS index($0, "/")};' temp.txt
 x [deleted]         (none)     -> origin/redeem_deal0
54
42
 x [deleted]         (none)     -> origin/redeem_deal1
54
42
 x [deleted]         (none)     -> origin/redeem_deal2
54
42
 x [deleted]         (none)     -> origin/redeem_deal3
54
42