views:

505

answers:

6

Say I want to iterate from letter A to letter Z in csh shell. How do I succinctly do that?

In bash I would do something like

for i in 'A B C ...Z'; do echo $i; done

The point is I don't want to write A through Z, I want something like

[A-Z]

Can you suggest a one line suggestion in AWK or Perl?

A: 
foreach i (`jot -c 26 65`)
    echo $i
end
Uh Clem
ONE liner please, that's 3 lines. so shell complains with foreach i ( `jot -c 26 65` ); echo $i; endjot: Command not found.foreach?
vehomzzz
You can simply hit enter after foreach var (...) and the shell will let you continue enter lines until you type end
Zed
I guess now I understand the question :D
Zed
jot first appeared in 4.2BSD. It may not be in non-BSD derived OSes.
Uh Clem
+4  A: 
perl -e 'print for "a" .. "z", "A" .. "Z", 0 .. 9'
Sinan Ünür
not what I am looking for. With above , you cannot iterate over each letter and do something with it , say mkdir, as you CANNOT access them individually.
vehomzzz
Why can't you iterate over them? The Perl range operator just returns a list. It's a list like any other list. Do whatever you like with it. Furthermore, you need to clarify your question for all the stuff you aren't telling us.
brian d foy
Because Andrei wants to iterate in the shell.
mobrule
+3  A: 

Perl

$,=" ";print +(A..Z)

or to use inside a shell:

for i in `perl -e '$,=" ";print +(A..Z)'` ; do echo $i ; done
mobrule
Nice... thought manually printing A through B is a bit simpler...
vehomzzz
Why do you have the parens around the range operator? Is there some odd shell interaction that I'm missing?
brian d foy
I prefer `perl -E'say for A..Z'`
Brad Gilbert
@bdf -- you're right. `print A..Z` also works
mobrule
A: 

How about:

for i in $(perl -e 'for my $i ("a".."z") { print "$i "; }'); do ...; done

... or am I misunderstanding what you're trying to accomplish?

(Admittedly the suggestion using $,=" " is even better than the explicit Perl for loop). However, I don't understand value of wrapping the range in +() punctuation. It works for me if I just use: print A..Z; ... though perl -we 'print a..z;' gives me a warning about unquoted strings, but the uppercase version doesn't. The statement print +(a..z) gives exactly the same warning ... and quoting any of these eliminates the warning. So what's the intent of the +(...)`? Is it just trying to force this into a list context?)

Jim Dennis
+1  A: 

Hope you have Ruby installed. ;) See this, plain command-line from the shell:

  1. using Ruby to iterate from A to Z and ask to print the letters:

    $ ruby -e ' "a".upto("z") {|letter| print letter}; print "\n"'
    
  2. iterate from A to Z and substitute the value obtained during the current iteration into a string, then print the string:

    $ ruby -e ' "a".upto("z") {|letter| puts "mkdir #{letter}"}'
    
    mkdir a
    mkdir b
    mkdir c
    mkdir d
    ...
    mkdir z
    
  3. use the output of the iteration from A to C as an argument to mkdir, in order to create 3 directories:

    $ mkdir $(ruby -e ' "a".upto("c") {|letter| puts "#{letter}"}')
    
    do a listing to see the results:
    $ ls -al
    drwxr-xr-x  2 iuliu users  4096 2009-10-07 00:09 a
    drwxr-xr-x  2 iuliu users  4096 2009-10-07 00:09 b
    drwxr-xr-x  2 iuliu users  4096 2009-10-07 00:09 c
    

Hope this helps a bit! ;)

regards, Iuliu

Iuliu Pascaru
A: 

nobody has gawk solution, therefore here's one

awk 'BEGIN{for(i=65;i<=90;i++) printf "%c", i}'