Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

creating lot of similar tuples in shell script

550 views
Skip to first unread message

smileso...@gmail.com

unread,
Jul 3, 2013, 12:16:57 PM7/3/13
to
Hi all,
I am new to Shell scripting. I want to write a script to create lot of entries as following pattern and create a file new1.txt with all the entries.

new1 (h1,u1,d1) (junk1,u2,d2) (junk2,u3,d3) .....(junk1000,u1000,d1000)

I am planning to write a recursion function to create the similar tuples. I dont know whether it is possible or not in shell script or is there a better way to write it? Can anybody help me writing a sample program for it? I tried it but could not succeed.

Note: First entry is a valid entry and second entries are invalid entries.

Regards
Pradeep

Janis Papanagnou

unread,
Jul 3, 2013, 12:24:43 PM7/3/13
to
Am 03.07.2013 18:16, schrieb smileso...@gmail.com:
> Hi all, I am new to Shell scripting. I want to write a script to
> create lot of entries as following pattern and create a file new1.txt
> with all the entries.
>
> new1 (h1,u1,d1) (junk1,u2,d2) (junk2,u3,d3)
> .....(junk1000,u1000,d1000)
>
> I am planning to write a recursion function to create the similar
> tuples. I dont know whether it is possible or not in shell script or
> is there a better way to write it? Can anybody help me writing a
> sample program for it? I tried it but could not succeed.

With modern shells (ksh, bash, zsh) you can create tuples that way,
for example...

$ echo {junk,u,d}{1..10}
junk1 junk2 junk3 junk4 junk5 junk6 junk7 junk8 junk9 junk10 u1 u2 u3 u4
u5 u6 u7 u8 u9 u10 d1 d2 d3 d4 d5 d6 d7 d8 d9 d10


Janis

Janis Papanagnou

unread,
Jul 3, 2013, 12:26:24 PM7/3/13
to
Am 03.07.2013 18:16, schrieb smileso...@gmail.com:
> Hi all, I am new to Shell scripting. I want to write a script to
> create lot of entries as following pattern and create a file new1.txt
> with all the entries.
>
> new1 (h1,u1,d1) (junk1,u2,d2) (junk2,u3,d3)
> .....(junk1000,u1000,d1000)
>
> I am planning to write a recursion function to create the similar
> tuples. I dont know whether it is possible or not in shell script or
> is there a better way to write it? Can anybody help me writing a
> sample program for it? I tried it but could not succeed.

With modern shells (ksh, bash, zsh) you can create tuples that way,
for example...

$ echo {junk,u,d}{1..10}
junk1 junk2 junk3 junk4 junk5 junk6 junk7 junk8 junk9 junk10 u1 u2 u3 u4
u5 u6 u7 u8 u9 u10 d1 d2 d3 d4 d5 d6 d7 d8 d9 d10


Janis

>

Dave Gibson

unread,
Jul 3, 2013, 12:50:03 PM7/3/13
to
smileso...@gmail.com wrote:
> Hi all,
> I am new to Shell scripting. I want to write a script to create lot
> of entries as following pattern and create a file new1.txt with all
> the entries.
>
> new1 (h1,u1,d1) (junk1,u2,d2) (junk2,u3,d3) .....(junk1000,u1000,d1000)

Assuming the final tuple should be (junk999,u1000,d1000).

{
printf 'new1 (h1,u1,d1)'
for n in {2..1000} ; do
printf ' (junk%d,u%d,d%d)' $(( $n - 1 )) $n $n
done
printf '\n'
} > new1.txt
0 new messages