Visit Our UNIX and Linux User Community


I'm having problems with a simple for loop on a newline


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers I'm having problems with a simple for loop on a newline
# 1  
Old 10-02-2009
I'm having problems with a simple for loop on a newline

Code:
for i in `seq 1 10 ` ; do
        printf $i '\n';
done

gives me this:
Code:
1234567891064mbarch ~ $

(output followed by bash prompt) Smilie

I've tried so many ways to create a newline at the end. Does anyone have any ideas.. Thanks in advance. Sorry
# 2  
Old 10-02-2009
Hi.

Try this usage of printf:

Code:
printf "%d\n" $i

# 3  
Old 10-02-2009
Quote:
Originally Posted by scottn
Hi.

Try this usage of printf:

Code:
printf "%d\n" $i

That didn't do what I wanted. That gave the same effect as using:

Code:
for i in `seq 1 10 ` ; do
        echo $i;
done

I want
Code:
64mbarch ~ $
12345678910

# 4  
Old 10-02-2009
You want something like this:

Code:
for i in $(seq 10); do
    printf '%d' "$i"
    [ "$i" -eq 10 ] && printf '\n'
done

Or rather:

Code:
echo  $(seq 10)

... and with some shells even:
Code:
echo {1..10}

# 5  
Old 10-02-2009
Hi.

Sorry, misread your post.

Put an echo after the done...

Code:
for i in `seq 1 10 ` ; do
        printf $i
done
echo


Last edited by Scott; 10-02-2009 at 12:03 PM..
# 6  
Old 10-02-2009
Quote:
Originally Posted by radoulov
You want something like this:

Code:
for i in $(seq 10); do
    printf '%d' "$i"
    [ "$i" -eq 10 ] && printf '\n'
done

Or rather:

Code:
echo  $(seq 10)

... and with some shells even:
Code:
echo {1..10}

Thanks, that's what I was looking for.

---------- Post updated at 04:35 PM ---------- Previous update was at 03:59 PM ----------

Quote:
Originally Posted by scottn
Hi.

Sorry, misread your post.

Put an echo after the done...

Code:
for i in `seq 1 10 ` ; do
        printf $i
done
echo

That's ok. Sorry for not explaining my post well.

I prefer your solution because I understand it better. Thanks
# 7  
Old 10-03-2009
Quote:
Originally Posted by 64mb
Thanks, that's what I was looking for.

---------- Post updated at 04:35 PM ---------- Previous update was at 03:59 PM ----------



That's ok. Sorry for not explaining my post well.

I prefer your solution because I understand it better. Thanks
One more to go - why use printf?
Code:
for f in $(seq 1 10); do echo -n $f; done; echo

BR,

pen
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #897
Difficulty: Medium
BusyBox is a package containing multiple binaries.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Simple loop using for

Dear experts, I am writing a bash script. At some point of the program I need to have 'for' loop. For simplicity I tried with some other simple code. The format of the loop is given below. k=51 m=55 for j in {$k..$m};do w=$(($j+2)) z=$(($j+9)) echo "$w, $z" done But my... (4 Replies)
Discussion started by: vjramana
4 Replies

2. Shell Programming and Scripting

simple problems in awk

Dear All, I have the following awk script. #!/bin/bash sh stdev.cmd data.file | awk '{print $2}' > out.data read d < out.data echo $d awk '{print $1,$2- $f}' new > newz The script runs "stdev.cmd" and output a file "out.data" and the value of the... (2 Replies)
Discussion started by: Yacob_123
2 Replies

3. UNIX for Dummies Questions & Answers

Simple Problems to Solve!

Hi, I'm pretty poor at using UNIX but I'm learning. Please help me with these simple problems! Much appreciated! 1. I've changed my shell from bash to csh but I prefer bash. How do I change back? I've tried using chsh -s but it's not working! 2. I'm trying to download TopCat. I've done... (2 Replies)
Discussion started by: SimonWhite
2 Replies

4. Programming

fgets problems newline

hello, i'm trying to write a C-program that reads a file line by line. (and searches each line for a given string) This file is an special ASCII-database-file, with a lot of entries. I checked the line with most length, and it was about 4000 characters. With google i found several... (4 Replies)
Discussion started by: p1cm1n
4 Replies

5. Shell Programming and Scripting

Very Simple "Tabs to Newline" Code???

hi people; i have a text, iptables.txt and values are as follows inside: (seperators are TAB) 10.15.2.2 2 40 10.15.3.3 1 10 10.32.2.1 3 40 10.33.6.7 2 50 10.16.3.2 1 60 10.11.3.9 1 20 10.19.2.1 1 80 10.16.9.9 3 20 10.29.7.9 2 90 10.33.2.7 3 40 ...... i... (5 Replies)
Discussion started by: gc_sw
5 Replies

6. Shell Programming and Scripting

Problems with simple script in cygwin

Hello! I have somo problems with simple scripts like this: #!/bin/bash echo -n "Enter your name and press : " read var_name echo "Your name is: $var_name" When I try to run it, this error occurs: ':not a valid identifier var_name. Why?? (I work in cygiwin) Is there anybody out... (10 Replies)
Discussion started by: blianna
10 Replies

7. UNIX for Dummies Questions & Answers

Simple loop

I need to chmod a bunch of files with a specific extension in one directory. If I understand correctly first I would run ls command like this ls -R | grep .mp3 > /tmp/list once I have the output file I should be able to run a loop to chmod all the files in the list created. This is where... (5 Replies)
Discussion started by: eugenes18t
5 Replies

8. Shell Programming and Scripting

Problems using join for simple database lookup

I am trying to get a script working that will perform a simple database lookup using the join command. Here are the two files that I am trying to join: % cat lookup1.txt Number_1 Other_data_a Number_5 Other_data_b Number_8 Other_data_c Number_10 Other_data_d % cat... (2 Replies)
Discussion started by: JasonHamm
2 Replies

9. Shell Programming and Scripting

Simple using For loop

Hi expert, I'm using csh Code: #!/bin/csh set x = 0 set number = `awk '{array=$0} END {print array;}'` i want to use for loop to store data to $number repeatly untill x = 23 How to use c shell for loop? (2 Replies)
Discussion started by: vincyoxy
2 Replies

10. Shell Programming and Scripting

simple for loop

i have the following process running in background: when i give "ps -lef" ------------------------------------------------------------------------ user2 user1 user1 user3 user1 user4 user5 user4 user3 user4 user2 user1 user1 user3 user1 user4 (3 Replies)
Discussion started by: ali560045
3 Replies

Featured Tech Videos