• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
rahulJ james
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It gives me an illegeal option message as shown below,

sed: illegal option -- i

 
Stefan Wagner
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mine is Gnu-sed on linux:
reply
    Bookmark Topic Watch Topic
  • New Topic