Chinaunix
标题:
将一行变成多行
[打印本页]
作者:
151wqooo
时间:
2013-05-30 14:28
标题:
将一行变成多行
原文是:
[root@wqk1 mnt]# cat datafile
northwest
western
southwest
southern
southeast
eastern
northeast
north
central
NW
WE
SW
SO
SE
EA
NE
NO
CT
Charles Main
Sharon Gray
Lewis Dalsass
Suan Chin
Patricia Hemenway
TB Savage
AM Main Jr.
Margot Weber
Ann Stephens
3.0
5.3
2.7
5.1
4.0
4.4
5.1
4.5
5.7
.98
.97
.8
.95
.7
.84
.94
.89
.94
3
5
2
4
4
5
3
5
5
34
23
18
15
17
20
13
9
13
====================
想要得到的结果是:
BaiduShurufa_2013-5-30_14-27-58.png
(16.92 KB, 下载次数: 49)
下载附件
2013-05-30 14:28 上传
怎么用shell实现????
作者:
seesea2517
时间:
2013-05-30 14:51
[seesea@UC ll]$ cat file | sed '/^$/d' | split -l9; paste -d'|' x* | column -t -s'|'
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
复制代码
作者:
seesea2517
时间:
2013-05-30 14:52
加上删除中间文件的 rm x*:
cat file | sed '/^$/d' | split -l9; paste -d'|' x* | column -t -s'|'; rm x*
复制代码
作者:
kk5234
时间:
2013-05-30 14:54
awk '/^$/{n=0;next}{++n;a[n]=a[n]!=""?a[n]" "$0:$0}END{for(i=1;i<=n;i++)print a[i]}' file
复制代码
作者:
dn833
时间:
2013-05-30 15:49
for i in $(echo {0..10});do awk -v line=$i 'a++%10==line{printf $1" "}' a && echo "";done|column -t
复制代码
作者:
yestreenstars
时间:
2013-05-30 15:50
@seesea2517
学习了,格式很整齐~
@kk5234
用awk就格式不好搞啊~
作者:
kk5234
时间:
2013-05-30 16:01
@yestreenstars
是啊,不好对齐。有好办法没?
作者:
seesea2517
时间:
2013-05-30 16:14
回复
7#
kk5234
格式如果要在awk里用的话,可能用printf好控制一点。不过管道输出给column专业处理(专业通下水道,专业刻章,专业办证啥的都很专业)也可以:
[seesea@UC ~]$ awk '/^$/{n=0;next}{++n;a[n]=a[n]!=""?a[n]"|"$0:$0}END{for(i=1;i<=n;i++)print a[i]}' file | column -t -s'|'
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
复制代码
作者:
jason680
时间:
2013-05-30 16:18
回复
6#
yestreenstars
$ awk 'BEGIN{f[0]=12;f[2]=20;L=0}!NF{c=0;L++;next}{s=f[L]?f[L]:5;a[c]=a[c]""sprintf("%-"s"s",$0);c++}END{for(n=0;n++<c;)print a[n]}' datafile
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
复制代码
作者:
kk5234
时间:
2013-05-30 16:24
@seesea2517
@jason680
学习了!
作者:
代号:军刀
时间:
2013-05-30 16:30
本帖最后由 代号:军刀 于 2013-05-30 16:38 编辑
awk '{a[NR]=$0}END{for(j=1;j<=9;j++){for(i=j;i<=NR-10;i+=10){s=s?s"\t"a[i]:a[i]}print s;s=""}}'
复制代码
对齐很蛋疼,不知我这个要对齐的话应该怎么修改
作者:
心若寒江雪
时间:
2013-05-30 19:18
awk 'BEGIN{RS="";FS="\n"}{for(i=1;i<=NF;i++){a[i]!=""?a[i]=a[i]"\t"$i:a[i]=$i}}END{for(i in a)printf("%s\n",a[i])}' file
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2