×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

parse text file

parse text file

parse text file

(OP)
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

RE: parse text file

Hi seifou45,

I tried this

seifou45.awk

CODE

# run:
# awk -f seifou45.awk seifou45.txt

NR > 1 && $1 ~ /DATA/ {
  data_nr = NR
  next
}

NR == data_nr + 1 {
  beg_line = "DATAFIRST="$1
  next
}

$1 ~ /BRAND/ {
  brand_nr = NR  
  next
}

NR == brand_nr + 1 {
  if ($1 ~ /TRUE/) {
    end_line = "BLOC,BRAND="$1";"
  }
  if ($1 ~ /FALSE/) {
    print "" 
    end_line = "DEBLOC,BRAND="$1";"
  }
  next
}

$1 == "LST" || $1 == "LIST" {
  list_nr = NR
  next
}

NR > list_nr && $1 !~ /END/ {
  if ($1  == "") {
    next
  }
  else {
    middle_line = "LSIT="$1
    print beg_line","middle_line","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; 

RE: parse text file

(OP)
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

RE: parse text file

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
https://www.tutorialspoint.com/awk/index.htm
https://www.gnu.org/software/gawk/manual/gawk.html

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close