LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Align lines in shell script (https://www.linuxquestions.org/questions/linux-general-1/align-lines-in-shell-script-762065/)

jeesun 10-15-2009 06:47 AM

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.

Simon Bridge 10-15-2009 06:57 AM

man column
Of course, there are much fancier methods if you want.

jeesun 10-15-2009 07:02 AM

Sorry. Its missing

$ man column
No manual entry for column

Any other hints?

rizhun 10-15-2009 07:05 AM

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.

jeesun 10-15-2009 11:43 PM

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?

Simon Bridge 10-16-2009 12:29 AM

Quote:

Originally Posted by jeesun (Post 3720173)
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.


All times are GMT -5. The time now is 04:24 PM.