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 12-16-2008, 07:55 AM   #1
g_kapoor85
LQ Newbie
 
Registered: Dec 2008
Location: INDIA
Distribution: RHEL4
Posts: 7

Rep: Reputation: 0
Smile need help regarding shell script


Hi,

I want to change the following output in tabular format..How can I do this?

Current output:

abc
100
def
200
ghi
300

But I want it in the following format, How can I implement this using shell script?

abc def ghi
100 200 300

Thanks!~

Kapoor
 
Old 12-16-2008, 07:57 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by g_kapoor85 View Post
Hi,

I want to change the following output in tabular format..How can I do this?

Current output:

abc
100
def
200
ghi
300

But I want it in the following format, How can I implement this using shell script?

abc def ghi
100 200 300

Thanks!~

Kapoor
Hmm..perhaps there are some tips elsewhere in your school textbook that could help you.

This sounds very much like homework.
 
Old 12-16-2008, 08:08 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
ditto--looks like homework.

g_kapoor; You need to tell us what you have tried, what books you are using in the class, etc.

I think this particular problem will involve one or more of the following commands:
sort
sed
awk

For "sort", the man page will give you what you need. For "sed" and "awk", try this: http://www.grymoire.com/Unix/
 
Old 12-16-2008, 08:26 AM   #4
g_kapoor85
LQ Newbie
 
Registered: Dec 2008
Location: INDIA
Distribution: RHEL4
Posts: 7

Original Poster
Rep: Reputation: 0
This is not a homework guys I was just trying to capture the output of a free command from 100 servers to an excel sheet. However, I want it in different columns instead of predefined format of free command.

I have acomplished it by redirecting ooutput to three different files using awk and pasting it to one file in tabular format and redirecting it which worked.

however, I thought it as a lengthy process to capture the outputs seperately and then pasting it That's why I asked this question hoping that some other trick will work.
Thanks!
 
Old 12-16-2008, 08:54 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by g_kapoor85 View Post
This is not a homework guys
OK!! Now we're talking!

My hunch is to first sort the list so it looks like this:
abc
def
ghi
100
200
300

Then--eg--you can use sed to combine groups of lines. Here is a start, based on the sorted list above:
sed -n -e '1,3H' -e '3{g;s/^\n//;s/\n/ /gp}' filename

slightly cleaner:
sed -n -e '1h' -e '2,3H' -e '3{g;s/\n/ /gp}' filename
 
Old 12-17-2008, 05:11 AM   #6
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
Try (using GNU sed)
Code:
sed -n 'h;n; :a x;$!N;s/\n/ /;ta;p;x;p' infile > outfile
It needs an even number of input lines or it will reverse the order of the two output lines.

Or try
Code:
sed -n 'p;n' infile | paste -sd' ' > outfile
sed -n 'n;p' infile | paste -sd' ' >> outfile
sed -n 'p;n' prints every second line starting with line 1.
sed -n 'n;p' prints every second line starting with line 2.
paste -sd' ' converts multiple lines into a single line with a space as the deliminater.
 
  


Reply



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 ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 06:24 AM
help with execute mulitple shell script within shell script ufmale Programming 6 09-13-2008 12:21 AM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 02:34 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM

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

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