How to print in the new line? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

How to print in the new line?

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-13-2013
targetshell targetshell is offline
Registered User
 
Join Date: Jun 2013
Last Activity: 6 November 2013, 4:53 AM EST
Posts: 23
Thanks: 1
Thanked 0 Times in 0 Posts
How to print in the new line?

I have written below script for the output:


Code:
1234
123
12
1



Code:
" {
 read x
 y=$x
 i=1
 while [ $i -le $x ]
 do
 j=1
 while [ $j -le $y ]
 do
 if [ $i == 1 ]
 then
 print -n $j
 fi
 j=`expr $j + 1`
 done
 i=`expr $i + 1`
 y=`expr $y - 1`
 done
 }"

However, I am getting o/p horizontally: 1234123121

Can anyone please help?
Sponsored Links
    #2  
Old 09-13-2013
Jotne's Avatar
Jotne Jotne is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 22 August 2014, 7:56 AM EDT
Posts: 1,040
Thanks: 62
Thanked 216 Times in 204 Posts
What o/p do you expect? Example
Sponsored Links
    #3  
Old 09-13-2013
targetshell targetshell is offline
Registered User
 
Join Date: Jun 2013
Last Activity: 6 November 2013, 4:53 AM EST
Posts: 23
Thanks: 1
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Jotne View Post
What o/p do you expect? Example
Hi there,

I got O/p from the above script as:


Code:
1234123121

I want it to be as shown below:


Code:
1234
123
12
1

Plz advise !!!

Last edited by Scott; 09-13-2013 at 09:45 AM.. Reason: Code tags
    #4  
Old 09-13-2013
Jotne's Avatar
Jotne Jotne is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 22 August 2014, 7:56 AM EDT
Posts: 1,040
Thanks: 62
Thanked 216 Times in 204 Posts
Using awk

Code:
echo "4" | awk '{for (j=$0;j>=1;j--) {for (i=1;i<=j;i++) printf i; print ""}}'
1234
123
12
1

Sponsored Links
    #5  
Old 09-13-2013
apmcd47 apmcd47 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 29 July 2014, 8:57 AM EDT
Posts: 98
Thanks: 0
Thanked 10 Times in 10 Posts

Code:
{
   read x
   y=$x
   i=1
   while [ $i -le $x ]
   do
      j=1
      while [ $j -le $y ]
      do
         echo -n $j
         j=`expr $j + 1`
      done
      echo
      i=`expr $i + 1`
      y=`expr $y - 1`
   done
}

I have replaced the prints with echos and indented for reading purposes. YOu had an if construct which I assume you put in for debugging purposes.

Andrew
Sponsored Links
    #6  
Old 09-13-2013
targetshell targetshell is offline
Registered User
 
Join Date: Jun 2013
Last Activity: 6 November 2013, 4:53 AM EST
Posts: 23
Thanks: 1
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Jotne View Post
Using awk

Code:
echo "4" | awk '{for (j=$0;j>=1;j--) {for (i=1;i<=j;i++) printf i; print ""}}'
1234
123
12
1


Shell should take input from the user instead of assuming it to be "4"

---------- Post updated at 08:44 AM ---------- Previous update was at 08:41 AM ----------

Quote:
Originally Posted by apmcd47 View Post
Code:
{
   read x
   y=$x
   i=1
   while [ $i -le $x ]
   do
      j=1
      while [ $j -le $y ]
      do
         echo -n $j
         j=`expr $j + 1`
      done
      echo
      i=`expr $i + 1`
      y=`expr $y - 1`
   done
}

I have replaced the prints with echos and indented for reading purposes. YOu had an if construct which I assume you put in for debugging purposes.

Andrew

I should have quoted that I am working in KORN shell. I don't know how much implications it has on the syntax, but here is the O/P of your amended script.


Code:
3
-n 1
-n 2
-n 3

-n 1
-n 2

-n 1


Plz advise !

Last edited by Scott; 09-13-2013 at 09:46 AM.. Reason: Code tags
Sponsored Links
    #7  
Old 09-13-2013
apmcd47 apmcd47 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 29 July 2014, 8:57 AM EDT
Posts: 98
Thanks: 0
Thanked 10 Times in 10 Posts
Quote:
Originally Posted by targetshell View Post


I should have quoted that I am working in KORN shell. I don't know how much implications it has on the syntax, but here is the O/P of your amended script.


Code:
3
-n 1
-n 2
-n 3

-n 1
-n 2

-n 1

Plz advise !
It works for ksh93 on Linux for me. Try putting the prints back in instead of the echos. What system are you running this on?

Andrew
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
String search and print next all lines in one line until blank line jaydeep_sadaria Shell Programming and Scripting 7 10-01-2013 05:21 AM
Using regex's from file1, print line and line after matches in file2 pathunkathunk Shell Programming and Scripting 1 08-02-2013 03:28 AM
Print pipe separated list as line by line in Korn Shell polavan Shell Programming and Scripting 3 10-28-2011 11:42 AM
read file line by line print column wise rocking77 Shell Programming and Scripting 2 12-07-2010 07:02 AM
Compare multiple fields in file1 to file2 and print line and next line gillesc_mac Shell Programming and Scripting 7 03-16-2009 06:26 AM



All times are GMT -4. The time now is 10:54 AM.