LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-29-2021, 03:42 PM   #1
amateurscripter
Member
 
Registered: Nov 2011
Posts: 41

Rep: Reputation: Disabled
perl: regex exact match using if statement


Hello,

I tried few different ways to return a unique result but it's not working.

I have a script that goes through the below sample data set and then return matches based on third column. So it extracts column three and then goes through the data again to dissect each line. Notice there are dupes for each app(ie: sweep running on zpd58 and tdp58).

zpd58.vm2.config|sweep|SWEPCRS|299.352.773.112
zpd71.vm14.config|mkt|SPREADSOOT|299.352.773.113
zpd71.vm15.config|mkt|SPREADSOOT2|299.352.773.111
zpd71.vm16.config|rpt2|SOOTOT2|299.352.773.112
zpd71.vm16.config|spt2|SPAAN2|299.352.773.112
zpd72.vm14.config|rpt3|SOOTOT3|299.352.773.113
tpd44.vm2.config|sweep|SWEPCRS|288.532.235.156
tpd34.vm14.config|mkt|SPREADSOOT|288.532.235.157
tpd22.vm15.config|mkt|SPREADSOOT2|288.532.235.156
tpd28.vm16.config|rpt2|SOOTOT2|288.532.235.156
tpd41.vm16.config|spt2|SPAAN2|288.532.235.156
tpd62.vm14.config|rpt3|SOOTOT3|288.532.235.157

So when I run this part of the code, I think it's greedy and matches both SPREADSOOT and SPREADSOOT2 when it tries to match for SPREADSOOT. And notice I want to only process lines starting with "z". I tried all different variation and no luck:

if(($data =~ m/^zpd/) and ($routes =~ m/$routes\b0$data/)){

if(($data =~ m/^zpd/) and ($routes =~ m/$routes\|$data/)){

if(($data =~ m/^zpd/) and ($routes =~ m/$routes?.*$data/)){

OUTPUT:
....
The route is SWEPCRS app is sweep host: zpd58 and vm: vm2 quote:299.352.773.112
The route is SPAAN2 app is spt2 host: zpd71 and vm: vm16 quote: 299.352.773.112
The route is SPREADSOOT app is mkt host: zpd71 and vm: vm14 quote: 299.352.773.113
The route is SPREADSOOT app is mkt host: zpd71 and vm: vm14 quote: 299.352.773.113
The route is SPREADSOOT2 app is mkt host: zpd71 and vm: vm15 quote: 299.352.773.111

So when it's processing SPREADSOOT, how do I get it to not process or match against data for SPREADSOOT2?
 
Old 12-29-2021, 06:14 PM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,670
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
Simplify the problem: begin with split. Since this is “pipe-delimited data,” first split it by pipe, thus creating an array, then concentrate on the one column of interest. This should completely eliminate concerns of “greediness.”

Last edited by sundialsvcs; 12-29-2021 at 06:17 PM.
 
Old 12-30-2021, 05:28 AM   #3
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Code:
perl -F'[|]' -le '
@a=split(/[.]/);
print"The route is $F[2] app is $F[1] host: $a[0] and vm: $a[1] quote: $F[3]" if /^z/' \
  input_data
awk is a bit more concise here:
Code:
awk -F\| '
/^z/{
  split($1,a,".")
  print"The route is",$3,"app is",$2,"host:",a[1],"and vm:",a[2],"quote:",$4
}' input_data
A shell script would do as well:
Code:
#!/bin/sh
file='input_data'

while IFS='.|' read -r h vm _ a r q
do
  case $h in z*)
    echo "The route is $r app is $a host: $h and vm: $vm quote: $q"
  esac
done <"$file"

Last edited by shruggy; 01-11-2022 at 07:44 AM.
 
Old 12-30-2021, 04:36 PM   #4
amateurscripter
Member
 
Registered: Nov 2011
Posts: 41

Original Poster
Rep: Reputation: Disabled
Thx guys. I'm good.
 
  


Reply

Tags
perl, regex



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to capture 1000 lines before a string match and 1000 line a string match including line of string match ? sysmicuser Linux - Newbie 12 11-14-2017 05:21 AM
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 01:10 AM
[SOLVED] if statement regex match for white spaces ted_chou12 Programming 4 04-09-2011 04:23 AM
[SOLVED] Match datetime by the minute (not an exact match by the second) [mysql] hattori.hanzo Programming 1 10-21-2010 05:43 PM
Perl to find regex and print following 5 lines after regex casperdaghost Linux - Newbie 3 08-29-2010 08:08 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration