LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   add column to file under header bash (https://www.linuxquestions.org/questions/programming-9/add-column-to-file-under-header-bash-4175559857/)

bishop2001 11-25-2015 08:48 AM

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

danielbmartin 11-25-2015 09:03 AM

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

bishop2001 11-25-2015 09:15 AM

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

pan64 11-25-2015 09:41 AM

what have you tried so far? Do you have probably a script already to work with.

schneidz 11-25-2015 09:45 AM

man paste

danielbmartin 11-25-2015 11:49 AM

Quote:

Originally Posted by schneidz (Post 5455147)
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

grail 11-25-2015 12:11 PM

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

danielbmartin 11-25-2015 12:39 PM

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

grail 11-26-2015 09:46 AM

Quote:

Originally Posted by danielbmartin
Disadvantage: uses temporary files.

Doesn't have to ;)

danielbmartin 11-26-2015 10:45 AM

Quote:

Originally Posted by grail (Post 5455662)
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!}

danielbmartin 11-30-2015 09:52 AM

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

grail 11-30-2015 10:45 AM

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))

danielbmartin 11-30-2015 11:35 AM

Quote:

Originally Posted by grail (Post 5457414)
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


All times are GMT -5. The time now is 08:09 AM.