Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
05-15-2012, 01:47 PM
|
#1
|
Member
Registered: Oct 2011
Posts: 73
Rep:
|
appending number at the end of string, recursively
Hi guys,
very simple recursion needed:
I have 48 strings, and I would like to append at the end of each string the numbers from 01 to 48. In the specific:
for i in aaa bbb ccc ddd ...(until string 48); do
echo "${i}_???"
done
What should I put in place of ??? in order to have:
aaa_01
bbb_02
ccc_03
...
or you have any other idea?
Any suggestion would be highly appreciated!
Thank you very much.
Sincerely,
Udiubu
|
|
|
05-15-2012, 02:06 PM
|
#2
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Code:
for i in aaa bbb ccc blah blah
do
((n++))
echo ${i}_$(printf "%02d" $n)
done
or
Code:
echo aaa bbb ccc blah blah | awk 'BEGIN{RS=" "}{printf "%s_%02d\n", $0, NR}'
|
|
1 members found this post helpful.
|
05-15-2012, 02:09 PM
|
#3
|
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573
|
Edit: colucix beat me to it
|
|
|
05-15-2012, 02:16 PM
|
#4
|
Member
Registered: Oct 2011
Posts: 73
Original Poster
Rep:
|
WOW COLUCIX thanks so much!
If you don't mind could you please explain:
printf "%02d"
Thanks again, it works!
Udiubu
|
|
|
05-15-2012, 02:18 PM
|
#5
|
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573
|
the %02d is a format code for the print, it forces it to print out the number in two digits, zero padding if necessary. Otherwise it would print "1", instead of "01".
|
|
1 members found this post helpful.
|
05-15-2012, 02:22 PM
|
#6
|
Member
Registered: Oct 2011
Posts: 73
Original Poster
Rep:
|
Great! Thanks!
|
|
|
All times are GMT -5. The time now is 12:42 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|