Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

format of flat file :

;metier_code              ;
;-------------------------;

(0 rows affected)
;CRDS_Ptf_No;          ;                                ;                         ;          ;                                ; ;                         ;                         ;               ;               ;               ;                         ;                         ;     ;Status;
;-----------;----------;--------------------------------;-------------------------;----------;--------------------------------;-;-------------------------;-------------------------;---------------;---------------;---------------;-------------------------;-------------------------;-----;------;
;       NULL;ABCD      ;ABHJARS                         ;                         ;ABCD      ;ABCD                            ;Y;                         ;                         ;               ;               ;               ;                         ;                         ;     ;A     ;
;       1234;XEU-ANKD  ;XEU-AJKD                        ;                         ;ABCD      ;ABCD                            ;Y;                         ;                         ;               ;               ;               ;                         ;                         ;     ;A     ;
.
.
;      11745;ANJLDMAOKD;AMKDJ AN DJ JAHF AS CPFVH ACCR  ;NONE                     ;AN DJ JAHA;AN DJ JAHA                      ;Y;NO ANKIO GAP             ;YES AMK SCF              ;               ;               ;               ;                         ;                         ;     ;I     ;
;      11744;AMKDIONSKH;AMKDJ AN DJ JAHF AS CPFVH MTM   ;NONE                     ;AN DJ JAHA;AN DJ JAHA                      ;Y;NO ANKIO GAP             ;YES AMK SCF              ;               ;               ;               ;                         ;                         ;     ;I     ;

(5436 rows affected)
(return status = 0)

Return parameters:

;           ;
;-----------;
;       5436;


(1 row affected)
;       ;           ;
;-------;-----------;
;grepkey;       5436;

(1 row affected)

Below script is used to format the flat file.

awk -F ';' '/^;-----------;/ {start=1;next;}; start==0 {next;}; /^[^;]|^$/ { exit;}; { line_nr++; gsub(" +",""); print line_nr "" $0;}' temp_file > test

Below is the correct output that i am getting by using the above script .

1;NULL;ABCD;ABHJARS;;ABCD;ABCD;Y;;;;;;;;;A;
2;NULL;XEU-ANKD;XEU-AJKD;;ABCD;ABCD;Y;;;;;;;;;A;
3;NULL;SWAPOLEIL;SWAPOLEIL;;QWERDF;QWERDF;Y;;;;;;;;;A;
.
.
5436;11744;AMKDIONSKH;AMKDJ AN DJ JAHF AS CPFVH MTM;;QWERDF;QWERDF;Y;;;;;;;;;A;

CAN ANYONE PLEASE EXPLAIN ME THE BELOW AWK SCRIPT, which is used to format the file

awk -F ';' '/^;-----------;/ {start=1;next;}; start==0 {next;}; /^[^;]|^$/ { exit;}; { line_nr++; gsub(" +",""); print line_nr "" $0;}' temp_file > test

i am new to scripting world.

i was able to understand few things from the above script : 1) /^[^;]|^$/ { exit;}; stops the processing if a line occurs which does not start with ; 2) it is ignoring the leading lines

please help me understand the script and let me know if the script needs to be modified inorder to be more generic, if so.

Thanks in Advance!

share|improve this question

3 Answers 3

Awk scripts are much easier to understand if you format them with one condition per line:

'/^;-----------;/ {start=1;next;};

On reading A line beginning with ";----------;", set the variable start to true, then go to the next line of input without printing anything.

start==0 {next;};

If start is false, go to the next line of input without printing anything.

/^[^;]|^$/ { exit;};

If a line which starts with a character other than ; or is empty is read in, stop processing the file and exit (a simpler equivalent would be !/^;/{exit})

{ line_nr++; gsub(" +",""); print line_nr "" $0;}

For all other lines, increment a counter, delete all spaces from the line, then print the counter and the updated line.

share|improve this answer
    
The gsub removes spaces, not plus signs. It's a regex. –  daniel kullmann May 12 at 10:15
    
Oops, right. It's been a while. –  James McLeod May 12 at 10:27

Seems like awk -F ';' '/^;-----------;/ {start=1;next;}; start==0 {next;}; /^[^;]|^$/ { exit;}; { line_nr++; gsub(" +",""); print line_nr "" $0;}' temp_file > test is a complicated version of nl -w 1 -p -s';' temp_file > test (hint via Google).

share|improve this answer
    
Would be great if anyone could explain me the awk script that i shared. –  Rajiv May 12 at 7:39

The script skips all lines (2. command) until it comes to a line starting with ;----------; (1. command). It then prints the following lines, removing extraneous spaces (4. command), until it reaches either an empty line or one not starting with ; (3. command)

In short: it prints out the first block that is delimited by ;----------; and (empty line or line not starting with ;).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.