Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- contents of file1:
- ETIAM......
- SED........
- MAECENAS...
- DONEC......
- SUSPENDISSE
- contents of file2
- Lorem....
- Proin....
- Nunc.....
- Quisque..
- Aenean...
- Nam......
- Vivamus..
- Curabitur
- Nullam...
- paste file1 file2
- ETIAM...... Lorem....
- SED........ Proin....
- MAECENAS... Nunc.....
- DONEC...... Quisque..
- SUSPENDISSE Aenean...
- Nam......
- Vivamus..
- Curabitur
- Nullam...
- ETIAM...... Lorem....
- SED........ Proin....
- MAECENAS... Nunc.....
- DONEC...... Quisque..
- SUSPENDISSE Aenean...
- Nam......
- Vivamus..
- Curabitur
- Nullam...
- paste file1 file2 | column -t
- ETIAM...... Lorem....
- SED........ Proin....
- MAECENAS... Nunc.....
- DONEC...... Quisque..
- SUSPENDISSE Aenean...
- Nam......
- Vivamus..
- Curabitur
- Nullam...
- paste file1 file2 | expand -t 13
- paste file1 file2 | expand -t $((`wc -L file1|awk '{print $1}'` + 2))
- awk '{if(length($0)>max)max=length($0)}
- FNR==NR{s1[FNR]=$0;next}{s2[FNR]=$0}
- END { format = "%-" max "st%-" max "sn";
- numlines=(NR-FNR)>FNR?NR-FNR:FNR;
- for (i=1; i<=numlines; i++) { printf format, s1[i]?s1[i]:"", s2[i]?s2[i]:"" }
- }' file1 file2
- # 2013-11-05 mike@diehn.net
- # Invoke thus:
- # awk -f this_file file1 file2
- # The result is what you asked for and the columns will be
- # determined by input file order.
- #----------------------------------------------------------
- # No matter which file we're reading,
- # keep track of max line length for use
- # in the printf format.
- #
- { if ( length($0) > max ) max=length($0) }
- # FNR is record number in current file
- # NR is record number over all
- # while they are equal, we're reading the first file
- # and we load the strings into array "s1"
- # and then go to the "next" line in the file we're reading.
- FNR==NR { s1[FNR]=$0; next }
- # and when they aren't, we're reading the
- # second file and we put the strings into
- # array s2
- {s2[FNR]=$0}
- # At the end, after all lines from both files have
- # been read,
- END {
- # use the max line length to create a printf format
- # the right widths
- format = "%-" max "st%-" max "sn"
- # and figure the number of array elements we need
- # to cycle through in a for loop.
- numlines=(NR-FNR)>FNR?NR-FNR:FNR;
- for (i=1; i<=numlines; i++) {
- printf format, s1[i]?s1[i]:"", s2[i]?s2[i]:""
- }
- }
- paste file1 file2 | sed 's/^TAB/&&/'
- # Invoke thus:
- # awk -F\t -f this_file file1 file2
- # every time we read a new file, FNR goes to 1
- FNR==1 {
- curfile++ # current file
- }
- # read all files and save all the info we'll need
- {
- column[curfile,FNR]=$0 # save current line
- nlines[curfile]++ # number of lines in current file
- if (length > len[curfile])
- len[curfile] = length # max line length in current file
- }
- # finally, show the lines from all files side by side, as a table
- END {
- # iterate through lines until there are no more lines in any file
- for (line = 1; !end; line++) {
- $0 = _
- end = 1
- # iterate through all files, we cannot use
- # for (file in nlines) because arrays are unordered
- for (file=1; file <= curfile; file++) {
- # columnate corresponding line from each file
- $0 = $0 sprintf("%*s" FS, len[file], column[file,line])
- # at least some file had a corresponding line
- if (nlines[file] >= line)
- end = 0
- }
- # don't print a trailing empty line
- if (!end)
- print
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement