ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
schneidz correctly suggested that paste could be the heart of a good solution. Your two files are of unequal length because InFile1 has a header and InFile2 does not. There are different ways to cope with this "wrinkle." One method is to insert a blank line at the top of InFile2 which makes the two input files of equal length. Then use paste to weld the two files together, side by side, to form the desired OutFile.
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
Last edited by danielbmartin; 11-25-2015 at 01:54 PM.
Reason: Cosmetic improvement
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 )
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.
You are quite right, and I coded it two ways. First using work files, and second using process substitution. A newbie might grasp the first method more easily.
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
BobApp,Jan01,
JoeApp,Feb02,
InFile2 remains unchanged ...
Code:
1234
32124
Code:
The solutions are ...
echo
echo "Method #1 of LQ member danielbmartin."
echo " SED to prefix InFile2 with a blank line."
echo " PASTE to weld the two files together."
echo " COLUMN to produce an attractive table."
paste -d "\0" $InFile1 <(sed '1s/^/ \n/' $InFile2) |column -ts","
echo
echo "Method #2 of LQ member danielbmartin."
echo ' TAC to turn both input files "upside down."'
echo " PASTE to weld the two together."
echo ' TAC to turn the welded file "rightside up."'
echo " COLUMN to produce an attractive table."
tac $InFile1 \
|paste -d "\0" - <(tac $InFile2) \
|tac \
|column -ts","
echo
echo "Method #3 of LQ member danielbmartin."
echo " SED to peel off the first line of InFile1."
echo " PASTE to weld the rest of it to InFile2."
echo " SED to prefix the welded file with the first line of InFile1."
echo " COLUMN to produce an attractive table."
sed '1d' $InFile1 \
|paste -d "\0" - $InFile2 \
|sed "1s/^/"$(sed '1q' $InFile1)"\n/" \
|column -ts","
echo
echo "Method #4 of LQ member danielbmartin."
echo " SED to move the first line of InFile1 to bottom of file."
echo " PASTE to weld files together."
echo " SED to move the last line of combined file to top of file."
echo " COLUMN to produce an attractive table."
sed -n '1{h};1!{$!{p};${p;x;p}}' $InFile1 \
|paste -d "\0" - $InFile2 \
|sed '1h;1d;$!H;$!d;G' \
|column -ts","
Execution produces this result ...
Code:
Method #1 of LQ member danielbmartin.
SED to prefix InFile2 with a blank line.
PASTE to weld the two files together.
COLUMN to produce an attractive table.
App Date Error
BobApp Jan01 1234
JoeApp Feb02 32124
Method #2 of LQ member danielbmartin.
TAC to turn both input files "upside down."
PASTE to weld the two together.
TAC to turn the welded file "rightside up."
COLUMN to produce an attractive table.
App Date Error
BobApp Jan01 1234
JoeApp Feb02 32124
Method #3 of LQ member danielbmartin.
SED to peel off the first line of InFile1.
PASTE to weld the rest of it to InFile2.
SED to prefix the welded file with the first line of InFile1.
COLUMN to produce an attractive table.
App Date Error
BobApp Jan01 1234
JoeApp Feb02 32124
Method #4 of LQ member danielbmartin.
SED to move the first line of InFile1 to bottom of file.
PASTE to weld files together.
SED to move the last line of combined file to top of file.
COLUMN to produce an attractive table.
App Date Error
BobApp Jan01 1234
JoeApp Feb02 32124
Comments and corrections are welcomed.
If any LQ contributor coded an awk solution, please post it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.