LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-30-2007, 04:38 AM   #1
baidym
LQ Newbie
 
Registered: Oct 2007
Posts: 16

Rep: Reputation: 0
csh, perl, python??


Hi,

New to scripting so please take pitty on me.

I'm trying to create a script that will output a string of comma separated numbers to the command line when given the first and last number. e.g.
% <scriptname> 12345 12350
% 12345,12346,12347,12348,12349,12350

I've been trying to do it with bash and perl but to no avail. Can anyone help?

Cheers

Michelle.
 
Old 10-30-2007, 04:54 AM   #2
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 38
bash:
Code:
$ (IFS=,;set -- {12345..12350};printf "%s\n" "$*")
12345,12346,12347,12348,12349,12350
Edit: you need zsh (or bashdiff, IIRC) if you want to
use parameters inside the braces:
Code:
zsh-4.3.4% start=12345
zsh-4.3.4% end=12350
zsh-4.3.4% IFS=,;set -- {$start..$end};printf "%s\n" "$*"
12345,12346,12347,12348,12349,12350

Last edited by radoulov; 10-30-2007 at 05:00 AM.
 
Old 10-30-2007, 05:15 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Code:
for num in `seq $1 $2`
do
    echo $num
done
 
Old 10-30-2007, 06:27 AM   #4
arub
LQ Newbie
 
Registered: Sep 2007
Location: Coimbatore , India
Distribution: Fedora
Posts: 16
Blog Entries: 2

Rep: Reputation: 0
Simply by using python script

here simple script in python


print_num.py
-------------------------------------------------
#!/usr/bin/python
import sys
print range(int(sys.argv[1]),int(sys.argv[2])+1)
---------------------------------------------------

Result

$python print_num.py 3 11
[3, 4, 5, 6, 7, 8, 9, 10, 11]
 
Old 10-30-2007, 08:50 AM   #5
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

Some version of seq can do it directly:
Code:
#!/usr/bin/env sh

# @(#) s1       Demonstrate sequence of comma-separated integers with seq.

set -o nounset
echo

debug=":"
debug="echo"

## Use local command version for the commands in this demonstration.

echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version bash seq

echo

seq -s, 12345 12350

exit 0
producing:
Code:
% ./s1

(Versions displayed with local utility "version")
GNU bash 2.05b.0
seq (coreutils) 5.2.1

12345,12346,12347,12348,12349,1235
cheers, makyo
 
Old 10-30-2007, 09:03 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
in bash
Code:
# echo {12345..12350}
12345 12346 12347 12348 12349 12350
 
Old 10-30-2007, 09:15 AM   #7
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.
Quote:
Originally Posted by radoulov View Post
...
Edit: you need zsh (or bashdiff, IIRC) if you want to
use parameters inside the braces:
...
I like the shell solution. Adding a spin with eval makes it work with bash3:
Code:
#!/usr/bin/env bash3

# @(#) s2       Demonstrate feature.

set -o nounset
echo

debug=":"
debug="echo"

## Use local command version for the commands in this demonstration.

echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version bash3

echo

echo " bash3 with constants:"
(IFS=,;set -- {12345..12350};printf "%s\n" "$*")

# Original
# start=12345
# end=12350
# IFS=,;set -- {$start..$end};printf "%s\n" "$*"

start=12345
end=12350
echo
echo " bash3 with variables:"
IFS=,;set -- {$start..$end};printf "%s\n" "$*"

echo
echo " bash3 variables with eval:"
IFS=,;eval set -- {$start..$end};printf "%s\n" "$*"

exit 0
Producing:
Code:
% ./s2

(Versions displayed with local utility "version")
GNU bash 3.00.16(1)-release

 bash3 with constants:
12345,12346,12347,12348,12349,12350

 bash3 with variables:
{12345..12350}

 bash3 variables with eval:
12345,12346,12347,12348,12349,12350
cheers, makyo
 
Old 10-30-2007, 09:18 AM   #8
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.
Quote:
Originally Posted by ghostdog74 View Post
in bash
Code:
# echo {12345..12350}
12345 12346 12347 12348 12349 12350
The OP wanted "comma separated" (OTOH, the title says csh, yet he specifically mentioned bash) ... cheers, makyo

Last edited by makyo; 10-30-2007 at 09:21 AM.
 
Old 10-30-2007, 09:42 AM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by makyo View Post
Hi.

The OP wanted "comma separated" (OTOH, the title says csh, yet he specifically mentioned bash) ... cheers, makyo
yes i know. that's just trivial output formatting. just showing how it can be done in bash. btw, so is post #3.
 
Old 10-30-2007, 11:54 AM   #10
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/
 
Old 10-30-2007, 01:29 PM   #11
paddy3118
LQ Newbie
 
Registered: Jul 2007
Location: Bristol UK
Distribution: RedHat, Cygwin
Posts: 14

Rep: Reputation: 0
Post

A little Python throwaway script.
Pythoins range does not include the upper bound so I add one, and I use backticks to change ints to strings:

Code:
bash$ python -c 'print ",".join(`i` for i in range(12345,12351))'
12345,12346,12347,12348,12349,12350
bash$
 
Old 10-30-2007, 01:54 PM   #12
rsashok
Member
 
Registered: Nov 2006
Location: USA, CA
Distribution: RedHat, Debian
Posts: 202

Rep: Reputation: 31
#!/bin/bash
echo {12345..12350} | tr ' ' ','
 
Old 10-30-2007, 02:16 PM   #13
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Since python is already taken, and no one did perl yet.
Code:
$ perl -le 'print join ",", 12345..12350'
12345,12346,12347,12348,12349,12350
 
Old 10-31-2007, 12:35 AM   #14
paddy3118
LQ Newbie
 
Registered: Jul 2007
Location: Bristol UK
Distribution: RedHat, Cygwin
Posts: 14

Rep: Reputation: 0
Post

...And here is a solution in awk:
Code:
bash$ awk 'BEGIN{OFS=",";n=1;for(i=12345;i<=12350;i++){$n=i;n++}print $0;exit}'
12345,12346,12347,12348,12349,12350
bash$
I set the output field separator to a comma then stuff $1...$N with the appropriate values and rely on awk concatenating things to create $0 for printing.
 
Old 10-31-2007, 12:54 AM   #15
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by paddy3118 View Post
...And here is a solution in awk:
Code:
bash$ awk 'BEGIN{OFS=",";n=1;for(i=12345;i<=12350;i++){$n=i;n++}print $0;exit}'
12345,12346,12347,12348,12349,12350
bash$
I set the output field separator to a comma then stuff $1...$N with the appropriate values and rely on awk concatenating things to create $0 for printing.
another way, except there's extra "," at the end.
Code:
awk 'BEGIN{s=12345;n=12350}
     s<n{ printf s++"," }' "file"
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl and Python raghuveerbabu Programming 10 08-24-2007 08:45 AM
Python or Perl drdroid Programming 29 12-22-2006 09:54 AM
perl vs python yenonn Programming 6 08-01-2006 05:44 AM
Perl or Python JJX Programming 6 10-27-2004 03:58 AM
newgrp command within perl/csh script bobsey Programming 1 04-13-2001 10:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration