-2

I want to write a Perl script to find the variable if present in the TEST titles only, not in the FIRST, SECOND or THIRD titles. So there are N number of variables in a files before the TEST titles; But I want to match the variables after the TEST titles. If the TEST title doesn't have the variable need to print variable not exist in first TEST (or Second TEST or Third TEST).

I have a file like below.

FIRST
there is a variable one

SECOND
there is second variable

THIRD
there is third variable

FOUR
xxx
xxx

FIVE
variable
xxx

TEST1
firstline 001
secondline 002
thirdvariable line 003

TEST2
firstline 001
secondline 002
thirdline 003

TEST3
firstline 001
secondline 002
thirdvariable line 003
  • Why only Perl? You are not interested with awk : ) also would you please let us know what have you tried? – αғsнιη Oct 13 '17 at 6:09
  • If you know the perl uu can give the answer – Beginner Oct 13 '17 at 6:15
  • So what do you have so far? Which part is giving you trouble? You need to do some of the work here too. What output do you expect from the example you gave us? Will the actual words FIRST, FOUR etc? Be there? How come you have first, second and third but then have four and not fourth? Will there always be a single empty line between the sections? How do we find the "variable"? What variable? How do we recognize it? We can't help you unless you give us enough information to understand what you need. – terdon Oct 13 '17 at 8:41
2

I would use awk for this (since I know awk fun!).

awk -vRS='TEST[[:digit:]]' 'BEGIN{rs=RS} NF>1 && $1 ~ rs{ print $1":";$1=""; print $0;}
    NF==1{print "variable not exist in: "$1}' RS='' infile

This will give you output like below for your given sample data.

TEST1:
 firstline 001 secondline 002 thirdvariable line 003
TEST2:
 firstline 001 secondline 002 thirdline 003
TEST3:
 firstline 001 secondline 002 thirdvariable line 003
variable not exist in: TEST4
variable not exist in: TEST5
  • Thanks, but my requirement will be do in Perl – Beginner Oct 13 '17 at 7:07
  • Your are welcome dear! I hope I could know about Perl to help you but unfortunately people cannot learn everything. At least I tried : ) – αғsнιη Oct 13 '17 at 7:14
  • 1
    @Beginner You need to unlock the Homework Automation achievement to get access to Perl scripts. – Satō Katsura Oct 13 '17 at 11:13
  • there is a a2p package that turn awk to perl. (do not show output to a perl adept ...) – Archemar Oct 13 '17 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.