Chinaunix

标题: 两个文件按行拼接,不要连接符 [打印本页]

作者: linlone    时间: 2016-08-17 11:18
标题: 两个文件按行拼接,不要连接符
第一个文件:

  1. abc
  2. defg
  3. hijklmn
  4. piu
复制代码
第二个文件
123
4
567
890
期望按行拼接成:

  1. abc123
  2. defg4
  3. hijklmn567
  4. piu890
复制代码
貌似paste必须指定拼接字符?

作者: liion631818    时间: 2016-08-17 11:20
回复 1# linlone


    shell$ paste -d "" A B
abc123
defg4
hijklmn567
piu890
作者: linlone    时间: 2016-08-17 11:24
回复 2# liion631818


    你试过吗?我这里不行
作者: linlone    时间: 2016-08-17 11:46
回复 3# linlone


    “-d”后面要有一个空格,否则不行
  1. $ cat A.txt
  2. abc
  3. defg
  4. hijklmn
  5. piu

  6. $ cat B.txt
  7. 123
  8. 4
  9. 567
  10. 890

  11. $ paste -d "" A.txt B.txt
  12. abc123
  13. defg4
  14. hijklmn567
  15. piu890
复制代码

作者: moperyblue    时间: 2016-08-17 11:48
本帖最后由 moperyblue 于 2016-08-17 13:57 编辑
  1. paste -d '' file1 file2
  2. awk '{getline x<"file2";print $0x}'  file1
  3. awk '{s=$0;getline<"file2";print s$0}' file1
  4. sed '1{x;s/.*/cat file2/e;x};G;s/\n//;P;s/[^\n]*\n//;h;d' file1
  5. sed -r '1{x;s/.*/cat file2/e;x};G;h;s/([^\n]*)\n([^\n]*)(\n(.*))?/\1\2/p;g;s//\4/;h;d' file1
复制代码

作者: butterflyswim    时间: 2016-08-17 13:44
  1. awk '{printf $0;getline < "file02" ;print $0}' file01
复制代码

作者: Zivix0406    时间: 2016-08-17 13:46
2楼给的命令妥妥的

作者: 1cpuer    时间: 2016-08-17 14:58
回复 5# moperyblue

s//\4/
??????


   
作者: moperyblue    时间: 2016-08-17 15:00
回复 8# 1cpuer


    sed -r '1{x;s/.*/cat file2/e;x};G;h;s/([^\n]*)\n([^\n]*)(\n(.*))?/\1\2/p;g;s//\4/;h;d' file1
作者: 1cpuer    时间: 2016-08-17 15:11
回复 9# moperyblue

s//\4/;是何解释
看了好许sed url 没见识过



   
作者: moperyblue    时间: 2016-08-17 15:15
回复 10# 1cpuer


    <=> s/([^\n]*)\n([^\n]*)(\n(.*))?/\4/
作者: 杰瑞26    时间: 2016-08-17 15:52
  1. root@localhost:~# paste file1 file2 | tr -d '[ \t]'
  2. abc123
  3. defg4
  4. hijklmn567
  5. piu890
复制代码

作者: 1cpuer    时间: 2016-08-17 15:52
本帖最后由 1cpuer 于 2016-08-17 15:58 编辑

回复 11# moperyblue

\4 YI 解是第4参数
莫非是第 3()后 ?
s//是啥

还有如何单个回复的
s是替换的意思
//
1 /匹配开始
2 /匹配结束
//匹配NULL=0
/ /匹配空格
我是这样理解的
   
作者: moperyblue    时间: 2016-08-17 16:16
回复 13# 1cpuer


    `/REGEXP/'
     This will select any line which matches the regular expression
     REGEXP.  If REGEXP itself includes any `/' characters, each must
     be escaped by a backslash (`\').

     The empty regular expression `//' repeats the last regular
     expression match (the same holds if the empty regular expression is
     passed to the `s' command).  Note that modifiers to regular
     expressions are evaluated when the regular expression is compiled,
     thus it is invalid to specify them together with the empty regular
     expression.


作者: sunzhiguolu    时间: 2016-08-21 02:01
  1. perl -ne 'BEGIN{open(b,'b')}{chomp;print $_,scalar <b>}' a
复制代码
abc123
defg4
hijklmn567
piu890

作者: 我不哭想    时间: 2016-08-30 21:29
看看了。。。。。。。。
作者: 杰瑞26    时间: 2016-08-31 08:39
  1. root@localhost:~# paste -d '' file1 file2
  2. abc123
  3. defg4
  4. hijklmn567
  5. piu890
复制代码

作者: 睡罗    时间: 2016-08-31 21:24
分享了。。。。。。。。。。
作者: 睡罗    时间: 2016-08-31 21:24
分享了。。。。。。。。。。
作者: junlee1986    时间: 2016-12-15 11:04
又学到一个命令了。。。
作者: qinwei1314ai    时间: 2016-12-22 14:16
使用awk数组实现,当模式(条件)是NR==FNR的时候,处理的是第一个(a.txt)文件;当NR!=的时候处理的是第二个文件(b.txt):
  1. awk 'NR==FNR{a[FNR]=$1}NR!=FNR{print a[FNR]$1}' a.txt b.txt
复制代码

C:\Users\Administrator\Desktop\1.png
作者: qinwei1314ai    时间: 2016-12-22 14:17
使用awk数组实现,当模式(条件)是NR==FNR的时候,处理的是第一个(a.txt)文件;当NR!=的时候处理的是第二个文件(b.txt):
  1. awk 'NR==FNR{a[FNR]=$1}NR!=FNR{print a[FNR]$1}' a.txt b.txt
复制代码

C:\Users\Administrator\Desktop\1.png




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2