2

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 ...
  • Where are you obtaining this "new column" from? – Joseph R. Nov 23 '13 at 3:54
  • @JosephR.: sometimes from a file, sometimes it's the output of wc or other similar program. – Quora Feans Nov 23 '13 at 13:32
6

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

1

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.

  • But 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. – Quora Feans Nov 23 '13 at 18:04
  • @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. – slm Nov 23 '13 at 18:13
  • I 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. – Quora Feans Nov 23 '13 at 20:37
  • @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. – slm Nov 23 '13 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.