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.