|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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! |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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! |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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! |
|
![]() |
| Outils de la discussion | |
|
|