LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-15-2009, 06:47 AM   #1
jeesun
Member
 
Registered: Aug 2007
Location: Australia
Distribution: RedHat Enterprise
Posts: 82

Rep: Reputation: 15
Align lines in shell script


I have a file containing lines bellow

Name abc
Status available
Name def
Status available
.............
Now I need to rearrange the output as like in POSIX script.

Name Status
---------- --------
abc available
def available.


Appreciate for your help.
 
Old 10-15-2009, 06:57 AM   #2
Simon Bridge
LQ Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 198Reputation: 198
man column
Of course, there are much fancier methods if you want.
 
Old 10-15-2009, 07:02 AM   #3
jeesun
Member
 
Registered: Aug 2007
Location: Australia
Distribution: RedHat Enterprise
Posts: 82

Original Poster
Rep: Reputation: 15
Sorry. Its missing

$ man column
No manual entry for column

Any other hints?
 
Old 10-15-2009, 07:05 AM   #4
rizhun
Member
 
Registered: Jun 2005
Location: England
Distribution: Ubuntu, SLES, AIX
Posts: 268

Rep: Reputation: 47
Try something like this:

create_report.sh:
Code:
#!/bin/bash

datafile=$1

awk '

  BEGIN {
    namecount = 0
    statuscount = 0
  }

  /Name/ {
    namearray[namecount] = $2
    namecount++
  }

  /Status/ {
    statusarray[statuscount] = $2
    statuscount++
  }

  END {
    printf "%-20s %-20s\n", "Name", "Status"
    print "---------------------------------------------"
    for (x = 0; x <= statuscount; x++)
      printf "%-20s %-20s\n", namearray[x], statusarray[x]
  }

' ${datafile}
Make sure it's executable:
Code:
$ chmod +x create_report.sh
Then run it with a parameter of your data file:
Code:
$ ./create_report.sh /path/to/your/datafile.txt
Your data file must be exactly as you described it for this to work.

Hope this helps.

Last edited by rizhun; 10-15-2009 at 07:51 AM. Reason: Mis-read the question...
 
Old 10-15-2009, 11:43 PM   #5
jeesun
Member
 
Registered: Aug 2007
Location: Australia
Distribution: RedHat Enterprise
Posts: 82

Original Poster
Rep: Reputation: 15
Thanks a lot.

One more thing. Suppose i have the lines bellow with some exceptional lines like

not available
cannot display
..............
Name abc
Status available
Name def
Status available
.............

i want to rearrange the "not available" and "cannot display" to like the following lines

Name cannot display
Status not available
..............
Name abc
Status available
Name def
Status available
.............

How can i do this along with your script?
 
Old 10-16-2009, 12:29 AM   #6
Simon Bridge
LQ Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 198Reputation: 198
Quote:
Originally Posted by jeesun View Post
Sorry. Its missing

$ man column
No manual entry for column

Any other hints?
Yes - look closely at the bit where I wrote "man column", you will see that it is a hyperlink to a web page ... follow the link.

Quote:
One more thing. Suppose i have the lines bellow with some exceptional lines like
[snip]

How can i do this along with your script?
This is going to sound harsh, but you need to learn how that script works. When you do, you'll know how to change it to suit your needs.

Otherwise you'll just be asking the same question over and again with slightly different parameters.

In the script, printf arguments are used to format the columns to get them to line up. The rest is about sorting the data in the datafile - in your example, the data is organised fairly sensibly. If you are saying that some data in the file is organised differently, they you will have to go through and reorganise it, then remember to be better organised in future.

in the original organisation, every other line has the same kind of information. starting with the first gives the names, starting with the second gives the status.

the actual name and status is in fact the second word in each line, so you need to miss the first word out when you copy the line over to the namearray and statusarray structures.

you have an exception though - where some status entries say "cannot display" and "not available". The scheme described above would print "display available" in the row for that entry - not good.

The easiest approach is to find-and-replace on the datafile to replace those entries, as you have realised.

eg, you find the pattern "cannot display \n not available" and replace with "Name cannot display \n Status not available".

My awk is a tad rusty but I suspect it goes like this:

gsub("cannot display \n not available" "Name cannot display \n Status not available", datafile)

... goes after the awk and before /NAME/

You can also do this during processing, as an additional test in the loops which populate the arrays, or as a post-process on the arrays, before printing.

(if the namearray entry says "display" then it, and it's corresponding statusarray entry got changed.)

The trick is to think in terms of moving text around.
There are awk tutorials online - go do them.

Last edited by Simon Bridge; 10-16-2009 at 01:06 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Looking for a shell script that prints a file only if it has more than 10 lines djfog Linux - Newbie 8 07-01-2008 12:23 PM
SHELL Script to insert lines after certain Intervals rahulruns Linux - General 9 02-26-2008 01:21 AM
Shell script to jump over lines horacioemilio Programming 8 12-02-2007 05:44 PM
insert lines in a function using shell script shyamdey Programming 1 08-30-2006 07:48 AM
shell script for expanding lines in a file b123coder Programming 1 05-25-2006 04:51 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:14 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration