String handling

String handling

Post by Loki Harfag » Sat, 09 Apr 2005 23:09:41



Le Thu, 07 Apr 2005 18:09:11 -0700, Colin a crit?:

Quote:> I'm trying to count the number of leading spaces in lines from a file using:

> tfc=0
> w=$0
> for(i=1;i<=NF;i++) if (w[i] == ' ') tfc++ else break}

> but this generates a syntax error

> What am I doing wrong ??

Now you know that :-)

Quote:> Any help ...

Then, another help/way of, just to be exhaustive
and for the pleasure to have fun with the toolbox :-)

$ sed 's/[^ ].*//' testfile|tr -d '\n' |wc -c

Well, it'd *on non LF-terminating files of course ...

 
 
 

String handling

Post by Michael Tos » Mon, 11 Apr 2005 00:17:36



> Le Thu, 07 Apr 2005 18:09:11 -0700, Colin a crit?:

> > I'm trying to count the number of leading spaces in lines from a file using:

> > tfc=0
> > w=$0
> > for(i=1;i<=NF;i++) if (w[i] == ' ') tfc++ else break}

Is this an awk script?

Quote:

> > but this generates a syntax error

> > What am I doing wrong ??

> Now you know that :-)

> > Any help ...

> Then, another help/way of, just to be exhaustive
> and for the pleasure to have fun with the toolbox :-)

> $ sed 's/[^ ].*//' testfile|tr -d '\n' |wc -c

With awk this becomes:

awk '{sum+=match($0"x","[^ ]")-1}END{print sum}' testfile

and can be stripped down to print the number per line:

awk '{print match($0"x","[^ ]")-1}' testfile

--


 
 
 

1. String handling in sh


Change your awk command to look something like this:
    awk '$1 ~ /dsk50|dsk53|dsk29/{print $1}'

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    Cal Dunigan                       My opinions *ARE* the company's

    Consulting, Modeling, Training
//////////////////////////////////////////////////////////////////////

2. DOSEMU from crontab

3. DBCS/MBCS aware string handling functions

4. window managers/desktop environments

5. String handling on VAC compiler (version 5) on AIX

6. mksysb & dd

7. C string handling question

8. A Linux word processor? (was Re: Linux vs. Windows 95: why

9. in 'getopt()', how to handle multiple options in the 'optarg' string?

10. sparse tool handling of escaped characters in strings

11. Q: Can dip handle complex strings ?

12. Q: ksh - how to handle strings with spaces

13. Find a string in a text file and replace with another string to eof