add column to file under header bash
greetings experts,
i have a file1 which contains a header and data under each of the first 2 columns. App, Date, Error BobApp,Jan01, JoeApp,Feb02, a have another file2 which contains 1234 32124 what i would like to do is add file2 contents under file1 contents "Error" heading to be able to open in csv in nice column format. Suggestions please? Thanks again |
Help us to help you. You gave sample input files (that's good) and some words (also good). Construct a sample output file which corresponds to your sample inputs and post it here. With "Before and After" examples we can better understand your needs and also judge if our proposed solution fills those needs.
Daniel B. Martin |
hi,
i would like my current file1 App, Date, Error BobApp,Jan01, JoeApp,Feb02, to take the contents of file2 and append to file1 so that it looks like this App, Date, Error BobApp,Jan01,1234 JoeApp,Feb02,32124 |
what have you tried so far? Do you have probably a script already to work with.
|
man paste
|
Quote:
I did this using paste and sed. There are certainly other workable methods. Make your best effort and post the results here. If successful, we all celebrate. If still stuck, more help will be forthcoming. Daniel B. Martin |
Due to the uneven-ness of the files, as pointed out above, I would use awk. As tips for this you might want to research the NR variable and also the outcome of comparing NR and FNR (to read multiple files - plenty of which has been done on this site ;) )
Here is a link to the manual :- http://www.gnu.org/software/gawk/man...ode/index.html |
Another approach to consider:
1) Use tac to turn Infile1 "upside down" and save it as a work file. 2) Use tac to turn Infile2 "upside down" and save it as a work file. 3) Use paste to weld the two work files together, side by side. In this instance the unequal lengths of the files won't be troublesome. 4) Use tac to turn the welded file "rightside up." Advantage: simple in concept, better for a newbie. Disadvantage: uses temporary files. Daniel B. Martin |
Quote:
|
Quote:
Daniel B. Martin {Happy Turkey Day to all who observe!} |
OP never came back to report success or failure. I coded eight solutions -- the best four are offered here.
Note that I took the liberty of removing extraneous blanks from InFile1 ... Code:
App,Date,Error Code:
1234 Code:
The solutions are ... Code:
Method #1 of LQ member danielbmartin. If any LQ contributor coded an awk solution, please post it. Daniel B. Martin |
Using Daniel's file names:
Code:
awk 'FNR==NR{a[NR]=$0;next}$0 = $0 a[FNR-1]' InFile2 InFile1 Code:
tac <(paste -d "\0" <(tac InFile1) <(tac InFile2)) |
Quote:
Daniel B. Martin |
All times are GMT -5. The time now is 08:09 AM. |