Link to home
Start Free TrialLog in
Avatar of radix655
radix655

asked on

How to get the substring from a line.

soemthing ..... [SourceSystemCode<25101>=789] something else.

How do I get the substring using awk

I am trying to use the index function.

awk '{ substr($0, index($0, "<25101>=")+8, not sure what index I should pass here... }'

Please suggest.
Avatar of tel2
tel2
Flag of New Zealand image

Hi radix,

In getting the index, what are you trying to achieve?  Are you wanting to return the whole line starting from "<25101>="?  If so, there are probably better ways.
If you could give us sample input and output, that would be great.
Avatar of radix655
radix655

ASKER

Sample Input:

[Source<25101>=sr2][SourceSystemCode<25102>=sr3]
[Source<25101>=sr3][OrderId<25102>=sr3]

Sample Output:
sr2
sr3

Please use this.

Sample Input:

[Source<25101>=sr2][SourceSystemCode<25102>=sr3]
[Source<25101>=source3][OrderId<25103>=101]

Sample Output:
sr2
source3
Thanks radix,

Does it have to be awk, or are sed or Perl OK?
It has to be awk please if possible.
SOLUTION
Avatar of tel2
tel2
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
BTW, why do you prefer awk?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Amazing. Thanks.
Nice work, wesley.

radix, mine could be written better as:
    perl -pe 's/^.+<25101>=(.+?)].+/$1/' inputfile
@tel2
you might want to check radix first question, it is very challenging in shell script but easier in Perl.
https://www.experts-exchange.com/questions/27239450/Script-to-get-count-of-parent-Orders.html
Thanks wesly,

Yes, I had seen that question, and was ignoring it because:
1. He hadn't responded to your request for more data yet.
2. I don't know if I have time.