LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-06-2012, 06:41 AM   #1
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Append output of a command in column wise manner


Hello!
Suppose a command generates output in every 1 hr. in form:
Code:
Server1
1
2
3
4
5
And I am redirecting this output in some file, let's say output.txt
So I want that everytime this script runs, it appends the output in next column of the file output.txt, not below the existing content. It should look like:
Code:
Server1 Server1 Server1 ......
1       1       1       ......
2       2       2       ......
3       3       3       ......
4       4       4       ......
5       5       5       ......
Apparently it can be managed using paste cmd, but I do not want to generate too many files. So any idea is welcome.

Last edited by shivaa; 12-06-2012 at 06:44 AM. Reason: Info updated
 
Old 12-06-2012, 09:27 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by shivaa View Post
Apparently it can be managed using paste cmd, but I do not want to generate too many files.
What's "too many"? Using paste would require just one temp file per run. To avoid any temp files, I guess you could read the current file entirely into memory:
Code:
#!/bin/bash
oldoutput=$(< output.txt)
paste - <(the_script) > output.txt <<<"$oldoutput"
 
1 members found this post helpful.
Old 12-06-2012, 11:52 PM   #3
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800

Original Poster
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
oldoutput=$(< output.txt)
paste - <(the_script) > output.txt <<<"$oldoutput"
It's not working properly or may be I couldn't implement it correctly. Script is simple as:
Sample.sh script:
Code:
echo "usre1 user2" > output.txt
ls /home/user1 > file1
ls /home/uesr2 > file2
paste -d" " file1 file2 >> output.txt
Now when this script will run next time, it should show:
Code:
cat output.txt
user1  user2  user1  user2
A      E      A      E  
B      F      B      F
C      G      C      G
D      H      D      H
Also could elaborate your cmd little more (points marked in red)? What's does <<< do?
 
Old 12-07-2012, 07:39 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Those are bashisms.

"$( <filename ) is a built-in shortcut that acts like "$( cat filename )"

"<( command )" is process substitution

"<<<word" is a here string, a simplified form of the here document.

IIRC, at least some of your work is being done on a Unix box, without the availability of GNU tools, and perhaps an older version of bash? Remember to always detail the environment you're using in your posts if it's significantly different from that of a recent Linux distro.

But if the tools that you use can't handle the syntax given, then you'll have to find some workaround. I don't see any problem with using text files if that's all you have available. You just need three files, used in rotation.

(Note that the code below is untested)

Code:
echo "user1" > "$tempfile1"
printf '%s\n' "user1" "/home/user1"/* >> "$tempfile1"

for dname in /home/user2 /home/user3 /home/user4; do

	echo "${dname##*/}" > "tempfile2"
	printf "%s\n" "$dname"/* >> "$tempfile2"

	paste -d" " "$tempfile1" "$tempfile2" > "$outputfile"

	cp -f "$outputfile" "$tempfile1"

done

rm -f "$tempfile1" "$tempfile2"
 
1 members found this post helpful.
Old 12-07-2012, 07:47 AM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by shivaa View Post
It's not working properly or may be I couldn't implement it correctly.
What does "not working properly" mean? Please post error messages, or the actual outcome. My guess is you have an older version of bash, or a plain bourne shell.


Quote:
Sample.sh script:
Code:
echo "usre1 user2" > output.txt
...
This script is overwriting output.txt every time. I was assuming a script that writes to stdout.
 
Old 02-18-2013, 02:43 AM   #6
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800

Original Poster
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Thanks David and ntubski! I found some really good study material at Greg's wiki.
Ciao!!
 
  


Reply

Tags
add, awk, columns, output, paste



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
[SOLVED] Converting the contents of a column with the output of the date command using awk mystupidquestion Programming 4 10-02-2011 04:22 PM
[SOLVED] append command output to file by giving command in terminal sumeet inani Linux - Newbie 4 07-03-2009 10:36 AM
printing column wise bharatbsharma Programming 4 09-11-2008 11:00 AM
command to sort by floats in fourth column of standard output jhwilliams Linux - Software 4 06-22-2007 01:27 PM
shell scipting: append output of a command to a variable jonhewer Linux - Newbie 10 08-24-2005 05:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:18 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