LinuxQuestions.org
Help answer threads with 0 replies.
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 11-16-2007, 02:48 AM   #1
som_kurian
Member
 
Registered: Oct 2007
Posts: 33

Rep: Reputation: 15
Patterns


Hi guys

I want to get the output of shell script as


1
12
123
1234
12345
123456
........


what I did is

for (( a=1; a <= 9; a++ ))
do
for (( b=a; b <= a; b++ ))
do
echo :$b\n"
done
done


But I am not getting the desired output
 
Old 11-16-2007, 04:13 AM   #2
Rawoof
LQ Newbie
 
Registered: Oct 2007
Posts: 1

Rep: Reputation: 0
Quote:
Originally Posted by som_kurian View Post
Hi guys

I want to get the output of shell script as


1
12
123
1234
12345
123456
........


what I did is

for (( a=1; a <= 9; a++ ))
do
for (( b=a; b <= a; b++ ))
do
echo :$b\n"
done
done


But I am not getting the desired output
IN the second for loop variable b must be assigned 1 instead of a to get
the required output
 
Old 11-16-2007, 07:54 AM   #3
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 17
Posts: 150

Rep: Reputation: 18
Wouldn't one loop do the trick?

Code:
for i in $(seq 1 9)
do
 j=${j}${i}
 echo ${j}
done
 
Old 11-18-2007, 08:28 AM   #4
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
If the ending digit is fixed, have a look at brace expansion (find it in man bash):
Code:
for i in {1..6}
do
 j=${j}${i}
 echo ${j}
done
It's more compact, & possibly faster, than $(seq 1 6).

Using seq & tr inside the loop can eliminate a line from it:
Code:
for X in {1..6}
do echo $(seq 1 $X)  | tr -d ' '
done
If you're going to seq in the loop definition, you might as well get the benefit of variable limits:
Code:
n=6
for X in $(seq 1 $n)
do echo $(seq 1 $X)  | tr -d ' '
done
 
Old 11-18-2007, 10:03 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk 'BEGIN {
    a=1
    print 1
    for(i=2;i<=6;i++){
        printf "%s%s\n", a,i
        a=a i
    }
}'
output:
Code:
#./test.sh
1
12
123
1234
12345
123456
 
Old 11-23-2007, 03:20 AM   #6
som_kurian
Member
 
Registered: Oct 2007
Posts: 33

Original Poster
Rep: Reputation: 15
can anyone reply what is wrong in this program


for (( a=1; a <= 9; a++ ))
do
for (( b=1; b <= a; b++ ))
do
echo "$b\n"
done
done

The output I am getting is
1\n
1\n
2\n
1\n
2\n
3\n
1\n
2\n
3\n
4\n

 
Old 11-23-2007, 03:25 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
echo on bash doesn't do "\n"
you need echo -e
or, leave it off as it does one anyway,
or use printf


get in the habit of reading the manual
 
Old 11-23-2007, 04:21 AM   #8
som_kurian
Member
 
Registered: Oct 2007
Posts: 33

Original Poster
Rep: Reputation: 15
Thank you dear "bigearsbilly"...I understand...I will try some other way.Thanks for your advice
 
Old 11-24-2007, 07:33 AM   #9
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
RTM for echo -- the option you want is "-n'.

While I consider them too long & complex to be considered elegant, your nested loops can be easily fixed:
Code:
for (( a=1; a <= 9; a++ ))
do
  for (( b=1; b <= a; b++ ))
  do
     echo -n "$b"
  done
  echo
done

Timing
Just for fun, I checked the time used by the various techniques suggested using time. To get some to register a measurable time, I had to push n/a/i to 99.

The results surprised me: The hands down winner was:
Code:
for ((i=1;i<=99;i++))
do
   j=${j}${i}
   echo ${j} 
done
2nd:
Code:
for i in {1..99}
do
   j=${j}${i}
   echo ${j} 
done
3rd was my repaired ver. of the orig. code.

So much for "elegance" when production matters.
 
Old 12-05-2007, 11:37 PM   #10
som_kurian
Member
 
Registered: Oct 2007
Posts: 33

Original Poster
Rep: Reputation: 15
Thank you very much dear "archtoad6".......I was looking for this answer...Thank you once again for everyone who helped me...I got the output....YOU GUYS ROCK!!!!!!
 
Old 12-06-2007, 01:46 AM   #11
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Now the awk and shell script is out of the way, we can have some fun with Perl:
Code:
$ perl -e 'map {printf "%s\n",$s.=$_;} (1..6);'
 
Old 12-06-2007, 04:20 AM   #12
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
darn it, a bit longer matthew

Code:
perl -l012 -e 'foreach (1..6) { push @L, $_;print @L}'
 
Old 12-06-2007, 08:43 AM   #13
enemorales
Member
 
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Rep: Reputation: 31
Lol

I took the three scripts from archtoad6's post and run the same tests, but got different results. After several running and looking at the times, the three scripts were very close to each other (in my --slow-- machine, about 0.050 s).

Then, only for curiosity, I took one the scripts and changed it a "little":

Code:
for ((i=1;i<=99;i++))
do
   j=${j}${i}
   echo "echo \"${j}\""
done
to create a new script with goes like

Code:
echo "1"
echo "12"
echo "123"
...
Which turned to be slower (first 2 runs), but after the third one it was slightly faster ( about 0.040s), some caching probably helping...



 
Old 12-06-2007, 09:01 AM   #14
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
well, the perl thrashes the bash up to 99, but make it 999 and the perl loses.
I guess it is constructing a large list.
 
Old 06-29-2023, 02:04 PM   #15
punnu6789
LQ Newbie
 
Registered: Jun 2023
Posts: 1

Rep: Reputation: 1
Smile Solution to this problem with one loop

read n
for (( i=1 ; i<=$n ; i++ ))
do
seq -s " " $i
done
 
1 members found this post helpful.
  


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
grep for multiple patterns???? lucastic Linux - Software 4 08-06-2010 06:07 PM
Exclude certain patterns from grep? kinetik Linux - General 4 04-24-2006 05:37 AM
selecting patterns in file manjushp Programming 15 09-10-2005 08:43 AM
Remembering patterns and printing only those patterns using sed bernie82 Programming 5 05-26-2005 05:18 PM
Nautilus backgrounds/patterns jeickal Linux - Software 0 01-03-2005 03:26 AM

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

All times are GMT -5. The time now is 10:48 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