PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > splitting words into individual characters
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

splitting words into individual characters

Réponse
 
LinkBack Outils de la discussion
Vieux 03/11/2006, 23h49   #1
Mitch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut splitting words into individual characters

Hello again.

I'm still learning, but a lot of it I am finding through reading the
groups and by asking Google nicely, however I have yet to find an answer
to this one.

I am trying to break a word down into individual characters. OK that's
not true, I'm trying to break down the file permissions into individual
sections.

I've got ls -l filename | awk ' { print $1 } ' which works (I worked
this out myself, so if there is an easier/better way of getting it, I'd
appreciate it...) Its giving me what I think I want (-rw-r--r--) but I
am trying to figure out how to break it down into individual letters. I
have seen examples of "case in (?r*)" format but this would leave me
with a lot of variations to consider when I am trying to translate the
file permission string into a pretty table.

so my question is, how can I change a word (-rw-r--r-- or equiv) into a
list/array of individual characters.

Thanks for your time.

Mitch.
  Réponse avec citation
Vieux 04/11/2006, 00h06   #2
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: splitting words into individual characters

Mitch wrote:
> Hello again.
>
> I'm still learning, but a lot of it I am finding through reading the
> groups and by asking Google nicely, however I have yet to find an answer
> to this one.
>
> I am trying to break a word down into individual characters. OK that's
> not true, I'm trying to break down the file permissions into individual
> sections.
>
> I've got ls -l filename | awk ' { print $1 } ' which works (I worked
> this out myself, so if there is an easier/better way of getting it, I'd
> appreciate it...) Its giving me what I think I want (-rw-r--r--) but I
> am trying to figure out how to break it down into individual letters. I
> have seen examples of "case in (?r*)" format but this would leave me
> with a lot of variations to consider when I am trying to translate the
> file permission string into a pretty table.
>
> so my question is, how can I change a word (-rw-r--r-- or equiv) into a
> list/array of individual characters.
>
> Thanks for your time.
>
> Mitch.


If you want to stay with awk have a look at this program...

ls -l | awk 'NR>1 {split($1,a,""); for(i=1;i<=10;i++) print a[i]}'

If you just want to process a single file then omit the condition...

ls -l filename | awk '{split($1,a,""); for(i=1;i<=10;i++) print a[i]}'


Janis
  Réponse avec citation
Vieux 04/11/2006, 00h14   #3
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: splitting words into individual characters

On 2006-11-03, Mitch wrote:
> Hello again.
>
> I'm still learning, but a lot of it I am finding through reading the
> groups and by asking Google nicely, however I have yet to find an answer
> to this one.
>
> I am trying to break a word down into individual characters. OK that's
> not true, I'm trying to break down the file permissions into individual
> sections.
>
> I've got ls -l filename | awk ' { print $1 } ' which works (I worked
> this out myself, so if there is an easier/better way of getting it, I'd
> appreciate it...) Its giving me what I think I want (-rw-r--r--) but I
> am trying to figure out how to break it down into individual letters. I
> have seen examples of "case in (?r*)" format but this would leave me
> with a lot of variations to consider when I am trying to translate the
> file permission string into a pretty table.
>
> so my question is, how can I change a word (-rw-r--r-- or equiv) into a
> list/array of individual characters.


set -- $( ls -l "$filename" | sed -e 's/ .*//' -e 's/./& /g' )


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
  Réponse avec citation
Vieux 04/11/2006, 17h39   #4
Mitch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: splitting words into individual characters

Janis Papanagnou wrote:
> Mitch wrote:
>> Hello again.
>>
>> I'm still learning, but a lot of it I am finding through reading the
>> groups and by asking Google nicely, however I have yet to find an
>> answer to this one.
>>
>> I am trying to break a word down into individual characters. OK
>> that's not true, I'm trying to break down the file permissions into
>> individual sections.
>>
>> I've got ls -l filename | awk ' { print $1 } ' which works (I worked
>> this out myself, so if there is an easier/better way of getting it,
>> I'd appreciate it...) Its giving me what I think I want (-rw-r--r--)
>> but I am trying to figure out how to break it down into individual
>> letters. I have seen examples of "case in (?r*)" format but this
>> would leave me with a lot of variations to consider when I am trying
>> to translate the file permission string into a pretty table.
>>
>> so my question is, how can I change a word (-rw-r--r-- or equiv) into
>> a list/array of individual characters.
>>
>> Thanks for your time.
>>
>> Mitch.

