Creating number by pattern matching


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating number by pattern matching
# 1  
Old 04-20-2012
Creating number by pattern matching

I have a certain mnemonic string from which I want to calculate a number

The pattern follows three letters s, v and d. If a letter is by its own, the number assigned to the letter is assumed to be one. Else it takes the value preceeding it. I then need to add the numbers together.

Example
Code:
s       n=1
svd     n=3
2svd    n=4
3svd    n=5
2s2vd   n=5

---------- Post updated at 11:18 AM ---------- Previous update was at 10:58 AM ----------

I have started by separating the numbers from the letters, and storing things in an array.

Code:
lastArg="$#"
 
result=`echo "${!lastArg}" | sed 's/\([0-9][0-9.]*\)/ & /g'`
echo "result = $result"
array=( "$result" )

# 2  
Old 04-20-2012
Hi kristinu,

Using a perl regexp:
Code:
$ cat infile 
s      
svd   
2svd 
3svd
2s2vd
$ perl -lne 's/(\d*)\w(?{ our $total += $1 || 1 })/$&/g; printf qq[%s\t%d\n], $_, $total; $total = 0' infile 
s       1
svd     3
2svd    4
3svd    5
2s2vd   5

# 3  
Old 04-20-2012
Forgot to say that I am doing this in bash.


I have done this and seems to work. The sed command will separate the number from the characters.



Code:
str="12svd"
result=`echo "${!str}" | sed 's/\([0-9][0-9.]*\)/ & /g'`
array=( $result )
 
n=0
for str in "${array[@]}"; do
   echo "str = $str"
   if [[ "$str" =~ ^[0-9]+$ ]]; then
     k=$str
        fi
        if [ "$str" == "svd" ]; then
            k=2
        elif [ "$str" == "sv" ] || [ "$str" == "vd" ] || [ "$str" == "sd" ]; then
     k=1
        fi
   n=$((n+k))
done

---------- Post updated at 01:21 PM ---------- Previous update was at 12:27 PM ----------

How can I introduce a 1 if the letter does not have a number in front of it?

Example
Code:
change svd to 1s1v1d
change 2svd to 2s1v1d
change 2sv to 2s1v


Last edited by kristinu; 04-20-2012 at 02:52 PM..
# 4  
Old 04-21-2012
You can use the 't' command of sed:
Code:
sed ':lab; s/\([^0-9]\)\([a-z]\)/\11\2/; t lab'

After successful substitution, it sends the flow back to the label :lab.
Think of it as "goto"
# 5  
Old 04-27-2012
When I use the command below

Code:
echo "svd" | sed ':lab; s/\([^0-9]\)\([a-z]\)/\11\2/; t lab'

I get

Code:
s1v1d

However I want to get

Code:
1s1v1d

# 6  
Old 04-27-2012
I see. You could take care of the leading letter case in a separate command:
Code:
sed -e 's/^\([a-z]\)/1\1/' -e   ':lab; s/\([^0-9]\)\([a-z]\)/\11\2/; t lab'

Or, perhaps more readable:
Code:
sed -e '/^[a-z]/ s/^/1/' -e   ':lab; s/\([^0-9]\)\([a-z]\)/\11\2/; t lab'

This User Gave Thanks to mirni For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove matching pattern on each line with number variations

Hello folks! I have a file containing lines like this Something text 18:37Remove This: 1,111"Keep this text" Some more text 19:37Remove This: 222"Keep this text" More text 20:50Remove This: 3,333Keep this text And more text 25:50Remove This: 44,444Keep this text I would like to... (4 Replies)
Discussion started by: martinsmith
4 Replies

2. Shell Programming and Scripting

Pattern Matching and creating output

HI Unix Forum, My requirement I have two set of Patterns UBA and CIE for which different Phases are there which will have Start and End time. They are not in same order. I want the o/p in the below mentioned format. Eg: Mangolia Alien 03:04:56 Phase 0 started (10... (5 Replies)
Discussion started by: TechGyaann
5 Replies

3. UNIX for Dummies Questions & Answers

Grep -v lines starting with pattern 1 and not matching pattern 2

Hi all! Thanks for taking the time to view this! I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern. Example: Drink a soda Eat a banana Eat multiple bananas Drink an apple juice Eat an apple Eat multiple apples I... (8 Replies)
Discussion started by: demmel
8 Replies

4. Shell Programming and Scripting

PHP - Regex for matching string containing pattern but without pattern itself

The sample file: dept1: user1,user2,user3 dept2: user4,user5,user6 dept3: user7,user8,user9 I want to match by '/^dept2.*/' but don't want to have substring 'dept2:' in output. How to compose such regex? (8 Replies)
Discussion started by: urello
8 Replies

5. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

6. UNIX for Dummies Questions & Answers

Find pattern suffix matching pattern

Hi, I am trying to get a result out of this but fails please help. Have two files /tmp/1 & /tmp/hosts. /tmp/1 IP=123.456.789.01 WAS_HOSTNAME=abcdefgh.was.tb.dsdc /tmp/hosts 123.456.789.01 I want this result in /tmp/hosts if hostname is already there dont want duplicate entry. ... (5 Replies)
Discussion started by: rajeshwebspere
5 Replies

7. Shell Programming and Scripting

Creating single pattern for matching multiple files.

Hi friends, I have a some files in a directory. for example 856-abc 856-def 851-abc 945-def 956-abc 852-abc i want to display only those files whose name starts with 856* 945* and 851* using a single pattern. i.e 856-abc 856-def 851-abc 945-def the rest of the two files... (2 Replies)
Discussion started by: Little
2 Replies

8. Shell Programming and Scripting

sed to replace the matching pattern with equal number of spaces

Hi I have written a shell script which used sed code below sed -i 's/'"$Pattern"'/ /g' $FileName I want to count the length of Pattern and replace it with equal number of spaces in the FileName. I have used $(#pattern) to get the length but could not understand how to replace... (8 Replies)
Discussion started by: rakeshkumar
8 Replies

9. Shell Programming and Scripting

counting the lines matching a pattern, in between two pattern, and generate a tab

Hi all, I'm looking for some help. I have a file (very long) that is organized like below: >Cluster 0 0 283nt, >01_FRYJ6ZM12HMXZS... at +/99% 1 279nt, >01_FRYJ6ZM12HN12A... at +/99% 2 281nt, >01_FRYJ6ZM12HM4TS... at +/99% 3 283nt, >01_FRYJ6ZM12HM946... at +/99% 4 279nt,... (4 Replies)
Discussion started by: d.chauliac
4 Replies

10. Shell Programming and Scripting

comment/delete a particular pattern starting from second line of the matching pattern

Hi, I have file 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433... (4 Replies)
Discussion started by: imas
4 Replies
Login or Register to Ask a Question