|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
awk: don't print sub-arrays
Hi ! I have this input: Code:
12{11}{11110}{80}3456
{123}15{60}9876{8083}34I 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})%34My 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])
}
}
}
}1I 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
|
||||
|
||||
|
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' infileCould 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
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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 < fileRegards, Alister |
| The Following User Says Thank You to alister For This Useful Post: | ||
beca123456 (08-10-2012) | ||
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|