脚本:只变换很长的行


脚本:只变换很长的行
 
比如,要快速的扫描一个文件,看看哪些行过长:
 
1
awk '{if (length($0) > 72) {print $0}}'  filename
   还有一个有趣点的方法,就是使用shell中的$#vamname结构,它会返回任意一个替换了vamname的变量的长度(指内容,不是变量名)。
01
#!/bin/sh
02
 
03
# toolong.sh -- 使用fmt.sh格式化那些超过指定长度的长行
04
 
05
width=72
06
 
07
if [ ! -r "$1" ]; then
08
    echo "Usage: `basename $0` filename" >&2
09
    exit 1
10
fi
11
 
12
while read input
13
do
14
    if [ ${#input} -gt $width ]; then
15
        echo "$input" | fmt.sh
16
    else
17
        echo "$input"
18
    fi
19
done < $1
20
 
21
exit 0
   这个脚本中处理输入文件的方法很有意思。首先用一个简单的 <$1 达到输入文件的目的,然后用一个 read input 将每行都解析下。 
   如果你的shell环境中没有 ${#var} 记法的话,可以使用如下方法代替: 
1
varlength="$(echo "$var" | wc -c)"
但是,wc命令有一个十分让人生厌的特性,它的输出会有一个前导空格,目的是让输出列表排列的漂亮些。为了回避这个讨厌的问题,会有一个微小的改动,就是在通过最后一个管道时,只允许数字通过:[注: 在我的Linux中,没发现作者说的这个特性,并且使用了sed后,可能会有问题,需读者在自己的环境中检验]
1
varlength="$(echo "$var" | wc -c | sed 's/[^:digit:]//')"
运行结果:
 
01
$ toolong ragged.txt
02
So she sat on, with closed eyes, and half believed herself in
03
Wonderland, though she knew she had but to open them again, and
04
all would change to dull reality--the grass would be only rustling
05
in the wind, and the pool rippling to the waving of the reeds--the
06
rattling teacups would change to tinkling sheep-bells, and the
07
Queen's shrill cries to the voice of the shepherd boy--and the
08
sneeze
09
of the baby, the shriek of the Gryphon, and all the other queer
10
noises, would change (she knew) to the confused clamour of the busy
11
farm-yard--while the lowing of the cattle in the distance would
12
take the place of the Mock Turtle's heavy sobs.
13
 
14
Notice that, unlike a standard invocation of fmt, toolong has retained line breaks where possible, so the word "sneeze," 
15
which is on a line by itself in the input file, is also on a line by itself in the output.
 

相关内容

    暂无相关文章