LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Append output of a command in column wise manner (http://www.linuxquestions.org/questions/linux-newbie-8/append-output-of-a-command-in-column-wise-manner-4175440257/)

shivaa 12-06-2012 06:41 AM

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.

ntubski 12-06-2012 09:27 AM

Quote:

Originally Posted by shivaa (Post 4843727)
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"


shivaa 12-06-2012 11:52 PM

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?

David the H. 12-07-2012 07:39 AM

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"


ntubski 12-07-2012 07:47 AM

Quote:

Originally Posted by shivaa (Post 4844268)
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.

shivaa 02-18-2013 02:43 AM

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


All times are GMT -5. The time now is 04:53 AM.