一个文件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. Odoo 仓库扫码打包方案

        Odoo仓库扫码的设计,前提是操作人在PC上先做好分拣单,然后根据打印出来的分拣单去仓库进行扫码打包,默认的情况下,分拣在被确认的时候会自动保留库位中已经存在的库存(已经分配批次\序列号),而 ...

  2. [转] - bashrc与profile的区别

    bashrc与profile的区别 要搞清bashrc与profile的区别,首先要弄明白什么是交互式shell和非交互式shell,什么是login shell 和non-login shell. ...

  3. Git基本命令行操作

    A. 新建Git仓库,创建新文件夹git init  B. 添加文件到git索引git add <filename>  --- 单个文件添加git add * --- 全部文件添加 C. ...

  4. 打开FileGeoDatabase中要素类

    private IFeatureClass OpenFileGdbFtCls(string fn) { IFeatureClass pftcls = null; IWorkspaceFactory w ...

  5. Greenplum 集群部署

    最近开始接触Greenplum,线上也在使用了,感觉还不错,本次介绍一下集群的部署方法.那么Greenplum的架构如下: (架构图来源网络) 简单来说GPDB是一个分布式数据库软件,其可以管理和处理 ...

  6. 屏蔽Enter触发的事件

    无论是 <button type="button" onclick="console.log('123');">123</button> ...

  7. Server.UrlEncode与Server.UrlDecode(url传递中文的解决方案)

    1.设置web.config文件.<system.web> ...... <globalization requestEncoding="gb2312" resp ...

  8. 杭电ACM 1013 Digital Root

    #include<stdio.h>#include<stdlib.h>#include<string.h>int main(){char s[100000];int ...

  9. Graph | Eulerian path

    In graph theory, a Eulerian trail (or Eulerian path) is a trail in a graph which visits every edge e ...

  10. faster with MyISAM tables than with InnoDB or NDB tables

    http://dev.mysql.com/doc/refman/5.7/en/partitioning-limitations.html Performance considerations.  So ...