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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rearange the text using AWK/NAWK 1

Status
Not open for further replies.

budimanonly

Technical User
May 22, 2005
10
SG
Dear All,

Can somebody help me please I have a file like this

H'000000:H'0000)H'8432,H'9220,H'0000,H'FFF5:H'8432,H'9220,H'0000,H'FFF9:
H'000002:H'0000)H'8432,H'9220,H'1000,H'FFF1:H'8432,H'9220,H'1000,H'FFF2:
H'000004:H'0000)H'8432,H'9220,H'1000,H'FFF4:H'8432,H'9220,H'1000,H'FFF6:
H'000006:H'0000)H'8432,H'9220,H'1000,H'FFF7:H'8432,H'9220,H'1000,H'FFF8:
H'000008:H'0000)H'8432,H'9220,H'1000,H'FFF9:H'8432,H'9220,H'3000,H'FFF2:
H'00000A:H'0000)H'8432,H'9220,H'3000,H'FFF3:H'8432,H'9220,H'3000,H'FFF5:
END

And I want the output like this :
2348022900005FFF
2348022900009FFF
2348022900011FFF
2348022900012FFF
2348022900014FFF
2348022900016FFF
2348022900017FFF
2348022900018FFF
2348022900019FFF
2348022900032FFF
2348022900033FFF
2348022900035FFF


Thanks and regards,

Budiman

 
pls describe the transformation rules.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Hi,
Each line record will give 2 output line result.
eq.
this is the first line record :
H'000000:H'0000)H'8432,H'9220,H'0000,H'FFF5:H'8432,H'9220,H'0000,H'FFF9:

the result will be like this :
2348022900005FFF
2348022900009FFF

transformation like this
H'8432 transform to 2348
H'9220 transform to 0229
H'0000 transform to 0000

 
continued..
H'5FFF transform to 5FFFF

and so on..

BR,

Budiman
 
you need to define a function for reversing strings

function rev ( a ) {
b=""
for (i=length(a);i>0;i--) {
b=sprintf("%s%c", b , substr(a,i,1))
}
return b
}

then use in your awk program (as an example)

printf "%s\n", rev(substr($1, 19, 4))

to convert 8432 into 2348

you take it from here.

HTH,

p5wizard
 
Dear p5wizard,

I've try the function reversing but its error (unexpexted "{")

Any idea?

BR,

Budiman
 
Make sure that you are not using an obsolete version of Awk. If you have nawk, use it instead of awk because on some systems awk is very old and lacks many useful features. Under Solaris, use /usr/xpg4/bin/awk.
Code:
BEGIN { FS = "[,:]" ; ORS = "" }

/^END/ { exit }

{ gsub( /^.*\)/, "" )
  gsub( /[H']/, "" )
  for (i=1; i<9; i++)
  { print rev( $i )
    if ( 4 == i )
      print "\n"
  }
  print "\n"
}

function rev( s,    i,result )
{ for (i=length(s); i>0; i--)
    result = result substr( s, i, 1 )
  return result
}
 
One seldom sees recursion in Awk. Let's give it a shot.
Code:
function rev( s, result )
{ if ( length( s ) )
    return rev(substr(s,2),substr(s,1,1) result)
  else
    return result
}

BEGIN { FS = ",|:" ; ORS = "" }

/^END/ { exit }

{ gsub( /^.*\)/, "" )
  gsub( /[H']/, "" )
  for (i=1; i<9; i++)
  { print rev( $i )
    if ( 4 == i || 8 == i )
      print "\n"
  }
}
 
Dear sirs,

Thanks for the inputs, its really help me and I appreciate it.

BR,

Budiman
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top