a=`cat file`;echo $a
awk '{printf("%s ",$0)}' file // %s 后记得要有一空格,否则出来就是完全连在一起的,中间连空格都没有
cat file |xargs
awk '{printf("%s ",$0)}' file // %s 后记得要有一空格,否则出来就是完全连在一起的,中间连空格都没有
cat file |xargs
2
应用举例:一个文件每行都有一个数字,现在需要把每行的数字用“+”连接起来。
cat a
96
1093
1855
1253
1364
1332
2308
2589
2531
1239
2164
2826
2787
2145
2617
4311
1810
2115
1235
awk '{printf ("%s+",$0)}' a; echo ""
96+1093+1855+1253+1364+1332+2308+2589+2531+1239+2164+2826+2787+2145+2617+4311+1810+2115+1235+
这里注意,最后一个是带“+”的。echo "" 的作用是换行。
另外的方法是
cat a|xargs|sed 's/ /+/g'
96+1093+1855+1253+1364+1332+2308+2589+2531+1239+2164+2826+2787+2145+2617+4311+1810+2115+1235
cat a
96
1093
1855
1253
1364
1332
2308
2589
2531
1239
2164
2826
2787
2145
2617
4311
1810
2115
1235
awk '{printf ("%s+",$0)}' a; echo ""
96+1093+1855+1253+1364+1332+2308+2589+2531+1239+2164+2826+2787+2145+2617+4311+1810+2115+1235+
这里注意,最后一个是带“+”的。echo "" 的作用是换行。
另外的方法是
cat a|xargs|sed 's/ /+/g'
96+1093+1855+1253+1364+1332+2308+2589+2531+1239+2164+2826+2787+2145+2617+4311+1810+2115+1235
0
linux下的计算器 gdb
GNU gdb 6.8-debian
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
(gdb) p 478+3677+1866+2141+2199+1776+3359+3383+5326+2049+3560+2608+3682+5549+6122+6150+9966+3756+1943
$1 = 69590
// 注意,这里的p后面不是回车,是空格
GNU gdb 6.8-debian
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
(gdb) p 478+3677+1866+2141+2199+1776+3359+3383+5326+2049+3560+2608+3682+5549+6122+6150+9966+3756+1943
$1 = 69590
// 注意,这里的p后面不是回车,是空格
0
现在了解了python,其实直接可以用python去计算,更加简单
只要输入 python,然后输入
478+3677+1866+2141+2199+1776+3359+3383+5326+2049+3560+2608+3682+5549+6122+6150+9966+3756+1943
即可
[root@www python]# python
Python 2.4.3 (#1, Jul 27 2009, 17:57:39)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 478+3677+1866+2141+2199+1776+3359+3383+5326+2049+3560+2608+3682+5549+6122+6150+9966+3756+1943
69590
>>>
只要输入 python,然后输入
478+3677+1866+2141+2199+1776+3359+3383+5326+2049+3560+2608+3682+5549+6122+6150+9966+3756+1943
即可
[root@www python]# python
Python 2.4.3 (#1, Jul 27 2009, 17:57:39)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 478+3677+1866+2141+2199+1776+3359+3383+5326+2049+3560+2608+3682+5549+6122+6150+9966+3756+1943
69590
>>>
0
cat a|xargs
96 1093 1855 1253 1364 1332 2308 2589 2531 1239 2164 2826 2787 2145 2617 4311 1810 2115 1235
# cat a|xargs|sed "s/ /+/g"
96+1093+1855+1253+1364+1332+2308+2589+2531+1239+2164+2826+2787+2145+2617+4311+1810+2115+1235
96 1093 1855 1253 1364 1332 2308 2589 2531 1239 2164 2826 2787 2145 2617 4311 1810 2115 1235
# cat a|xargs|sed "s/ /+/g"
96+1093+1855+1253+1364+1332+2308+2589+2531+1239+2164+2826+2787+2145+2617+4311+1810+2115+1235
0
黄圣倨 - 与大家共勉,一起进步。
使用paste命令也可以实现的,加个选项-s就行了,而且paste命令自带空格属性将一行的内容分开,要是指定分割符的话,加-d即可
示例
[root@hsj-01 ~]# cat b.txt
aaaaa
bbbbb
ccccc
[root@hsj-01 ~]# paste -s b.txt
aaaaa bbbbb ccccc
[root@hsj-01 ~]# paste -s -d '#' b.txt
aaaaa#bbbbb#ccccc
[root@hsj-01 ~]# paste -s b.txt >c.txt
[root@hsj-01 ~]# cat c.txt
aaaaa bbbbb ccccc
编辑回复