awk to format file

Hello,

I shall like using the function awk to modify the contents of the following file:

/tmp/conf-1 -sec=sys,rw=lpar1:lpar2:lpar3,access=lpar1:lpar2:lpar3
/tmp/conf-2 -vers=4,sec=sys,rw=lpar4:lpar5:lpar6,access=lpar4:lpar5:lpar6
 

I need to have the result below towards another file

/tmp/conf-1 servers=lpar1 lpar2 lpar3
/tmp/conf-2 servers=lpar4 lpar5 lpar6

thank you very much, I begin it awk

awk -F'[ =]' '{ gsub(/\,.*|:/," ",$5); printf "%s servers=%s\n",$1,$5; }' file
 awk -F"[ =:]" '{print $1,"servers=" $(NF-2),$(NF-1),$NF}'

try also:

awk '{sub(" .*="," servers="); gsub(":"," "); print}' infile > another_file

or

sed 's/ .*=/ servers=/; s/:/ /g' infile > another_file

The input file format is poorly specified by the two line input sample given in the first message in this thread. It isn't clear from this how many comma separated attributes are expected in the second field in the input, whether access= will always be the last attribute, or even if this is the field that should be used to get the list of servers. The attribute rw= contains the same list of servers in the two given lines, but we don't know if both attributes will always appear nor that, if both are present, they will contain the same data. When ambiguous descriptions of the input are provided, there is much less chance of possible solutions being posted that will actually produce the desired output.

The awk script bipinajith provided above is clever code that uses field #5 (using space and equals signs as delimiters) to get the server list and adjusts for the fact that there are 5 fields in the 1st line and 6 fields in the 2nd line by translating ,access to a single space character (which then becomes a trailing space in the output) in the second line. However, if other input lines add more attributes, this might not work.

With the given input, it would be simpler to just always use the last field:

awk -F'[ =]' '{gsub(/:/," ",$NF); printf "%s servers=%s\n",$1,$NF}' file

And this will always work if the access= attribute is the last attribute in the comma separated list of attributes on all input lines.

If some input lines have other attributes following the access= attribute, the following more general solution should still work and lets you name the attribute to be used to get the list of servers:

#!/bin/ksh
attr_name=${1:-rw}
printf "Run awk script using attribute \"%s\" to get server list:\n" $attr_name
awk -v an="$attr_name" -F '[ ,]' '# Use space and comma as field delimiters.
BEGIN { # Create a patter to match the attribute to use to get the server list.
        attr = "^" an "="
}
{       # Field 1 contains a pathname, search the remaining comma separated
        # fields on each input line for an attribute containing the list of
        # servers.
        for(i = 2; i <= NF; i++)
                if($i ~ attr) {
                        # We have a match, strip off the attribute name.
                        sub(attr, "", $i)
                        # Change colon separators between server names to
                        # spaces.
                        gsub(/:/, " ", $i)
                        # Print the pathname and the server list.
                        printf("%s servers=%s\n", $1, $i)
                        # Skip the remainder of this input line.
                        next
                }
}' file

If you save the above Korn shell script in a file, change the /bin/ksh in the first line to an absolute path to the Korn shell (or bash, or a Bourne shell) on your system, change file on the last line of the script to the name of your sample input file, make it executable, and run it as shown below (assuming you save it in a file named get_servers ):

./get_servers; ./get_servers access

it will produce the output:

Run awk script using attribute "rw" to get server list:
/tmp/conf-1 servers=lpar1 lpar2 lpar3
/tmp/conf-2 servers=lpar4 lpar5 lpar6
Run awk script using attribute "access" to get server list:
/tmp/conf-1 servers=lpar1 lpar2 lpar3
/tmp/conf-2 servers=lpar4 lpar5 lpar6

Hi,

I tested the various codes, it works with no problem at all.
Thanks to all for your reactivity it is exactly what I wanted.

Good day

---------- Post updated at 06:31 AM ---------- Previous update was at 04:04 AM ----------

A last thing.
After generated this list, i have to launch a command ssh on every partition
--> servers=x x x of the list in the awk.

ssh -q server lsnfsmnt for example

thanks