This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Linux / UNIX and the fly likes how to replace a string on a line irrespective of the position Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "how to replace a string on a line irrespective of the position" Watch "how to replace a string on a line irrespective of the position" New topic
Author

how to replace a string on a line irrespective of the position

rahulJ james
Ranch Hand

Joined: Oct 03, 2008
Posts: 123
I have a file as mentioned below,

ISA*1*123434
GS*2229*12333
ST*9993*99
HS*29909
SE*2*292
ST*9993*99
HS*29909
SE*2*292
GE*00344*1991 -> I wanted to replace the "00344" with the count of occurences of the row starting with ST
IEA*0032*2992 -> I wanted to replace the "0032" with the count of occurences of the row starting with GS


The length of the values "00344" and "0032" can be anywhere between 1 to 6 and it is not fixed. I am trying the below way to replace the "2992" value with the correct value, it will work when the positions are fixed but it might differ.



$tmpcr is a file which will contain the data starting from GS to GE as shown below,

GS*2229*12333
ST*9993*99
HS*29909
SE*2*292
ST*9993*99
HS*29909
SE*2*292
GE*00344*1991

$tmpiea will contain the IEA*0032*2992

Can anyone adivse me with an approach where it will work irrespective of the positions I have mentioned in my code.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

rahulJ james wrote:I have a file as mentioned below,

ISA*1*123434
GS*2229*12333
ST*9993*99
HS*29909
SE*2*292
ST*9993*99
HS*29909
SE*2*292
GE*00344*1991 -> I wanted to replace the "00344" with the count of occurences of the row starting with ST
IEA*0032*2992 -> I wanted to replace the "0032" with the count of occurences of the row starting with GS


.


Assumed the ST-lines are all identical, we can get one using sort -u, -u for unique.
STCnt=$(sort -u $tmpcr | egrep "^ST" | sed 's/^ST\*\(.*\)\*.*/\1/')
GSCnt=$(sort -u $tmpcr | egrep "^GS" | sed 's/^ST\*\(.*\)\*.*/\1/')

egrep is an extended grep, which knows regular expressions.

The sed-command is a bit complicatet - it's a substitution-command s/from/to, which uses * as repetition-modifier, and so it has to be escaped, if meant by itself.
^ST means ST at the beginning of the line.
round brackets are used for grouping, but have to be masked itself, for not being interpreted as just round brackets.
. a dot stands for every single character, .* for 0 to arbitrary repetitions of arbitrary characters.
The whole expression ^ST\*\(.*\)\*.* stands for ST*something*something, and \1 is a backreference to the first something, which was grouped by the brackets.


This similar command replaces in place (-i) the pattern, starting with GE with the ST-Pattern.
The ; makes it possible, to append a second command (for IEA).

Hehe, I don't conform to ranch standards.

We're sorry, but your post appears to contain abbreviations that we don't like people to use at the Ranch. Because JavaRanch is an international forum, many of our members are not native English speakers. For that reason, it's important that we all try to write clear, standard English, and avoid abbreviations and SMS shortcuts. See here for more of an explanation. Thanks for understanding.

If the abbreviation occurs within code, you can use code tags to post it successfully.

The specific error message is: "u" is a silly English abbreviation; use "you" instead.


http://home.arcor.de/hirnstrom/bewerbung
rahulJ james
Ranch Hand

Joined: Oct 03, 2008
Posts: 123
It gives me an illegeal option message as shown below,

sed: illegal option -- i

Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Mine is Gnu-sed on linux:
 
Don't get me started about those stupid light bulbs.
 
subject: how to replace a string on a line irrespective of the position
 
Similar Threads
Merging Multiple Lists
Extract string between two patterns
enabling & disabling menuitems
How many String objects ... ?!
Applet not displaying correctly