format the output from a file

hi ,
i need to format the output which is availble in a file
file output is
Following are the Process_Scheduler Domains running in the server Ram-pc
VPORCL
Following are the Application Server domains running in the server Ram-pc
VPORCL01
VPORCL02

these value VPORCL,VPORCL01... are dynamic value i need to print this out int his format

hostname domaintype domain name
Ram-pc Process_Scheduler VPORCL
Ram-pc Application Server VPORCL01
Ram-pc Application Server VPORCL02

thanxs in advance

try out this..

NoOfWords=0
while read LINE
do
        NoOfWords=`echo $LINE|wc -w|awk '{print $1}'`
        #echo "NoOfWords : [$LINE][$NoOfWords]"
        if [ "$NoOfWords" -eq "1" ]
        then
                echo "$OutString " " $LINE"
        elif [ "$NoOfWords" -eq "10" ]
        then
                OutString=`echo $LINE|awk '{printf "%s %s", $10,$4}'`
        elif [ "$NoOfWords" -eq "11" ]
        then
                OutString=`echo $LINE|awk '{printf "%s %s %s", $11,$4,$5}'`
        fi
done < Filename

Note : instead of Filename, you have to give the input filename.

Done using without external programs like awk.
1st version is generic, remove extra data from lines.

#!/usr/bin/ksh
while read id restline
do
        case "$restline" in
                "") # only id, so print line
                    print "$prevheader $id"
                    ;;
                *)  # long line, remove constant/extra strings
                    str=${restline/are the/}
                    str=${str/running in the server/}
                    str=${str/[Dd]omains/}
                    # what we have ? Values
                    prevheader=$str
                    ;;
        esac
done < input.txt

And then to solution for this case. Need to change field order

print "____________________________________________________"
# change fld order
while read id restline
do
        case "$restline" in
                "") print "$prevheader $id" ;;
                *)  str=${restline/are the/}
                    str=${str/running in the server/}
                    str=${str/[Dd]omains/}
                    # fields to array flds
                    set -A flds -- $str
                    lastfld=${#flds[*]}
                    # first id = 0
                    ((lastfld-=1))
                    # last field value
                    prevheader=${flds[$lastfld]}
                    ((lastfld-=1))
                    # rest fields
                    fld=0
                    while ((fld<=lastfld))
                    do
                          prevheader="$prevheader ${flds[$fld]}"
                          ((fld+=1))
                    done
                    ;;
        esac
done < input.txt

Or...

awk -F '(Following are the |[dD]omains running in the server )' 'NF>1{x=$3 OFS $2}NF==1{print x $1}' file1

...gives...

Ram-pc Process_Scheduler VPORCL
Ram-pc Application Server VPORCL01
Ram-pc Application Server VPORCL02

my solution is longer than ygor's but u can try :slight_smile:

awk '{ if(NF==1) printf ("%s %s\n", text, $NF);else if (NF==10) text = $10 OFS $4; else if(NF==11)  text = $11 OFS $4 OFS $5; }' test.txt
my($type,$name);
while(<DATA>){
	if(/.*the\s+(.*)\s+[Dd]omains.*server\s+(.*)/){
		$type=$1;
		$name=$2;
		next;
	}
	print $name," ",$type," ",$_;
}
__DATA__
Following are the Process_Scheduler Domains running in the server Ram-pc
VPORCL
Following are the Application Server domains running in the server Ram-pc
VPORCL01
VPORCL02