>
> If you want to stay with awk have a look at this program...
>
> ls -l | awk 'NR>1 {split($1,a,""); for(i=1;i<=10;i++) print a[i]}'
>
> If you just want to process a single file then omit the condition...
>
> ls -l filename | awk '{split($1,a,""); for(i=1;i<=10;i++) print a[i]}'
>
>
> Janis


Thank you, that worked perfectly. It forced me to learn a bit more awk
than I had, but that's no bad thing. Cheers!
  Réponse avec citation
Vieux 04/11/2006, 17h48   #5
Mitch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: splitting words into individual characters

Chris F.A. Johnson wrote:
> On 2006-11-03, Mitch wrote:
>> Hello again.
>>
>> I'm still learning, but a lot of it I am finding through reading the
>> groups and by asking Google nicely, however I have yet to find an answer
>> to this one.
>>
>> I am trying to break a word down into individual characters. OK that's
>> not true, I'm trying to break down the file permissions into individual
>> sections.
>>
>> I've got ls -l filename | awk ' { print $1 } ' which works (I worked
>> this out myself, so if there is an easier/better way of getting it, I'd
>> appreciate it...) Its giving me what I think I want (-rw-r--r--) but I
>> am trying to figure out how to break it down into individual letters. I
>> have seen examples of "case in (?r*)" format but this would leave me
>> with a lot of variations to consider when I am trying to translate the
>> file permission string into a pretty table.
>>
>> so my question is, how can I change a word (-rw-r--r-- or equiv) into a
>> list/array of individual characters.

>
> set -- $( ls -l "$filename" | sed -e 's/ .*//' -e 's/./& /g' )
>
>


Thanks, though I haven't had a chance to look at sed past simple
s/old/new yet. I know what -e does but have yet to read up on the
special characters. I know that & matches the found sample... I tried to
get this to work but just had no luck with it as is. the set --, does
this mean something I haven't come across yet because it gives errors if
I stick it in the command line. in fact if I include anything other than
ls -l "$filename" | sed -e 's/ .*//' -e 's/./& /g' I get errors. That
said, without the other bits works a treat,so thank you. I was just
curious why the outer parts were added.

Thanks!

Mitch.
  Réponse avec citation
Vieux 04/11/2006, 19h41   #6
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: splitting words into individual characters

On 2006-11-04, Mitch wrote:
> Chris F.A. Johnson wrote:
>> On 2006-11-03, Mitch wrote:
>>> Hello again.
>>>
>>> I'm still learning, but a lot of it I am finding through reading the
>>> groups and by asking Google nicely, however I have yet to find an answer
>>> to this one.
>>>
>>> I am trying to break a word down into individual characters. OK that's
>>> not true, I'm trying to break down the file permissions into individual
>>> sections.
>>>
>>> I've got ls -l filename | awk ' { print $1 } ' which works (I worked
>>> this out myself, so if there is an easier/better way of getting it, I'd
>>> appreciate it...) Its giving me what I think I want (-rw-r--r--) but I
>>> am trying to figure out how to break it down into individual letters. I
>>> have seen examples of "case in (?r*)" format but this would leave me
>>> with a lot of variations to consider when I am trying to translate the
>>> file permission string into a pretty table.
>>>
>>> so my question is, how can I change a word (-rw-r--r-- or equiv) into a
>>> list/array of individual characters.

>>
>> set -- $( ls -l "$filename" | sed -e 's/ .*//' -e 's/./& /g' )

>
> Thanks, though I haven't had a chance to look at sed past simple
> s/old/new yet. I know what -e does but have yet to read up on the
> special characters. I know that & matches the found sample...


The other special characters are '.' and '*'; the dot matches any
single character, and the asterisk matches zero or more of the
preceding character.

