LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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

Reply
 
Search this Thread
Old 05-15-2012, 12:47 PM   #1
udiubu
Member
 
Registered: Oct 2011
Posts: 54

Rep: Reputation: Disabled
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
 
Old 05-15-2012, 01:06 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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.
Old 05-15-2012, 01:09 PM   #3
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 3,038

Rep: Reputation: 1057Reputation: 1057Reputation: 1057Reputation: 1057Reputation: 1057Reputation: 1057Reputation: 1057Reputation: 1057
Edit: colucix beat me to it
 
Old 05-15-2012, 01:16 PM   #4
udiubu
Member
 
Registered: Oct 2011
Posts: 54

Original Poster
Rep: Reputation: Disabled
WOW COLUCIX thanks so much!

If you don't mind could you please explain:

printf "%02d"

Thanks again, it works!

Udiubu
 
Old 05-15-2012, 01:18 PM   #5
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 3,038

Rep: Reputation: 1057Reputation: 1057Reputation: 1057Reputation: 1057Reputation: 1057Reputation: 1057Reputation: 1057Reputation: 1057
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.
Old 05-15-2012, 01:22 PM   #6
udiubu
Member
 
Registered: Oct 2011
Posts: 54

Original Poster
Rep: Reputation: Disabled
Great! Thanks!
 
  


Reply

Tags
recursion, strings


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
sed - Problem appending at the end of line jdom Linux - Newbie 1 11-05-2010 01:21 AM
Appending a string to columns in a file raghu123 Programming 2 08-29-2008 01:19 AM
Search a string recursively under one directory say_hi_ravi Programming 6 07-09-2008 04:47 AM
How to count the number of files recursively with wild card redir Linux - Newbie 5 02-28-2006 11:25 AM
Appending a string to a file JStew Linux - General 13 03-04-2003 03:48 PM


All times are GMT -5. The time now is 04:26 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration