lost and found ( for me ? )

shell script : odd line , even line

 # cat zzz.txt

1 hello
2 hi
3 hello
4 hi
5 hello
6 hi
7 hello
8 hi
9 hello
10 hi

- 奇数  ( head -1 , -3 , -5 ... )

# head -1 zzz.txt | tail -1
1 hello
# head -3 zzz.txt | tail -1
3 hello

- 偶数 ( head -2 , -4 , -6 )

# head -2 zzz.txt | tail -1
2 hi
# head -4 zzz.txt | tail -1
4 hi

- シェルスクリプト

# cat abc.sh
#!/bin/sh

i=1
j=2
k=`wc -l zzz.txt | awk '{print $1+1}'`

echo "odd line"

while [ $i -lt $k ];
do
        head -$i zzz.txt | tail -1
        i=`expr $i + 2`
done

echo ""

echo "even line"

while [ $j -lt $k ];
do
        head -$j zzz.txt | tail -1
        j=`expr $j + 2`
done


# ./abc.sh
odd line
1 hello
3 hello
5 hello
7 hello
9 hello

even line
2 hi
4 hi
6 hi
8 hi
10 hi

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.