LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 07-02-2015, 03:28 PM   #1
gctaylor1
Member
 
Registered: Dec 2006
Distribution: Red Hat
Posts: 45

Rep: Reputation: 0
Append two columns to file with printf.


I'm trying to find a way to use printf to append a column to two rows of a file at the same time. I currently have an awk script that appends one column to a single row. Like this:
Code:
printf " " sum+1 >> "/tmp/awktest.txt"
I want to build two rows at the same time while appending this data to an existing file. This is an example of how the file would grow with each passing addition of data to the file.
Code:
01:13:34 
10
Code:
01:13:34 02:13:34 
10	 15
Code:
01:13:34 02:13:34 03:13:34 
10	 15	  12
Code:
01:13:34 02:13:34 03:13:34 04:13:34 
10	 15	  12	   20
Code:
01:13:34 02:13:34 03:13:34 04:13:34 05:13:34
10	 15	  12	   20	    8
The formatting between the integer values does not have to be exactly as above. In the above example I just lined up the columns for easier reading. The example below is OK as well.
Code:
01:13:34 02:13:34 03:13:34 04:13:34 05:13:34
10 15 12 20 8
It seems like I should be able to use some combination of LF, CR, FF to do this but I have not been able to find the right combination. Is it possible to do what I'm asking?
 
Old 07-02-2015, 03:53 PM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
column may be what you are looking for.

With this InFile ...
Code:
01:13:34 02:13:34 03:13:34 04:13:34 05:13:34
10 15 12 20 8
... this column ...
column -t $InFile >$OutFile
... produced this OutFile ...
Code:
01:13:34  02:13:34  03:13:34  04:13:34  05:13:34
10        15        12        20        8
Daniel B. Martin
 
Old 07-02-2015, 04:53 PM   #3
gctaylor1
Member
 
Registered: Dec 2006
Distribution: Red Hat
Posts: 45

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by danielbmartin View Post
column may be what you are looking for.
Thanks Daniel. It appears that column is operating on an entire line? I'm not sure how well I explained my use case.

Here's another example that I hope makes it clearer.
What I do not want:
Code:
$ echo 'top bottom' | column -t
top  bottom
What I want is:
Code:
top
bottom
The next run should do the same:
What I want:
Code:
top top
bottom bottom
Gary
 
Old 07-02-2015, 09:55 PM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
This code ...
Code:
# Start with an empty file.
echo 'top1 bottom1'  \
|tr " " "\n"       \
>$OutFile
echo; echo "OutFile ..."; cat $OutFile; echo "End Of File"

# Now add a second datum.
cat $OutFile >$WorkFile
echo 'top2 bottom2'  \
|tr " " "\n"         \
|paste $WorkFile -   \
>$OutFile
echo; echo "OutFile ..."; cat $OutFile; echo "End Of File"

# Now add a third datum.
cat $OutFile >$WorkFile
echo 'top3 bottom3'  \
|tr " " "\n"         \
|paste $WorkFile -   \
>$OutFile
echo; echo "OutFile ..."; cat $OutFile; echo "End Of File"
... produced this result ...
Code:
OutFile ...
top1
bottom1
End Of File

OutFile ...
top1	top2
bottom1	bottom2
End Of File

OutFile ...
top1	top2	top3
bottom1	bottom2	bottom3
End Of File
Daniel B. Martin
 
  


Reply

Tags
awk, printf



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
Trying to append file to tar archive returns error “Cannot stat: No such file or dire Simon_zhu Linux - Newbie 3 06-11-2015 11:02 PM
[SOLVED] Converting a file with Rows and Columns to just Columns mphillips67 Linux - Newbie 14 03-05-2014 10:31 AM
how to append columns form a column file in another file adam_blackice Programming 4 09-17-2007 11:33 PM
[Perl] append columns to file noir911 Programming 3 02-08-2007 05:29 AM
printf: postion cursor to output columns schneidz Programming 5 09-20-2005 08:59 AM

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

All times are GMT -5. The time now is 07:23 AM.

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