?
Solved

Word Manipulation

Posted on 2011-05-01
5
Medium Priority
?
343 Views
Last Modified: 2012-05-11
Hello,

I have a file with words on each line:

example:

word1
word2
word3
etc.


I'm looking for a way to output them in the following format

$i=="word1" || $i=="word2"  || $i=="word3"  || $i=="etc"

Thank you
0
Comment
Question by:faithless1
  • 2
  • 2
5 Comments
 
LVL 4

Assisted Solution

by:a1j
a1j earned 200 total points
ID: 35503004
#!/usr/bin/perl

my $tmp=<>;
print '$tmp=="' . $tmp . "'";
while (<>) {
   chomp;
   print '|| $i=="' . $_ . "'";
}
##################
usage: cat infile.txt | ./script > outfile.txt
0
 
LVL 4

Expert Comment

by:a1j
ID: 35503022
errr ... probably need to insert "chomp $tmp;" before "print $tmp"
0
 
LVL 12

Accepted Solution

by:
tel2 earned 1800 total points
ID: 35503677
Hi faithfull1,

How's this:
    perl -0ne 's/^(.*)$/\$i=="$1"/mg;s/\n\$/ || \$/g;print' infile.txt
0
 
LVL 12

Expert Comment

by:tel2
ID: 35503736
...or written as a traditional script:
    #!/usr/bin/perl
    undef $/;
    $_ = <>;
    s/^(.*)$/\$i=="$1"/mg;
    s/\n\$/ || \$/g;
    print;

Usage: ./myscript.pl <data.in
0
 

Author Closing Comment

by:faithless1
ID: 35714127
Thanks!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question