LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-25-2015, 08:48 AM   #1
bishop2001
Member
 
Registered: Apr 2015
Posts: 58

Rep: Reputation: Disabled
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
 
Old 11-25-2015, 09:03 AM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,879

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
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
 
Old 11-25-2015, 09:15 AM   #3
bishop2001
Member
 
Registered: Apr 2015
Posts: 58

Original Poster
Rep: Reputation: Disabled
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
 
Old 11-25-2015, 09:41 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,692

Rep: Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274
what have you tried so far? Do you have probably a script already to work with.
 
Old 11-25-2015, 09:45 AM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
man paste
 
1 members found this post helpful.
Old 11-25-2015, 11:49 AM   #6
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,879

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by schneidz View Post
man paste
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
 
Old 11-25-2015, 12:11 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
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
 
1 members found this post helpful.
Old 11-25-2015, 12:39 PM   #8
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,879

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
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
 
1 members found this post helpful.
Old 11-26-2015, 09:46 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
Quote:
Originally Posted by danielbmartin
Disadvantage: uses temporary files.
Doesn't have to
 
1 members found this post helpful.
Old 11-26-2015, 10:45 AM   #10
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,879

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by grail View Post
Doesn't have to
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.

Daniel B. Martin

{Happy Turkey Day to all who observe!}
 
Old 11-30-2015, 09:52 AM   #11
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,879

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
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.

Daniel B. Martin
 
Old 11-30-2015, 10:45 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
Using Daniel's file names:
Code:
awk 'FNR==NR{a[NR]=$0;next}$0 = $0 a[FNR-1]' InFile2 InFile1
For the paste solution I used the process substitution twice:
Code:
tac <(paste -d "\0" <(tac InFile1) <(tac InFile2))
 
1 members found this post helpful.
Old 11-30-2015, 11:35 AM   #13
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,879

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by grail View Post
For the paste solution I used the process substitution twice:
Code:
tac <(paste -d "\0" <(tac InFile1) <(tac InFile2))
Concise and readable. Superb!

Daniel B. Martin
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to add header file daf1kpp Linux - Kernel 1 03-09-2014 07:58 AM
[SOLVED] add 250 single column files to create one file wilelucho Programming 10 06-27-2012 02:05 PM
[SOLVED] Add a column to a file. Sha_unix Linux - Newbie 9 11-23-2011 04:08 PM
[SOLVED] Need to add a column to a text file Thaidog Programming 2 04-28-2011 09:09 AM
[SOLVED] how to add blank lines whenever the value of a given column alters in a data file? vcmota Programming 9 01-11-2011 08:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration