awk to format file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to format file
# 1  
Old 01-11-2013
awk to format file

Hello,

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

Code:
/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

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

thank you very much, I begin it awk

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.
# 2  
Old 01-11-2013
Code:
awk -F'[ =]' '{ gsub(/\,.*|:/," ",$5); printf "%s servers=%s\n",$1,$5; }' file

# 3  
Old 01-11-2013
Code:
 awk -F"[ =:]" '{print $1,"servers=" $(NF-2),$(NF-1),$NF}'

# 4  
Old 01-11-2013
try also:
Code:
awk '{sub(" .*="," servers="); gsub(":"," "); print}' infile > another_file

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

# 5  
Old 01-11-2013
Quote:
Originally Posted by bipinajith
Code:
awk -F'[ =]' '{ gsub(/\,.*|:/," ",$5); printf "%s servers=%s\n",$1,$5; }' 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:
Code:
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:
Code:
#!/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):
Code:
./get_servers; ./get_servers access

it will produce the output:
Code:
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

# 6  
Old 01-14-2013
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.
Code:
ssh -q server lsnfsmnt for example

thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to format file with conditional split

In the awk below I am splitting $7 on the : (colon) then - (hyphen) as array a. The word chr is printed at the start of every $1 line. Next, $4 is split on the > (greater then) as array b. I am not sure how to account for the two other possibilities in $4 so the correct output is printed. Every... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Format a file using awk

I have a file as below empty May7 noread May8 How can process this file and print as below. neat and clean. Filename Date Created empty May7 noread May8 I tried something with printf before creating the file itself... (3 Replies)
Discussion started by: Tuxidow
3 Replies

3. Shell Programming and Scripting

Changing format of file with awk

Hi all, I have a file that looks like this: Closest words to: manifesto >>>> Closest words to: passport >>>> and I want to reformat this with awk with the following desired result: manifesto 0.99999999999999978, 'manifesto' 0.72008211381623111, 'communiqu\xe9'... (5 Replies)
Discussion started by: owwow14
5 Replies

4. Shell Programming and Scripting

Need help on awk script to format a file

Hi everyone, Looking for some help in awk script to modify the format of a file. I seem to be struggling with this: Input: ***************************************************** ,,,,TOTAL,2014/1-,2014/2- 0,TOTAL,BLANK,"Description",Total 1,Total 2,Total 3 1, MFG Name 1,,"Description",MFG... (4 Replies)
Discussion started by: yogesh_jain
4 Replies

5. Shell Programming and Scripting

Help with awk statement to format text file.

Hello, I am fairly new to shellscripting and have written a script to check on messages file and report failed logins: Here is the original file: Jul 17 03:38:07 sfldmilx086 sshd: error: PAM: Authentication failure for houghn97 from 10.135.77.201 Jul 17 03:38:07 sfldmilx086 sshd: error:... (2 Replies)
Discussion started by: neilh1704
2 Replies

6. Shell Programming and Scripting

Change a file content format using awk

Hi, i have a file input.txt Continent North America Country USA Capital Washington D.C. Country Canada Capital Ottawa Continent South America Country Argentina Capital Buenos Aires Country Brazil Capital Brasília Coutry Colombia Capital Bogotá and i want to get an output.txt ... (3 Replies)
Discussion started by: fastlane3000
3 Replies

7. Shell Programming and Scripting

Need awk/sed to format a file

My content of source file is as below scr1 a1 scr2 a2 b2 scr3 a3 b3 c3 I need a awk/sed command (to be used in C shell)to format it to something like below scr1 $a1 >file1 scr2 $a2 $b2 >file2 scr3 $a3 $b3 $c3 >file3 (12 Replies)
Discussion started by: animesharma
12 Replies

8. Shell Programming and Scripting

File Join with different format using awk

File Join with different format using awk file1 file2 nawk -F"=" 'NR==FNR{a++;next} a{ print a, output Help is appreciated (5 Replies)
Discussion started by: pinnacle
5 Replies

9. Shell Programming and Scripting

awk/nawk question to format a file

Hi, I am new to awk/nawk, needs help. I want to merge the rows having emplid attribute same into a single row in the following file. In actual this kind of file will have around 50k rows. Here is my input file id|emplid|firstname|dep|lastname 1|001234|test|1001|1 2|002345|test|1032|2... (7 Replies)
Discussion started by: kumar04
7 Replies

10. Shell Programming and Scripting

AWK CSV to TXT format, TXT file not in a correct column format

HI guys, I have created a script to read 1 column in a csv file and then place it in text file. However, when i checked out the text file, it is not in a column format... Example: CSV file contains name,age aa,11 bb,22 cc,33 After using awk to get first column TXT file... (1 Reply)
Discussion started by: mdap
1 Replies
Login or Register to Ask a Question