Advertisement
Guest User

Untitled

a guest
Jul 10th, 2014
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. contents of file1:
  2.  
  3. ETIAM......
  4. SED........
  5. MAECENAS...
  6. DONEC......
  7. SUSPENDISSE
  8.  
  9. contents of file2
  10.  
  11. Lorem....
  12. Proin....
  13. Nunc.....
  14. Quisque..
  15. Aenean...
  16. Nam......
  17. Vivamus..
  18. Curabitur
  19. Nullam...
  20.  
  21. paste file1 file2
  22.  
  23. ETIAM...... Lorem....
  24. SED........ Proin....
  25. MAECENAS... Nunc.....
  26. DONEC...... Quisque..
  27. SUSPENDISSE Aenean...
  28. Nam......
  29. Vivamus..
  30. Curabitur
  31. Nullam...
  32.  
  33. ETIAM...... Lorem....
  34. SED........ Proin....
  35. MAECENAS... Nunc.....
  36. DONEC...... Quisque..
  37. SUSPENDISSE Aenean...
  38. Nam......
  39. Vivamus..
  40. Curabitur
  41. Nullam...
  42.  
  43. paste file1 file2 | column -t
  44.  
  45. ETIAM...... Lorem....
  46. SED........ Proin....
  47. MAECENAS... Nunc.....
  48. DONEC...... Quisque..
  49. SUSPENDISSE Aenean...
  50. Nam......
  51. Vivamus..
  52. Curabitur
  53. Nullam...
  54.  
  55. paste file1 file2 | expand -t 13
  56.  
  57. paste file1 file2 | expand -t $((`wc -L file1|awk '{print $1}'` + 2))
  58.  
  59. awk '{if(length($0)>max)max=length($0)}
  60. FNR==NR{s1[FNR]=$0;next}{s2[FNR]=$0}
  61. END { format = "%-" max "st%-" max "sn";
  62. numlines=(NR-FNR)>FNR?NR-FNR:FNR;
  63. for (i=1; i<=numlines; i++) { printf format, s1[i]?s1[i]:"", s2[i]?s2[i]:"" }
  64. }' file1 file2
  65.  
  66. # 2013-11-05 mike@diehn.net
  67. # Invoke thus:
  68. # awk -f this_file file1 file2
  69. # The result is what you asked for and the columns will be
  70. # determined by input file order.
  71. #----------------------------------------------------------
  72. # No matter which file we're reading,
  73. # keep track of max line length for use
  74. # in the printf format.
  75. #
  76. { if ( length($0) > max ) max=length($0) }
  77.  
  78. # FNR is record number in current file
  79. # NR is record number over all
  80. # while they are equal, we're reading the first file
  81. # and we load the strings into array "s1"
  82. # and then go to the "next" line in the file we're reading.
  83. FNR==NR { s1[FNR]=$0; next }
  84.  
  85. # and when they aren't, we're reading the
  86. # second file and we put the strings into
  87. # array s2
  88. {s2[FNR]=$0}
  89.  
  90. # At the end, after all lines from both files have
  91. # been read,
  92. END {
  93. # use the max line length to create a printf format
  94. # the right widths
  95. format = "%-" max "st%-" max "sn"
  96. # and figure the number of array elements we need
  97. # to cycle through in a for loop.
  98. numlines=(NR-FNR)>FNR?NR-FNR:FNR;
  99. for (i=1; i<=numlines; i++) {
  100. printf format, s1[i]?s1[i]:"", s2[i]?s2[i]:""
  101. }
  102. }
  103.  
  104. paste file1 file2 | sed 's/^TAB/&&/'
  105.  
  106. # Invoke thus:
  107. # awk -F\t -f this_file file1 file2
  108.  
  109. # every time we read a new file, FNR goes to 1
  110.  
  111. FNR==1 {
  112. curfile++ # current file
  113. }
  114.  
  115. # read all files and save all the info we'll need
  116. {
  117. column[curfile,FNR]=$0 # save current line
  118. nlines[curfile]++ # number of lines in current file
  119. if (length > len[curfile])
  120. len[curfile] = length # max line length in current file
  121. }
  122.  
  123. # finally, show the lines from all files side by side, as a table
  124. END {
  125. # iterate through lines until there are no more lines in any file
  126. for (line = 1; !end; line++) {
  127. $0 = _
  128. end = 1
  129.  
  130. # iterate through all files, we cannot use
  131. # for (file in nlines) because arrays are unordered
  132. for (file=1; file <= curfile; file++) {
  133. # columnate corresponding line from each file
  134. $0 = $0 sprintf("%*s" FS, len[file], column[file,line])
  135. # at least some file had a corresponding line
  136. if (nlines[file] >= line)
  137. end = 0
  138. }
  139.  
  140. # don't print a trailing empty line
  141. if (!end)
  142. print
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement