有道面试题打印1 100的奇数(方法越多越好)
1. seq 1 2 100
2. seq 100 | awk 'i=!i'
3. i=1
while [ $i -lt 100 ]
do
echo $i
let i=i+2
done
4. for((i=1;i<=100;i++))
do
echo $i
let i+=1
done
5. seq 1 100 |sed -n '{p;n}'
6. seq 1 100 |sed -n '1~2p'
sed
1)
奇数行:sed -n'1~2p' urfile
偶数行:sed -n'2~2p' urfile
2)
偶数行:sed-n 'n;p' urfile
奇数行:sed-n '$!N;P' urfile
3)
奇数行:sed'n;d' urfile
偶数行:sed'1d;n;d' urfile
4)
偶数行:sed -n 'n;p' urfile
奇数行:sed -n'p;n' urfile
5)
奇数行:sed '0~2d' urfile
偶数行:sed '1~2d' urfile
awk
1)
奇数行:awk'NR%2==1{print $0}' urfile
偶数行:awk'NR%2==0{print $0}' urfile
2)
偶数行:awk 'NR%2'urfile
奇数行:awk'!(NR%2)' urfile
3)
奇数行:awk'i=i?0:1' urfile
偶数行:awk'!(i=i?0:1)' urfile
4)
偶数行:awk'!(i=!i)' urfile
奇数行:awk 'i=!i'urfile
5)
奇数行:awk '++i%2'urfile
偶数行:awk 'i++%2'urfile
6)
偶数行:awk'!and(FNR,1)' urfile
奇数行:awk'and(FNR,1)' urfile
7)
奇数行:awk 'getline p||1' urfile
偶数行:awk 'getline p||2' urfile
Perl
1)
奇数行:perl -ne 'print if $. % 2' urfile
偶数行:perl -ne 'print if not $. % 2' urfile
2)
奇数行:perl -pe '<>' urfile
偶数行:perl -pe '$_ = <>' urfile
转载于:https://blog.51cto.com/sukaka/911814