awk: don't print sub-arrays


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk: don't print sub-arrays
# 1  
Old 07-30-2012
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 08:49 AM..
# 2  
Old 07-30-2012
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.
# 3  
Old 07-30-2012
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
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.
This User Gave Thanks to neutronscott For This Post:
# 5  
Old 07-31-2012
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
This User Gave Thanks to alister For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using arrays with awk

I'm a little stuck and would be grateful of some advice! I have three files, two of which contain reference data that I want to add to a line of output in the third file. I can't seem to get awk to print array contents as I would expect. The input files are: # Input file AAA,OAA,0313... (2 Replies)
Discussion started by: maccas17
2 Replies

2. Shell Programming and Scripting

Compare strings between 2 arrays and print number in AWK

Hi to everyone, Please some help over here. Hi have array a with 6 elements and array b with 3 elements as shown inside BEGIN{} statement. I need help to get the correct sintax (the part in red) to compare if string from array b is in array a and print the number related for each match.... (3 Replies)
Discussion started by: Ophiuchus
3 Replies

3. Shell Programming and Scripting

help in awk arrays!

Hi, buddies I am new to shell scripting and trying to solve a problem. I read about arrays in awk that they are quite powerful and are associative in nature. Awk Gurus Please help! I have a file: Id1 pp1 0t4 pp8 xy2 Id43 009y black Id6 red xy2 Id12 new pp1 black I have... (5 Replies)
Discussion started by: geli21
5 Replies

4. Programming

std::cout and gfortran print*, don't output to the screen

I am not sure where to post this other than here. I am trying to figure out why an app gives different output when compiled under Ubuntu 10.10 and CentOS 5.5. I am pretty sure that the issue is that the Cent version has gcc 4.1 installed, while Ubuntu has gcc 4.4. I am trying to print from some... (20 Replies)
Discussion started by: LMHmedchem
20 Replies

5. UNIX for Dummies Questions & Answers

Help to know the various possible uses of awk arrays

Hi to all, I write this time to ask for different syntax or ways of arrays within awk and uses. Since I don't know how actually could work and the real potencial of awk arrays, I look for a proactive reply/help in order to get more information than it seems I'm trying to get. I think I... (2 Replies)
Discussion started by: cgkmal
2 Replies

6. HP-UX

awk don't work in hp-ux 11.11

Hello all! I have problem in hp-ux 11.11 in awk I want to grep sar -d 2 1 only 3 column, but have error in awk in hp-ux 11.11 Example: #echo 123 234 | awk '{print $2}' 123 234 The situattions in commands bdf | awk {print $5}' some... In hp-ux 11.31 - OK! How resolve problem (15 Replies)
Discussion started by: ostapv
15 Replies

7. Shell Programming and Scripting

awk arrays can do this better - but how?

Hi, I have spent the afternoon trawling Google, Unix.com and Unix in a Nutshell for information on how awk arrays work, and I'm not really getting too far. I ahve a batch of code that I am pretty sure can be better managed using awk, but I'm not sure how to use awk arrays to do what I'm... (1 Reply)
Discussion started by: littleIdiot
1 Replies

8. Shell Programming and Scripting

awk arrays

Guys, OK so i have been trying figure this all all day, i guess its a pretty easy way to do it. Right, so i have to column of data which i have gotten from one huge piece of data. What i would like to do is to put both of these into one array using awk. Is this possible?? If so could... (1 Reply)
Discussion started by: imonthejazz
1 Replies

9. Shell Programming and Scripting

arrays in awk???

Been struggling with a problem, I have been trying to do this in awk, but am unable to figure this out, I think arrays have to be used, but unsure how to accomplish this. I have a input file that looks like this: 141;ny;y;g 789;ct;e;e 23;ny;n;u 45;nj;e;u 216;ny;y;u 7;ny;e;e 1456;ny;e;g... (3 Replies)
Discussion started by: craigsky
3 Replies

10. Programming

may be simple but i don't know -- Print current date from C program

How to print current date of the Unix system accessing thru C++ program ? I wrote like this #include <time.h> ....... time_t tt; struct tm *tod; .... time(&tt); tod = localtime(&tt); cout << tod->tm_mon + 1 << "/" << tod->tm_mday << "/" ... (6 Replies)
Discussion started by: ls1429
6 Replies
Login or Register to Ask a Question