LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 01:47 PM   #1
udiubu
Member
 
Registered: Oct 2011
Posts: 73

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, 02:06 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
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, 02:09 PM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

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

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, 02:18 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143
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, 02:22 PM   #6
udiubu
Member
 
Registered: Oct 2011
Posts: 73

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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:42 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