LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 01-15-2011, 10:18 PM   #1
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,746

Rep: Reputation: 256Reputation: 256Reputation: 256
Printf: giving "001 002 003 004 006.... 150" in a single line dash command?


Hello,

I am intended to simply make printf give "001 002 003 004 006.... 150" but with the single command line of SH (dash) ... not easy ...

well a non working solution..
Code:
tucholsky:~$ seq 1 1 150 | sed 's/\([0-9]\+\).*/\1/g' | tr '\n' ' '
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

-
For the geeks, which distro/version is it, based on the code above
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 01-15-2011, 10:54 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,038

Rep: Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203
Maybe something like:
Code:
for i in $(seq 1 1 150);do printf "%03d " $i;done
 
1 members found this post helpful.
Old 01-15-2011, 11:49 PM   #3
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by grail View Post
Maybe something like:
Code:
for i in $(seq 1 1 150);do printf "%03d " $i;done
Hi grail,

nice solution. It can be even shortened to just
Code:
printf "%03d " $(seq 1 150)
 
2 members found this post helpful.
Old 01-15-2011, 11:52 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,038

Rep: Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203
Cheers crts ... wasn't aware it would feed the items singularly to printf.
 
Old 01-16-2011, 12:23 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 683Reputation: 683Reputation: 683Reputation: 683Reputation: 683Reputation: 683
Does your version of seq have the -w and -s options? If so, using -w will give you the leading zeros. Using -s' ' will use spaces to separate numbers instead of newlines.
 
2 members found this post helpful.
Old 01-16-2011, 04:02 AM   #6
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,746

Original Poster
Rep: Reputation: 256Reputation: 256Reputation: 256
wow, thank you !! It is gorgeously short and effective I am sorry that I forgot such elementary thing with printf
 
Old 01-16-2011, 07:00 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,038

Rep: Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203
Once you have a solution please mark as SOLVED
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
How Can I Use An "if" statement on a Single Command Line? jagooch Linux - General 4 02-10-2009 11:32 PM
giving search-string(s) from a file to linux "find" command Fond_of_Opensource Linux - Newbie 3 02-02-2009 06:14 PM
A client has "NUM 1 FDX 001-001 08:02a" displaying on her screen Goslowsky Linux - Software 3 03-07-2006 04:43 PM
cdrecord command-line parentheses "(" ")" coolingtower Linux - Software 1 10-28-2005 11:49 AM
libusb:001:002 ? d1l2w3 Linux - Laptop and Netbook 1 01-24-2005 02:22 AM

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

All times are GMT -5. The time now is 09:05 PM.

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