Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-30-2012
Registered User
 
Join Date: Apr 2012
Posts: 58
Thanks: 30
Thanked 0 Times in 0 Posts
awk: don't print sub-arrays

Hi !

I have this input:

Code:
12{11}{11110}{80}3456
{123}15{60}9876{8083}34

I try to work on individual numbers between braces.
3 possible cases (here I used colours to be clearer only):
- there is no "0" among the characters between braces: so we don't touch anything.
- there is a "0" among characters between braces, and the number of characters between braces is >2 ("0" included): e.g. {1110} needs to be written as ({111})%
- there is a "0" among characters between braces, and the number of characters between braces = 2 ("0" included): e.g. {10} needs to be written as 1%

From the input above, I should obtain this output:

Code:
12{11}({1111})%8%3456
{123}156%9876({883})%34

My first attempt was with this code, but I get the same as input:

Code:
BEGIN{FS=OFS=""}

{
	a = split($0,b,"{")
	
	for(i=1; i<=a; i++){
	    c = split(b[i],d,"}")
	    
	    for(j=1; j<=c; j++){
	    	e = split(d[j],f,"")
	    	
	    	if(d[j] ~ /0/ && e == 2){
	    		gsub(/0/,"",d[j])
	    		gsub(/$/,"%",d[j])
	    	}
	    	
	    	else if(d[j] ~ /0/ && e > 2){
	    		gsub(/^/,"({",d[j])
	    		gsub(/$/,"})%",d[j])
	    		gsub(/0/,"",d[j])
	    	}
	    	
	    	else if(d[j] !~ /0/){
	    		gsub(/^/,"{",d[j])
	    		gsub(/$/,"}",d[j])
	    	}
	    }
	}
}1

I expect to make some modifications after but i would like to understand why it doesn't print anything first.

Thanks for your help !

Last edited by beca123456; 07-30-2012 at 07:49 AM..
Sponsored Links
    #2  
Old 07-30-2012
elixir_sinari's Avatar
Gotham Knight
 
Join Date: Mar 2012
Location: India
Posts: 1,372
Thanks: 87
Thanked 478 Times in 458 Posts
Try this:

Code:
awk '{
while(match($0,/\{[^}]*\}/))
{
 inbraces=substr($0,RSTART,RLENGTH)
 if(index(inbraces,"0"))
 {
  if(RLENGTH==4)
  {
   gsub(/0/,"",inbraces);sub(/\{/,"",inbraces);sub(/\}/,"",inbraces)
   inbraces=inbraces "%"
   $0=substr($0,1,RSTART-1) inbraces substr($0,RSTART+RLENGTH)
  }
  else if(RLENGTH>4)
  {
   gsub(/0/,"",inbraces)
   $0 = substr($0,1,RSTART-1) "(" inbraces ")%" substr($0,RSTART+RLENGTH)
  }
 }
 sub(/\{/,"~")
 sub(/\}/,"!")
}
gsub(/~/,"{")
gsub(/!/,"}")
}1' infile

Could not go through your program. You may try this. Go through it and if any problems, let me know. Have assumed that the tilde and bang characters will never occur in your input.
Sponsored Links
    #3  
Old 07-30-2012
Registered User
 
Join Date: Apr 2012
Posts: 58
Thanks: 30
Thanked 0 Times in 0 Posts
Thanks for your help Elixir !

Unfortunately it doesn't work, the output is the same as the input.

With the code I first posted, my question is how could I print after each if statements?
    #4  
Old 07-30-2012
neutronscott's Avatar
script kiddie
 
Join Date: Jun 2011
Location: Charleston, SC
Posts: 649
Thanks: 18
Thanked 189 Times in 179 Posts
You make modifications to variables, not $0. So your 1 prints $0, not those modifications. you'd need printf statements at the end of your for loop. for this, i'd use a while match on the substr, like elixir suggested.
The Following User Says Thank You to neutronscott For This Useful Post:
beca123456 (08-10-2012)
Sponsored Links
    #5  
Old 07-30-2012
alister alister is offline Forum Advisor  
Registered User
 
Join Date: Dec 2009
Posts: 2,601
Thanks: 123
Thanked 717 Times in 600 Posts
A much less efficient but not too complicated shell script alternative:

Code:
while IFS= read -r line; do
    printf '%s\n' "$line" |
    tr '{}' '\n\n' |
    sed 'n; /0/!s/.*/{&}/; /0/{/.../s/.*/({&})/; s/.*/&%/; s/0//g;}' |
    paste -sd '\0' -
done < file

Regards,
Alister
The Following User Says Thank You to alister For This Useful Post:
beca123456 (08-10-2012)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Compare strings between 2 arrays and print number in AWK Ophiuchus Shell Programming and Scripting 3 10-23-2011 12:03 AM
[Solved] remove all print jobs from a print queue omonoiatis9 UNIX for Advanced & Expert Users 2 07-23-2011 03:11 AM
question about int arrays and file pointer arrays omega666 Programming 1 03-16-2011 12:15 AM
print first few lines, then apply regex on a specific column to print results. kchinnam Shell Programming and Scripting 4 08-24-2010 03:24 PM
Print Problem in UNIX. Need to know the option to specify the print paper size ukarthik HP-UX 1 06-07-2007 09:35 AM



All times are GMT -4. The time now is 02:20 PM.