Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parse text file

Status
Not open for further replies.

seifou45

Technical User
May 2, 2022
9
DZ
hi all:

I have just registred to start a new thread regarding use of awk to parse text file and I' m new in awk language

I took a portion of data from texte file :

Code:
DATA                 
                     
DATA                 
GHR1                 
                     
BRAND                
TRUE                 
                     
     LST             
     115-34-SPECT    
     115-42-SPECT    
     115-50-SPECT    
                     
                     
BRAND                
FALSE                
                     
     LIST            
     115-34-SPECT    
     115-42-SPECT    
     115-50-SPECT    
                     
END

I have parsed the text file with this code . test file is texte file example.

Code:
awk -v  '/^DATA/&& /BRAND/ {if(NR>1) print  "DATAFIRST=" getline }' test

I want to get bellow data :

Code:
DATAFIRST=GHR1,LSIT=115-34-SPECT,BLOC,BRAND=TRUE;   
DATAFIRST=GHR1,LSIT=115-42-SPECT,BLOC,BRAND=TRUE;   
DATAFIRST=GHR1,LSIT=115-50-SPECT,BLOC,BRAND=TRUE;   
                                                    
DATAFIRST=GHR1,LSIT=115-34-SPECT,DEBLOC,BRAND=FALSE;
DATAFIRST=GHR1,LSIT=115-42-SPECT,DEBLOC,BRAND=FALSE;
DATAFIRST=GHR1,LSIT=115-50-SPECT,DEBLOC,BRAND=FALSE;

I cant figured out how to do that any help will be appreciated .

thanks
 
Hi seifou45,

I tried this

seifou45.awk
Code:
[COLOR=#0000ff]# run:[/color]
[COLOR=#0000ff]# awk -f seifou45.awk seifou45.txt[/color]

[COLOR=#6a5acd]NR[/color] > [COLOR=#ff00ff]1[/color] [COLOR=#6a5acd]&&[/color] [COLOR=#6a5acd]$1[/color] [COLOR=#6a5acd]~[/color] [COLOR=#ff00ff]/DATA/[/color] {
  data_nr [COLOR=#6a5acd]=[/color] [COLOR=#6a5acd]NR[/color]
  [COLOR=#a52a2a][b]next[/b][/color]
}

[COLOR=#6a5acd]NR[/color] [COLOR=#6a5acd]==[/color] data_nr [COLOR=#6a5acd]+[/color] [COLOR=#ff00ff]1[/color] {
  beg_line [COLOR=#6a5acd]=[/color] [COLOR=#ff00ff]"DATAFIRST="[/color][COLOR=#6a5acd]$1[/color]
  [COLOR=#a52a2a][b]next[/b][/color]
}

[COLOR=#6a5acd]$1[/color] [COLOR=#6a5acd]~[/color] [COLOR=#ff00ff]/BRAND/[/color] {
  brand_nr [COLOR=#6a5acd]=[/color] [COLOR=#6a5acd]NR[/color]  
  [COLOR=#a52a2a][b]next[/b][/color]
}

[COLOR=#6a5acd]NR[/color] [COLOR=#6a5acd]==[/color] brand_nr [COLOR=#6a5acd]+[/color] [COLOR=#ff00ff]1[/color] {
  [COLOR=#a52a2a][b]if[/b][/color] ([COLOR=#6a5acd]$1[/color] [COLOR=#6a5acd]~[/color] [COLOR=#ff00ff]/TRUE/[/color]) {
    end_line [COLOR=#6a5acd]=[/color] [COLOR=#ff00ff]"BLOC,BRAND="[/color][COLOR=#6a5acd]$1[/color][COLOR=#ff00ff]";"[/color]
  }
  [COLOR=#a52a2a][b]if[/b][/color] ([COLOR=#6a5acd]$1[/color] [COLOR=#6a5acd]~[/color] [COLOR=#ff00ff]/FALSE/[/color]) {
    [COLOR=#a52a2a][b]print[/b][/color] [COLOR=#ff00ff]""[/color] 
    end_line [COLOR=#6a5acd]=[/color] [COLOR=#ff00ff]"DEBLOC,BRAND="[/color][COLOR=#6a5acd]$1[/color][COLOR=#ff00ff]";"[/color]
  }
  [COLOR=#a52a2a][b]next[/b][/color]
}

[COLOR=#6a5acd]$1[/color] [COLOR=#6a5acd]==[/color] [COLOR=#ff00ff]"LST"[/color] [COLOR=#6a5acd]||[/color] [COLOR=#6a5acd]$1[/color] [COLOR=#6a5acd]==[/color] [COLOR=#ff00ff]"LIST"[/color] {
  list_nr [COLOR=#6a5acd]=[/color] [COLOR=#6a5acd]NR[/color]
  [COLOR=#a52a2a][b]next[/b][/color]
}

[COLOR=#6a5acd]NR[/color] > list_nr [COLOR=#6a5acd]&&[/color] [COLOR=#6a5acd]$1[/color] [COLOR=#6a5acd]![/color][COLOR=#6a5acd]~[/color] [COLOR=#ff00ff]/END/[/color] {
  [COLOR=#a52a2a][b]if[/b][/color] ([COLOR=#6a5acd]$1[/color]  [COLOR=#6a5acd]==[/color] [COLOR=#ff00ff]""[/color]) {
    [COLOR=#a52a2a][b]next[/b][/color]
  }
  [COLOR=#a52a2a][b]else[/b][/color] {
    middle_line [COLOR=#6a5acd]=[/color] [COLOR=#ff00ff]"LSIT="[/color][COLOR=#6a5acd]$1[/color]
    [COLOR=#a52a2a][b]print[/b][/color] beg_line[COLOR=#ff00ff]","[/color]middle_line[COLOR=#ff00ff]","[/color]end_line
  }
}

and got this result with data file you provided above:

Code:
$ awk -f seifou45.awk seifou45.txt
DATAFIRST=GHR1,LSIT=115-34-SPECT,BLOC,BRAND=TRUE;
DATAFIRST=GHR1,LSIT=115-42-SPECT,BLOC,BRAND=TRUE;
DATAFIRST=GHR1,LSIT=115-50-SPECT,BLOC,BRAND=TRUE;

DATAFIRST=GHR1,LSIT=115-34-SPECT,DEBLOC,BRAND=FALSE;
DATAFIRST=GHR1,LSIT=115-42-SPECT,DEBLOC,BRAND=FALSE;
DATAFIRST=GHR1,LSIT=115-50-SPECT,DEBLOC,BRAND=FALSE;
 
hi Mikrom:

sorry for my late response I was sick .

the scipt works fine thanks a lot of .

just last thing can you explain please the script .

for example :

data_nr it is a variable assigned to NR number of recored and in the next block NR == data_nr+1 new assignement with commparison incremented ?

thanks
 
Hi seifou45,

The blocks are executed only when the condition fits,
for example:

This block
Code:
NR > 1 && $1 ~ /DATA/ {
  data_nr = NR
  next
}
will be executed only from the second line, when column #1 contains string "DATA"

This block will be executed only on the line which follows the line which contains the string "DATA"
Code:
NR == data_nr + 1 {
  beg_line = "DATAFIRST="$1
  next
}

To understand how it works insert some print statements and look at an awk tutorial or manual
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top