一个文件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. spring mvc 拦截器 拦截子目录

    项目中碰到这一个问题: 对于/user/loginpage,/user/login这一类的url,放行: 对于/user/{userId}/xxx(xxx不为空)的操作,需要拦截,url-patter ...

  2. Hadoop.2.x_简单的测试文件读取与上传

    代码如下, 后备参考: package com.bigdata.hadoop.hdfs; import java.io.File; import java.io.FileInputStream; im ...

  3. QQ中打开链接不是默认浏览器

    电脑上装了搜狗和Chrome,Chrome为默认浏览器.但QQ中不论点什么都是以搜狗打开,解决办法: 1.设置, 2. 安全设置-->安全推荐-->使用搜狗打开链接增强安全性.去掉勾勾就行 ...

  4. 解决HDU爆栈的问题

    转载:http://forum.byr.edu.cn/article/ACM_ICPC/51264 大家懂的,鉴于HDU用的是windows服务器,所以stack大小及其坑爹,稍微深一点的递归栈就会s ...

  5. android基础知识之一

    1:Android系统架构(重点) 分层的架构 JNI java native interface 1.application :应用层 : java 2.application framework ...

  6. spring security使用数据库资源

    国内对权限系统的基本要求是将用户权限和被保护资源都放在数据库里进行管理,在这点上Spring Security并没有给出官方的解决方案,为此我们需要对Spring Security进行扩展.. 数据库 ...

  7. alpha发布之小组评论

    在alpha发布之后,让我看到了,大家都很努力,在alpha发布前大家都尽量完成自己的项目,虽然大家都很忙,但是,都在抽出时间趟黑起早的完成项目,在你们身上有很多很值得我学习的地方,虽然我认为半夜睡觉 ...

  8. The index also can be used for LIKE comparisons if the argument to LIKE is a constant string that does not start with a wildcard character.

    http://dev.mysql.com/doc/refman/5.7/en/index-btree-hash.html MySQL 5.7 Reference Manual  /  ...  /   ...

  9. 【java基础学习】GUI

    GUI 容器 布局管理器 组件 菜单 事件处理机制

  10. Mongo聚合函数

    { "_id" : ObjectId("57301c7e5fd5d6e2afa221d1"), "a" : "张三", ...