if 4th and 5th character of sting -ge 45 then add 1 to 3rd character


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users if 4th and 5th character of sting -ge 45 then add 1 to 3rd character
# 1  
Old 12-28-2009
if 4th and 5th character of sting -ge 45 then add 1 to 3rd character

I want to know how to, given a string like W87151WR71C, if the 4th and 5th character (in this case 15) are greater than 45, then to add 1 to the 3rd character (in this case 7) and assign the revised string the variable name MODSTRING.

Thanks in advance. This is ultimately to grab info from serial numbers, like production date and correspond machines with lease numbers.
# 2  
Old 12-28-2009
Tools What have you tried?

This starts the process...

Code:
$ echo W87151WR71C
W87151WR71C

$ echo W87151WR71C | cut -c4-5
15

$ testval=`echo W87151WR71C | cut -c4-5`

$ echo $testval
15

# 3  
Old 12-28-2009
Code:
ORIGSTRING=W87551WR71C
MODSTRING=`echo "$ORIGSTRING" | awk -v FS='' -v OFS='' '$4$5 > 45 {++$3; print}'`

NOTE: While splitting a record into characters when FS is null isn't part of the POSIX standard, it's an extension that's implemented in the two versions of AWK that i tested (nawk and gawk). If this doesn't work for you, perhaps your version of AWK doesn't support it.
# 4  
Old 12-28-2009
Code:
echo "W87461WR71C" | \
awk '{x=substr($0,3,1);if(substr($0,4,2) > 45) print substr($0,1,2)""++x""substr($0,4);else print}'


Last edited by shamrock; 12-28-2009 at 02:11 PM..
# 5  
Old 12-28-2009
JoeyG, I already got that far, but no farther..'

alister, looks good except I need it to echo origstring unaltered if the 4th and 5th < 45, your solution echos blank if the 4th and 5th characters arent greater than 45.
# 6  
Old 12-28-2009
Quote:
Originally Posted by glev2005
JoeyG, I already got that far, but no farther..'

alister, looks good except I need it to echo origstring unaltered if the 4th and 5th < 45, your solution echos blank if the 4th and 5th characters arent greater than 45.
To include printing of unaltered strings:

Code:
ORIGSTRING=W87551WR71C
MODSTRING=`echo "$ORIGSTRING" | awk -v FS='' -v OFS='' '$4$5 > 45 {++$3} {print}'`

# 7  
Old 12-28-2009
Tools Please show what you have so far...

Quote:
Originally Posted by glev2005
JoeyG, I already got that far, but no farther..'

alister, looks good except I need it to echo origstring unaltered if the 4th and 5th < 45, your solution echos blank if the 4th and 5th characters arent greater than 45
If you show what you have done, it makes it easier for others to assist and advise. I assumed you hadn't even gotten that far.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] Replace character in 3rd column and leave 1rst and last

Hello to all, I have the following text where columns are separated by spaces. I want to have the 3rd column separating 3 strings with 2 "_" in the format below: LeftSring_CentralString_RightString So, in 3rd column I want to replace all "_" with "-", except the first and last "_" The... (5 Replies)
Discussion started by: Ophiuchus
5 Replies

2. Shell Programming and Scripting

Function to add escape character before specified character

Hi , I am looking for a function which will do the following. 1. I have a variable which will hold few special chracter like SPECIAL_CHARS="& ;"2. I have an escape character. ESCAPE_CHAR="\"3. Now when I passed some string in the function it will return the same string but now it will... (8 Replies)
Discussion started by: Anupam_Halder
8 Replies

3. Shell Programming and Scripting

Grep the 5th and 6th position character of a word in a file

I am trying to find/grep the 5th and 6th position character (TX) of a word in a file. I tried to do grep "....TX" file The output gives me everything in the file with TX in it. I only need the output with the TX in the 5th and 6th position of the word. Any idea Example: test1 car... (5 Replies)
Discussion started by: e_mikey_2000
5 Replies

4. Shell Programming and Scripting

How to extract 3rd,4th and 5th element

Hi All! I trying to execute this perl script to extract a particular field in a text file, luckily it works. But, right now I would like to extract 3 more fields in the text file but couldnt get the right syntax on how to do it. command is: perl -pe '$_ = (split(//)) . "\n"' TestDoc.txt... (3 Replies)
Discussion started by: VicNetIT
3 Replies

5. Shell Programming and Scripting

Remove 3rd character from the end of a random-length string

Hi, I hope someone can share there scripting fu on my problem, I would like to delete the 3rd character from a random length of string starting from the end Example Output Hope you can help me.. Thanks in advance.. (3 Replies)
Discussion started by: jao_madn
3 Replies

6. Shell Programming and Scripting

Remove 3rd and onwards occurances of a character

I have a file with "@" in every line fdgfgddfg@sfsdfdsf@dfdfd@@vfdfsdfd@ sfsdfs@sdfd@gfhfgh@@ddffg@sdf@ srfgtfsdfs@sdfertrd@eergfhfgh@@ddffg@sdf@ wttrtrt@sdfd@sdfdf@@sdfdfds@@@ I need to remove all the fourth and onwards occurences of "@". so my final o/p would be fdgfgddfg@sfsdfdsf@dfdfd@... (7 Replies)
Discussion started by: Shivdatta
7 Replies

7. Shell Programming and Scripting

How to replace all after 4th character in line ?

How to replace all after 4 character at line? & convert to upper case ? google.com -> GOOG (2 Replies)
Discussion started by: Trump
2 Replies

8. Shell Programming and Scripting

how to change every 3rd character

hey champs, i have a files, whose contents are as follows, abcdefghijk lmnopqrstuv .............. .............. i want to replace every other 3rd character to some specified character. let here in this file i want to replace each 3rd character to z. abzdezghzjk lmzopzrszuv... (2 Replies)
Discussion started by: manas_ranjan
2 Replies

9. Shell Programming and Scripting

Remove 5th character from Field1 & Print

Hi , I need to remove the 5th character of column1 and print the rest. Can anybody give some advice? Input: 0001c xx 0001r gg jj 0002y vv 0002p kk 0003q gg ll 0003v tt 0003t gg pp kk Output: 0001 xx 0001 gg jj (9 Replies)
Discussion started by: Raynon
9 Replies
Login or Register to Ask a Question