> I tried to get this to work but just had no luck with it as is.


What does "no luck" mean? Please be specific.

> the set --, does this mean something I haven't come across yet
> because it gives errors if I stick it in the command line.


What errors?

> in fact if I include anything other than ls -l "$filename" | sed -e
> 's/ .*//' -e 's/./& /g' I get errors.


Are you using [t]csh? If so, Bourne-shell syntax will not work.

> That said, without the other bits works a treat,so thank you. I was
> just curious why the outer parts were added.


The script places each permissions character in a positional
parameter, so that they can be referred to by number:
$1, $2, ... ${10}.

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
  Réponse avec citation
Vieux 04/11/2006, 19h50   #7
Mitch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: splitting words into individual characters

Mitch wrote:
> Janis Papanagnou wrote:
>> Mitch wrote:
>>> Hello again.

[...]
>>> Mitch.

[...]
>> If you just want to process a single file then omit the condition...
>>
>> ls -l filename | awk '{split($1,a,""); for(i=1;i<=10;i++) print a[i]}'
>>
>>
>> Janis

>
> Thank you, that worked perfectly. It forced me to learn a bit more awk
> than I had, but that's no bad thing. Cheers!


Hopefully a quick one...

This prints the array, but with a newline after each character. What I
was hoping for, and have tried extensively is below:

ls -l | awk '{split($1,a,""); for(i=2;i<=10;i++) print -n a[i]}'

There are only 2 changes, the first is the for loop, I'm starting from 2
as I'm not interested currently in the file/directory bit. The second
and important change is the -n argument to print. I would like all the
characters separated by a space, or tab, but am having serious trouble
with those characters in sed or awk. do you know how I might do this?
I'm not near enough to understanding the other (sed) suggestion by Chris
yet to be able to use it (although it works the way i want it to). My
version of awk is GNU awk 3.1.0 and sed is GNU sed 3.02

putting the -n option after print i though removed the newline, but
instead it adds a zero to the beginning of every line. I thought i
could use sed s/ to replace the newlines with tabs or spaces, but it
appears that this is virtually impossible as well.

Any ideas?

Thanks again!
  Réponse avec citation
Vieux 04/11/2006, 19h55   #8
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: splitting words into individual characters

Mitch wrote:
> Mitch wrote:
>
>> Janis Papanagnou wrote:
>>
>>> Mitch wrote:
>>>
>>>> Hello again.

>
> [...]
>
>>>> Mitch.

>
> [...]
>
>>> If you just want to process a single file then omit the condition...
>>>
>>> ls -l filename | awk '{split($1,a,""); for(i=1;i<=10;i++) print a[i]}'
>>>
>>>
>>> Janis

>>
>>
>> Thank you, that worked perfectly. It forced me to learn a bit more
>> awk than I had, but that's no bad thing. Cheers!

>
>
> Hopefully a quick one...
>
> This prints the array, but with a newline after each character. What I
> was hoping for, and have tried extensively is below:
>
> ls -l | awk '{split($1,a,""); for(i=2;i<=10;i++) print -n a[i]}'
>
> There are only 2 changes, the first is the for loop, I'm starting from 2
> as I'm not interested currently in the file/directory bit. The second
> and important change is the -n argument to print. I would like all the
> characters separated by a space, or tab, but am having serious trouble
> with those characters in sed or awk. do you know how I might do this?
> I'm not near enough to understanding the other (sed) suggestion by Chris
> yet to be able to use it (although it works the way i want it to). My
> version of awk is GNU awk 3.1.0 and sed is GNU sed 3.02
>
> putting the -n option after print i though removed the newline, but
> instead it adds a zero to the beginning of every line. I thought i
> could use sed s/ to replace the newlines with tabs or spaces, but it
> appears that this is virtually impossible as well.
>
> Any ideas?


In awk there are no "options to commands", thus no '-n'. Use printf,
instead (as in the C language)...

printf("%s ", a[i]);

....or whatever format specifier is best for your needs.

Janis

>
> Thanks again!

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h24.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,19743 seconds with 16 queries