把文件每隔三行合并成一行

回复 收藏
比如文本如下:
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
2015-08-12 16:38 举报
已邀请:
0

loveyouhyf

赞同来自:

太精悍了
0

lyhabc

赞同来自:

#!/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

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: