比如文本如下:
1
2
3
a
b
c
合并后的结果是:
1 2 3
a b c
答案:
sed实现:
sed -n 'N;N;s/\n/ /g'p 1.txt
awk实现:
awk '{if(NR%3!=0)ORS=" ";else ORS="\n"}1' 1.txt
1
2
3
a
b
c
合并后的结果是:
1 2 3
a b c
答案:
sed实现:
sed -n 'N;N;s/\n/ /g'p 1.txt
awk实现:
awk '{if(NR%3!=0)ORS=" ";else ORS="\n"}1' 1.txt
0
#!/bin/bash
#write by 2016-1-31
>2.txt
>3.txt
n=$(cat /tmp/1.txt|sed -n ''p|wc -l)
for i in $(seq 1 $n)
do
if [ $(($i%3)) != 0 ]
then
sed -n "$i"p /tmp/1.txt >> /tmp/2.txt
else
sed -n "$i"p /tmp/1.txt >> /tmp/2.txt
cat /tmp/2.txt |xargs >> /tmp/3.txt
> /tmp/2.txt
fi
done
if [ $(($n%3)) -gt 0 ]
then
cat /tmp/2.txt |xargs >> /tmp/3.txt
> /tmp/2.txt
fi
cat /tmp/3.txt > /tmp/1.txt
#write by 2016-1-31
>2.txt
>3.txt
n=$(cat /tmp/1.txt|sed -n ''p|wc -l)
for i in $(seq 1 $n)
do
if [ $(($i%3)) != 0 ]
then
sed -n "$i"p /tmp/1.txt >> /tmp/2.txt
else
sed -n "$i"p /tmp/1.txt >> /tmp/2.txt
cat /tmp/2.txt |xargs >> /tmp/3.txt
> /tmp/2.txt
fi
done
if [ $(($n%3)) -gt 0 ]
then
cat /tmp/2.txt |xargs >> /tmp/3.txt
> /tmp/2.txt
fi
cat /tmp/3.txt > /tmp/1.txt
编辑回复