awk operation to get table formatted file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk operation to get table formatted file
# 1  
Old 02-05-2016
awk operation to get table formatted file

I need to parse .conf file in Unix system to get output from the snippets like below :

Code:
[sub_suffix-324]
; generated by mod_identity
exten => int,1,GoSub(mdc_template-3,s,1)
exten => int,n(next_380),Return()
; default action for busy
exten => ext,1,GoSub(mdc_template-3,s,1)
exten => ext,n,Set(PRI_CAUSE=17)
exten => ext,n,HangUp(17)
exten => ext,n(back),Return()

I interested specifically about below lines:

Code:
[sub_suffix-324]
exten => int,1,GoSub(mdc_template-3,s,1)
exten => ext,1,GoSub(mdc_template-3,s,1)

to get output to another file as below:

Code:
Extension   int_voicemail   ext_voicemail

324            yes(if int exist)                yes(if ext exist)

My initial awk script :


Code:
#!/usr/bin/awk -f

/^\[sub_suffix-/ {

  print $0
  next
}

/mdc_template/ {
print substr($3,1,3)
next
}

And the output like:
Code:
[sub_suffix-324]
int
ext
[sub_suffix-424]
int
ext
[sub_suffix-321]
[sub_suffix-3245]
ext
[sub_suffix-7435]
[sub_suffix-123]
[sub_suffix-325]
int
ext

but i need something like:
Code:
324
int
ext
424
int
ext
321
null
null
3245
null
ext
7435
null
null
123
null
null
325
int
ext

then format this as:
Code:
extension  int_mail    ext_mail
324           yes           yes
424           yes           yes
321           null           null
.
.
.
.

Any help is appreciated Smilie

Thanks
cheecky
Moderator's Comments:
Mod Comment Please use CODE tags (not QUOTE tags) when displaying sample input, sample output, and code segments.

Last edited by Don Cragun; 02-05-2016 at 07:16 PM.. Reason: Change QUOTE tags to CODE tags and add missing CODE tags.
# 2  
Old 02-05-2016
Making some wild guesses about an input file that produces the intermediate files you said you wanted, and writing a simple awk script that goes straight from the input file to the output you said you wanted (without creating intermediate files), you might want to try something like:
Code:
awk '
BEGIN {	FS = "[] ,-]"
	OFS = "\t"
	print "Extension", "int_mail", "ext_mail"
	OFS = OFS OFS
}
function ext_print() {
	if(e)	print e, f["int"], f["ext"]
	e = $2
	f["ext"] = f["int"] = "null"
}
/^[[]sub_suffix/ {
	ext_print()
	next
}
/mdc_template/ {
	f[$3] = "yes"
}
END {	ext_print()
}' file

If the file named file contains:
Code:
[sub_suffix-324]
; generated by mod_identity
exten => int,1,GoSub(mdc_template-3,s,1)
exten => int,n(next_380),Return()
; default action for busy
exten => ext,1,GoSub(mdc_template-3,s,1)
exten => ext,n,Set(PRI_CAUSE=17)
exten => ext,n,HangUp(17)
exten => ext,n(back),Return()
[sub_suffix-424]
; generated by mod_identity
exten => int,1,GoSub(mdc_template-3,s,1)
exten => int,n(next_380),Return()
; default action for busy
exten => ext,1,GoSub(mdc_template-3,s,1)
exten => ext,n,Set(PRI_CAUSE=17)
exten => ext,n,HangUp(17)
exten => ext,n(back),Return()
[sub_suffix-321]
; generated by mod_identity
exten => int,1,GoSub(mdc_template-3,s,1)
exten => int,n(next_380),Return()
; default action for busy
exten => ext,n,Set(PRI_CAUSE=17)
exten => ext,n,HangUp(17)
exten => ext,n(back),Return()
[sub_suffix-3245]
; generated by mod_identity
exten => int,n(next_380),Return()
; default action for busy
exten => ext,1,GoSub(mdc_template-3,s,1)
exten => ext,n,Set(PRI_CAUSE=17)
exten => ext,n,HangUp(17)
exten => ext,n(back),Return()
[sub_suffix-7435]
; generated by mod_identity
exten => int,n(next_380),Return()
; default action for busy
exten => ext,n,Set(PRI_CAUSE=17)
exten => ext,n,HangUp(17)
exten => ext,n(back),Return()
[sub_suffix-123]
; generated by mod_identity
exten => int,n(next_380),Return()
; default action for busy
exten => ext,n,Set(PRI_CAUSE=17)
exten => ext,n,HangUp(17)
exten => ext,n(back),Return()
[sub_suffix-325]
; generated by mod_identity
exten => int,1,GoSub(mdc_template-3,s,1)
exten => int,n(next_380),Return()
; default action for busy
exten => ext,1,GoSub(mdc_template-3,s,1)
exten => ext,n,Set(PRI_CAUSE=17)
exten => ext,n,HangUp(17)
eaten => ext,n(back),Return()

it produces the output:
Code:
Extension	int_mail	ext_mail
324		yes		yes
424		yes		yes
321		yes		null
3245		null		yes
7435		null		null
123		null		null
325		yes		yes

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awik or nawk.
# 3  
Old 02-08-2016
Code:
cat cheecky.conf

Code:
[sub_suffix-324]
; generated by mod_identity
exten => int,1,GoSub(mdc_template-3,s,1)
exten => int,n(next_380),Return()
; default action for busy
exten => ext,1,GoSub(mdc_template-3,s,1)
exten => ext,n,Set(PRI_CAUSE=17)
exten => ext,n,HangUp(17)
exten => ext,n(back),Return()
[sub_suffix-424]
; generated by mod_identity
exten => int,1,GoSub(mdc_template-3,s,1)
exten => int,n(next_380),Return()
; default action for busy
exten => ext,1,GoSub(mdc_template-3,s,1)
exten => ext,n,Set(PRI_CAUSE=17)
exten => ext,n,HangUp(17)
exten => ext,n(back),Return()
[sub_suffix-321]
; generated by mod_identity
exten => int,n(next_380),Return()
; default action for busy
exten => ext,n,Set(PRI_CAUSE=17)
exten => ext,n,HangUp(17)
exten => ext,n(back),Return()


Code:
#!/usr/bin/env perl -w
# cheecky.pl
use strict;

my @suf = ("extension", "int_mail", "ext_mail");
while (<>) {
    if( /^\[sub_suffix-(\d+)\]$/ ){
        if ( $suf[0] ){
            display();
            @suf = (undef, "null", "null");
        }
        $suf[0] = $1;
    }
    else {
        /int,.,GoSub/ and $suf[1] = "yes" and next;
        /ext,.,GoSub/ and $suf[2] = "yes";
    }
}
display();

sub display {
    printf "%9s\t%8s\t%8s\n", $suf[0], $suf[1], $suf[2];
}

Code:
perl cheecky.pl cheecky.conf

Code:
extension       int_mail        ext_mail
      324            yes             yes
      424            yes             yes
      321           null            null

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Processing a formatted file with awk

Hi - I want to interrogate information about my poker hands, sessions are all recorded in a text file in a particular format. Each hand starts with the string <PokerStars> followed by a unique hand reference and other data like date/time. There is then all the information about each hand. My first... (5 Replies)
Discussion started by: rbeech23
5 Replies

2. Shell Programming and Scripting

awk script to find data in three file and perform replace operation

Have three files. Any other approach with regards to file concatenation or splitting, etc is appreciated If column55(billngtype) of file1 contains YMNC or YPBC then pick the value of column13(documentnumber). Now find this documentnumber in column1(Billdoc) of file2 and grep the corresponding... (4 Replies)
Discussion started by: as7951
4 Replies

3. Shell Programming and Scripting

Multiple Replacement in a Text File in one operation (sed/awk) ?

Hi all, Saying we have two files: 1. A "Reference File" whose content is "Variable Name": "Variable Value" 2. A "Model File" whose content is a model program in which I want to substitute "VariableName" with their respective value to produce a third file "Program File" which would be a... (4 Replies)
Discussion started by: dae
4 Replies

4. Shell Programming and Scripting

awk --> math-operation in data-record and joining with second file data

Hi! I have a pretty complex job - at least for me! i have two csv-files with meassurement-data: fileA ...... (2 Replies)
Discussion started by: IMPe
2 Replies

5. Shell Programming and Scripting

awk to convert table-by-row to matrix table

Hello, I need some help to reformat this table-by-row to matrix? infile: site1 A:o,p,q,r,s,t site1 C:y,u site1 T:v,w site1 -:x,z site2 A:p,r,t,v,w,z site2 C:u,y site2 G:q,s site2 -:o,x site3 A:o,q,s,t,u,z site3 C:y site3 T:v,w,x site3 -:p,routfile: SITE o p q r s t v u w x y... (7 Replies)
Discussion started by: yifangt
7 Replies

6. Shell Programming and Scripting

awk, sed, perl assistance in outputting formatted file

Hello, Please advise. Scoured this site, as well as google for answers. However if you do not know what to search for, it's a bit hard to find answers. INPUT: ACTASS= 802 BASECOS= 279 COSNCHG= 3 CUSCOS= 52 UPLDCOS= 2 DESIRED OUTPUT: ACTASS=802 BASECOS=279 (13 Replies)
Discussion started by: abacus
13 Replies

7. Shell Programming and Scripting

output - tab formatted - awk

Dear All, Good Day. I would like to hear your suggestions for the following problem: I have a file with 5 columns with some numbers in 16 lines as shown below. Input file: Col 1 Col 2 Col 3 Col 4 Col 5 12 220 2 121 20 234 30 22 9... (3 Replies)
Discussion started by: Fredrick
3 Replies

8. UNIX for Dummies Questions & Answers

Problem with formatted printing in AWK

Hi, I want to print 130 fileds using formatted printing in AWK. It looks like awk '{printf ("%7.2f%7.2f...%7.2\n",$1,$2,...,$130)}' inflie>oufile But it gives me an error: Word too long! Can you please help me with this? Is there another way to do this? (1 Reply)
Discussion started by: PHL
1 Replies

9. Shell Programming and Scripting

Formatted output - awk

Hi I have the following records in a file SABN YOURTUBE 000514 7256 SACN XYOUDSDF 000514 7356 SADN KEHLHRSER 000514 7656 SAEN YOURTUBE 000514 7156 SAFN YOURTUBE 000514 7056 I need to put this in the format like this printf '%s %-50s %6s %-6s\n' I am not going to read individual... (3 Replies)
Discussion started by: dhanamurthy
3 Replies

10. Shell Programming and Scripting

awk - print formatted without knowing no of cols

Hi, i want to print(f) the content of a file, but i don't know how many columns it has (i.e. it changes from each time my script is run). The number of columns is constant throughout the file. Any suggestions? (8 Replies)
Discussion started by: bistru
8 Replies
Login or Register to Ask a Question