increment numbers in several parts of a string


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers increment numbers in several parts of a string
# 1  
Old 08-28-2010
increment numbers in several parts of a string

I know how to do produce this:

Code:
string01
string02
string03

several different ways.

But how do I do produce this (without getting lost in recursion):

Code:
string01morestring100yetmore10
string02morestring101yetmore20
string03morestring102yetmore30
...
string10morestring109yetmore100

I would like to be able to do this without using:

bash
ksh
zsh
perl
jot

I presume there is a way, or several, to do this using only base programs such as sh, printf, seq, etc. or perhaps a single function in the C std libs.

Update: Right after posting, I thought of this:

Code:
for i in $(seq 1 10);
do printf "string$(expr 00 + $i)morestring$(expr 99 + $i)yetmore$(expr 10 + $i "*" 10)\n";
done

It works!

(Note the quotes around the "*" meta-character.)

Last edited by uiop44; 08-28-2010 at 05:49 AM..
# 2  
Old 08-28-2010
Alternatively:
Code:
for i in $(seq 1 10)
do 
  printf "string0${i}morestring10$((i-1))yetmore${i}0\n"
done

# 3  
Old 08-28-2010
Quote:
Originally Posted by Scrutinizer
Alternatively:
Code:
for i in $(seq 1 10)
do 
  printf "string0${i}morestring10$((i-1))yetmore${i}0\n"
done

Excellent! I've learnt something new! Cheers! Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get first four parts of the string?

I have the string: XXXX.YYYY_ZZZ.20180724.01.txt I need to get rid of .txt and get full four parts XXXX.YYYY_ZZZ.20180724.01 I did: CTL=`echo XXXX.YYYY_ZZZ.20180724.01.txt | rev | cut -d"." -f4 | rev` But got only YYYY_ZZZ What should I do to get all four parts of that... (4 Replies)
Discussion started by: digioleg54
4 Replies

2. Shell Programming and Scripting

String increment in UNIX

Hi, I am able to increment numbers but unable to increment the charters in unix -AIX. Source : AAA BB CCC Increment Number : 5 OUTPUT: AAA BB CCC AAA BB CCD AAA BB CCE AAA BB CCF AAA BB CCG Thanks onesuri Please use CODE tags as required by the forum rules. I have made a wild... (5 Replies)
Discussion started by: onesuri
5 Replies

3. Shell Programming and Scripting

How to increment a string variable?

Hi All, I am new to this forum and a novice at shell script. I am trying to write a script to determine each of the NIC configured on a linux system and its speed and Duplex. I came up with the following piece of code: echo `ifconfig -a | grep eth > /home/a/nic.txt` i=`awk -F, '{print... (4 Replies)
Discussion started by: pravin883
4 Replies

4. Shell Programming and Scripting

How to cut the particular string and increment the rows?

Hi am using unix aix I have tried using awk but am getting only output = x ,its not incrementing next output set -A var1 vv qa za ct=0 i=3 while do var1=`echo ${var1}` count=`awk ' NR==$i++ {print;exit}' ${.txt} | cut -c5 ` echo $count let ct=ct+1 done (6 Replies)
Discussion started by: Venkatesh1
6 Replies

5. Shell Programming and Scripting

Increment Numbers in File

Hello, I have a text file withe some records 20121031|5 20121030|3 20121029|1 20121028|4 20121027|6 I want to search for a patten with '20121030' and then increment the second part of the delimiter i.e. 3 by 1 to make it 4 to look like 20121031|5 20121030|4 20121029|1 20121028|4... (7 Replies)
Discussion started by: pparthiv
7 Replies

6. Shell Programming and Scripting

perl, splitting out specific parts of the string

Hi there, I have an output from a command like this # ypcat -k netgroup.byuser| grep steven steven.* users_main,users_sysadmin,users_global,users_backup_team and wanted to pull the 'users' netgroups returned into a perl array, that will look like this users_main... (2 Replies)
Discussion started by: rethink
2 Replies

7. Shell Programming and Scripting

Spliting bash string into parts

Hello, let's say I have this string: string1="A\nB\nC D E\nFG\nH"; How can I split it so as to take every string separated with '\n' separately? For example, as for $string1, it would be split into string1_part1="A" string1_part2="B" string1_part3="C D E" string1_part4="FG"... (5 Replies)
Discussion started by: hakermania
5 Replies

8. UNIX for Dummies Questions & Answers

How to cut a string in two parts and show the other part

hi everybody.. I have a string like : abcd:efgh xxyy:yyxx ssddf:kjlioi ghtyu:jkksk nhjkk:heuiiue please tell me how i can display only the characters after ":" in the output the output should be : efgh yyxx kjlioi jkksk heuiiue please give quick reply.. its urgent..!! (6 Replies)
Discussion started by: adityamitra
6 Replies

9. Shell Programming and Scripting

Truncate Specific Parts of a String

How do you truncate specific parts of a string. Example: 1 This is the string Goal: This is the string As you can see I'm trying to simply remove the first two characters of the string the number one and the space between the one and the word "this." Your help is appreciated. ... (8 Replies)
Discussion started by: royarellano
8 Replies

10. Shell Programming and Scripting

A problem for sed? Remove parts of a string

Hi, My knowledge about sed is limited but I have a problem that I think can be solved with sed. I have a variable in a shell script that stores a lot of path/filenames and the delimitter between them is a space (they all exist on the same line). One part of the filename is the file creation... (4 Replies)
Discussion started by: pcrs
4 Replies
Login or Register to Ask a Question