一个文件1.txt,内容如下
a
b
c
d
e
目的把列变行,输出为: a b c d e

脚本如下:
awk 'BEGIN{RS="";FS="\n";OFS=" "}{print }' test
a
b
c
d
e
理论上应该实现我们想要的 a b c d e

问题出在这里

Understanding $0

It is important to remember that $0 is the full record, exactly as
it was read from the input. This includes any leading or
trailing whitespace, and the exact whitespace (or other
characters) that separate the fields.

It is a not-uncommon error to try to change the field
separators in a record simply by setting FS and OFS,
and then expecting a plain ‘print’
or ‘print $0’ to
print the modified record.

But this does not work, since nothing was done to change
the record itself. Instead, you must force the record to
be rebuilt, typically with a statement such as ‘$1 = $1’, as described
earlier.

给懒得看英文的人翻译一下:
$0精确的表示你从awk输入中读到的一条记录。它包含了开头和结尾的空格及分割字段间的空格(或其它字符)
这里有一个很不寻常的错误会发生,当你通过FS或OFS改变了记录的分隔符时,而你正好想用print或print $0打印出这条记录,但是
它不会如你所愿的(正如我们上面的例子),因为这条记录没有任何改变,所以你要强制改变这条记录,方法就是
$1=$1或$2=$2或随便你想改变什么变量。

It works !
quke@Raphaelqu:/tmp$ awk 'BEGIN{RS="";FS="\n";OFS=" "}{$1=$1;print}'
1.txt
a b c d e f g

详情参考:

http://www.gnu.org/software/gawk/manual/html_node/Changing-Fields.html

awk改变了OFS,$0却没变化的更多相关文章

  1. 有史来最大改变 Android 5.0十大新特性

    有史来最大改变 Android 5.0十大新特性 2014.10.16 14:51:31 来源:腾讯数码作者:腾讯数码 ( 0 条评论 )   距离Android系统上一次重大更新不到一年的时间,谷歌 ...

  2. [转]Material Design Library 23.1.0的新变化与代码实战

    Design Library出来已经快有一个月了,当时大概看了一下介绍这个新版本变化的译文,内容不多,给我印象最深的就是Percent lib.AppBarLayout 和NavigationView ...

  3. Material Design Library 23.1.0的新变化与代码实战

    Design Library出来已经快有一个月了,当时大概看了一下介绍这个新版本变化的译文,内容不多,给我印象最深的就是Percent lib.AppBarLayout 和NavigationView ...

  4. 有时候当你改动了xib   你调试,各种改的时候又没变化的时候

    当你xib 改变了   调试的时候又没变化的时候,当你各种改错无用的时候,就是xcode xib的缓存问题了.  please clear xcode . command + shift + K . ...

  5. fio 测试 磁盘I/O: ls -1 /sys/block/sda/queue/ | awk '{cmd="cat "i$0; print i$0; system(cmd) }' i=`pwd`'/'

    小型计算机系统接口(SCSI,Small Computer System Interface) SAS(Serial Attached SCSI,串列SCSI) SCSI 硬盘名称: sd[a-p] ...

  6. linux文件删除,剩余空间没变化

    centOS6.6,删除了一个13G的文件,但是使用df -h 查看,剩余空间没变化这个文件夹查看 du -h查看发现空间已经变少了.原因可能是删除文件时有进程在使用文件,导致空间未释放.yum in ...

  7. Vue1.x 到Vue2.0的一个变化

    小弟初来乍到,写的不好的地方还望指正.谢谢各位! 废话不多说  进入正题: Vue1.x到2.0的一个变化 1. 在每个组件模板,不在支持片段代码  组件中模板: 之前:     <templa ...

  8. vue2.0有哪些变化

    vue2.0之后有哪些变化: 1.每个组件模板template,不再支持片段代码 之前: <template> <h3>vue-router+vue-loader</h3 ...

  9. AWK改变输入输出分隔符实例分析

    awk默认从STDIN接受数据,打印文本到STDOUT. awk的默认输入和输出分隔符: FS : 输入字段分隔符,默认空格. RS : 输入行分隔符,默认\n. OFS : 输出字段分隔符,默认空格 ...

随机推荐

  1. android中versionCode&versionName

    原文来自:http://blog.csdn.net/wh_19910525/article/details/8660416 ,略有修改 一.概述 Android的版本可以在androidmainfes ...

  2. [LintCode] Backpack VI 背包之六

    Given an integer array nums with all positive numbers and no duplicates, find the number of possible ...

  3. android-ContentProvider

    一.理解ContentProvider 为了在应用程序之间交换数据,Android提供了ContentProvider,它是不同应用程序之间进行数据交换的标准API,当一个应用程序需要把自己的数据暴露 ...

  4. Defining custom settings in Odoo

    Unfortunately Odoo documentation doesn’t seem to include any information about adding new configurat ...

  5. 自己编写php框架(一)

    这两天一直在琢磨如何做一个php模板,将前台html页面和php代码分割开来.虽然现在市面上有各种各样的php模板,但还是想自己编写一个体验一下. 之前一直写到了将替换完php代码的含有php代码的h ...

  6. PHP后门新玩法:一款猥琐的PHP后门分析

    0x00 背景 近日,360网站卫士安全团队近期捕获一个基于PHP实现的webshell样本,其巧妙的代码动态生成方式,猥琐的自身页面伪装手法,让我们在分析这个样本的过程中感受到相当多的乐趣.接下来就 ...

  7. classmethod一个用处是创建可选类构造器

    Definition and Introduction通常来说, descriptor 是一种绑定着特殊行为属性的对象, 在访问它时行为被descriptor协议定义的方法所重载.这些方法是__get ...

  8. graph | hungary

    匈牙利算法,求二分图最大匹配. 若P是图G中一条连通两个未匹配顶点的路径,并且属于M的边和不属于M的边(即已匹配和待匹配的边)在P上交替出现,则称P为相对于M的一条增广路径.(M为一个匹配) 由增广路 ...

  9. support HTML5 in IE8

    <!--this code is used to support HTML5 in IE8--> <!--[if IE]><script type="text/ ...

  10. 【五子棋AI循序渐进】——多线程搜索

    关于多线程搜索,有很多方法来实现,很多文章推荐基于MTD(F)的方式.好处不言而喻,不过我的程序中采用的是基于PVS的多线程搜索.实现起来主要是这几个方面问题需要解决: 1.置换表的互斥访问. 2.局 ...