Bash: append columns to table
Solution 1
You want the paste
utility:
paste history today > new_history
By default, paste
separates the lines with a tab
. You can change that with the -d
option.
paste
only works as expected if the files to be combined have the same number of lines, in the same order, and without keys which might need to be deleted from one of the files. For a slightly more sophisticated utility, see join
Solution 2
The easiest way I can think to do this would be to loop through the prior days output and print that line + the new content that you want at the end of each of those lines.
$ while read i; do printf "%s %s\n" "$i" "..new stuff.."; done < prior.txt
Example
Say I put your lines into a file and call it prior.txt
.
file1 3443 words 11-20-13 44 4788 words 11-23-13
file2 4457 words ...
If I run the above command I'd get the following output:
$ while read i; do printf "%s %s\n" "$i" "..new stuff.."; done < prior.txt
file1 3443 words 11-20-13 44 4788 words 11-23-13 ..new stuff..
file2 4457 words ... ..new stuff..
Details
The above uses a while
loop to go through each line of prior.txt
. I then make use of printf
to print that line, which is contained in the variable $i
, followed by the string ..new stuff..
.
You can swap your own content into ..new stuff..
or augment the above as needed to suit your needs.
Related videos on Youtube
Quora Feans
Updated on September 18, 2022Comments
-
Quora Feans over 1 year
If you want to append a column (not a line) to a table, and the column has the same number of elements for sure, how can you do it?
Given a set of files, which keeps changing each day, you want to keep track of it.
An example would be:
file1 3443 words 11-20-13 44 4788 words 11-23-13 file2 4457 words ...
-
Quora Feans over 10 years@JosephR.: sometimes from a file, sometimes it's the output of wc or other similar program.
-
-
Quora Feans over 10 yearsBut how would this work if "...new stuff..." is a list (from a file or command like wc or ls -l). It seem the solution with the paste command would be simpler to implement.
-
slm over 10 years@QuoraFea - it really depends on what you're doing. Paste is easier if the contents are in 2 files already, if you're generating the information via a loop then this would method would be more appropriate. You didn't specify how you were going to generate the data to append to the columns. Both answers are correct, but only one is appropriate for your Q.
-
Quora Feans over 10 yearsI mean, how do you run a command in the place of "...new stuff..." If I put "ls -l" (for example) instead of it, it will just literally append the command "ls -l", not the output of it.
-
slm over 10 years@QuoraFea - Oh, easy. You can put this in place of
"..new stuff.."
. Just put whatever you want in the"$(...)"
. But this will put all the output of from this command so I don't think that's what you're after. As I said, I'd need to see more of your actual script to give you a better answer than this.