sed, awk, perl 
Author Message
 sed, awk, perl

i want to check if in an input file there
is indented code. For eg.

clear : var1,
        var2,  "notice the vert. alignment of var*
        var3.

I want to ensure if all the v's are vertically
alligned or report the line no. where the
vertical allignment has not been met.

is it possible to check this easily in
sed,awk or perl ? or shell for that matter ?

The beginning of the check would be the
appearance of a colon on a line and
The termination condition for the check would
be the appearance of a full stop on any succeeding
line.

Could someone please help me out ?

regards,
Darshan



Sat, 24 Sep 2005 12:31:41 GMT  
 sed, awk, perl
Are you going to have to handle tabs as wells blanks? It will make the
problem much harder. The simple case would be something like:

/:/     { j = index($0,":")+1
          while(substr($0,j,1)=" ") j++\
          next
        }
        { if((substr($0,1,j-1)!=substr(blanks,1,j))||(substr($0,j,1)=" ")) {
            print "Indentation failure at line " NR
          }
        }

You could replace the print with code to fix the alignment instead of
reporting the errors. Handling tabs is much messier and very prone to
off-by-one errors; if you're on *ix or can install cygwin, I recommend
using untab as a preprocessing step and, if necessary, the tab command
as a postprocessing step.

Quote:

> i want to check if in an input file there
> is indented code. For eg.

> clear : var1,
>         var2,  "notice the vert. alignment of var*
>         var3.

> I want to ensure if all the v's are vertically
> alligned or report the line no. where the
> vertical allignment has not been met.

> is it possible to check this easily in
> sed,awk or perl ? or shell for that matter ?

> The beginning of the check would be the
> appearance of a colon on a line and
> The termination condition for the check would
> be the appearance of a full stop on any succeeding
> line.

> Could someone please help me out ?

> regards,
> Darshan



Sat, 24 Sep 2005 21:38:57 GMT  
 sed, awk, perl
Robert,

Thanks for your prompt reply. No, the input file will never have
tabs so i don't think i will have to go into the mess. Thanks for
the caution anyway.

Regards,
Darshan.

Quote:

> Are you going to have to handle tabs as wells blanks? It will make the
> problem much harder. The simple case would be something like:

> /:/        { j = index($0,":")+1
>      while(substr($0,j,1)=" ") j++\
>      next
>    }
>    { if((substr($0,1,j-1)!=substr(blanks,1,j))||(substr($0,j,1)=" ")) {
>        print "Indentation failure at line " NR
>      }
>    }

> You could replace the print with code to fix the alignment instead of
> reporting the errors. Handling tabs is much messier and very prone to
> off-by-one errors; if you're on *ix or can install cygwin, I recommend
> using untab as a preprocessing step and, if necessary, the tab command
> as a postprocessing step.



Sun, 25 Sep 2005 14:51:34 GMT  
 sed, awk, perl

Quote:

> i want to check if in an input file there
> is indented code. For eg.

> clear : var1,
>         var2,  "notice the vert. alignment of var*
>         var3.

> I want to ensure if all the v's are vertically
> alligned or report the line no. where the
> vertical allignment has not been met.

indent {
        match($0, / */)
        if (RLENGTH != indent) {
                print "Incorrect indentation on row " NR " by " \
                       RLENGTH - indent ":"
                print
        }
Quote:
}

indent && /\.$/ { indent = 0 }
!indent && /:/ {
        match($0, /: */)
        indent = RSTART + RLENGTH - 1

Quote:
}

I made the assumption that indentations cannot be nested, and that all
indentation consists of spaces (ie no tabs).

Bj?rn



Sun, 25 Sep 2005 19:36:31 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. How to get info from a file with sed/awk/perl

2. extracting top 100 data using awk or sed/perl

3. Perl versus sed + awk + ksh

4. Tcl as a replacement for perl/sed command line work

5. Newbie awk (sed??) question, regular expressions

6. Awk/Sed Filehandler question

7. Extracting hyphenated words using sed/awk

8. Need help with sed or awk !!

9. A very simple question on SED or AWK for a GURU, and an enjoyable problem

10. SED to AWK...???

11. suggestion of sed or awk

12. Run awk from within sed script?

 

 
Powered by phpBB® Forum Software