Find the answer to your Linux question:
Results 1 to 2 of 2
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Algorithm using AWK, SED or your own recommendations...

    Dear All,

    First of all I'm sorry if my presentation of the problem is a little complicated.

    The following is a simple part of a postscript file:

    %%Pages: (atend)
    %%Title: scs2ps
    %%BoundingBox: 0 0 612 792
    %%LanguageLevel: 2

    %%BeginResource: procset general 1.0.0
    %%Title: (General Procedures)
    %%Version: 1.0
    /s { % x y (string)
    3 1 roll
    } def

    %%Page: 1 1
    /pgsave save def
    /Courier [4.09 0 0 10.91 0 0] selectfont
    40.09 690.55 (M) s
    44.18 690.55 (R) s
    48.27 690.55 (.) s
    56.45 690.55 (A) s
    60.55 690.55 (L) s
    64.64 690.55 (W) s
    68.73 690.55 (A) s
    72.82 690.55 (L) s
    76.91 690.55 (I) s
    81.00 690.55 (D) s
    89.18 690.55 (S) s
    93.27 690.55 (A) s
    97.36 690.55 (I) s
    101.45 690.55 (F) s
    109.64 690.55 (E) s
    113.73 690.55 (L) s
    117.82 690.55 (D) s
    121.91 690.55 (I) s
    126.00 690.55 (N) s
    134.18 690.55 (A) s
    138.27 690.55 (B) s
    pgsave restore
    %%Pages: 1

    The output is "MR. ALWALID SAIF ELDIN AB", the space between each character and the next to it is 4.09 and the space between each word and a new word is 8.18.

    I want to shift this line a little to the left to start at 50.09 instaed of 40.09.

    The difference between the "M" and the "R" is "44.18-40.09=4.09"
    The difference between a word and another like the "MR." and "ALWALI" is "56.45-48.27=8.18"

    So I want to make a simple operation to know whether the next character will be the start of a new word or not.

    I know how to do this using `sed` but I don't know how to shift the rest of the characters based on the space between each character and the other. Another solution using `AWK` is:

    awk '{print $1+10 " " $2 " " $3 " " $4}' /tmp/fic2

    But the problem of AWK is that the file is not of the same structure from the beginning.

    Can you provide a better algorithm or another tool?

    Thank you for your kind help in advance.

  2. #2
    Linux Guru
    Join Date
    Mar 2003
    Use Perl, this exactly the type of thing it's best at. start a loop at some trigger, say something like %%EndPageSetup, then have it test for what you want and add the appropriate amount. Do the loop until some end trigger like pgsave restore.

    When sed and awk aren't enough, go with perl.
    Good Luck,

    Registered Linux user #346571
    "All The Dude ever wanted was his rug back" - The Dude

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts