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.
Shell ScriptingLinux OS Dev

Avatar of undefined
Last Comment
tel2

8/22/2022 - Mon
tel2

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.
tel2

If you could give us sample input and output, that would be great.
radix655

ASKER
Sample Input:

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

Sample Output:
sr2
sr3

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
radix655

ASKER
Please use this.

Sample Input:

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

Sample Output:
sr2
source3
tel2

Thanks radix,

Does it have to be awk, or are sed or Perl OK?
radix655

ASKER
It has to be awk please if possible.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
SOLUTION
tel2

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
tel2

BTW, why do you prefer awk?
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
radix655

ASKER
Amazing. Thanks.
tel2

Nice work, wesley.

radix, mine could be written better as:
    perl -pe 's/^.+<25101>=(.+?)].+/$1/' inputfile
Your help has saved me hundreds of hours of internet surfing.
fblack61
wesly_chen

@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/Programming/Languages/Scripting/Shell/Q_27239450.html
tel2

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.