HELP: with very simple Perl Scripts

QTPie

Golden Member
Dec 30, 2001
1,813
1
81
Hello all,
Please help me with a very simple Perl script. I took a course b4 but it's been a long time so that I don't know what command to use :(

Example1: .txt file contains

text1 word1 sentence1
text2 word2 sentence2
text3 word3 sentence3
...
textN wordN sentenceN

I need to delete 2 columns of text in a file then save it into another file. In the example below, I need to detele 2 entities text(n) and word(n) , then save sentence(n) into a new text.file



Example2: .txt file contains

sentence1*phrase1
sentence2*phrase2
...
sentenceN*phraseN

I need to split this file into 2 .txt file: 1 file contains sentence(n) and another file contains *phrase(n)

You can just show me what command to use. Thank you very much for your help!
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Example 1:
open IN, "inputfile.txt";
open OUT, ">outputfile.txt";
while ( <IN> ){
$_ =~ /(sentence\d+)/;
print OUT "$1\n";
}
close IN;
close OUT;

Example 2:
open IN, "inputfile.txt";
open OUT1, ">outputfile1.txt";
open OUT2, ">outputfile2.txt";
while ( <IN> ){
$_ =~ /(sentence\d+)\*(phrase\d+)/;
print OUT1 "$1\n";
print OUT2 "$2\n";
}
close IN;
close OUT1;
close OUT2;
 

QTPie

Golden Member
Dec 30, 2001
1,813
1
81
notfred, thank you very much for your response.
I'm sorry that I didn't make my examples clear enough.

In the example 1, I'd like to copy a certain part of a sentence in a file into a new file. My text file contains many lines like this

Andy 12/03/2003 00:12:06 there're problems with the PC1
Mike 12/03/2003 00:28:25 Internet connection was down for 30min
....

I'd like to remove the name, date and time from the sentence and save the message so that I have a new file just like this:

there're problems with the PC1
Internet connection was down for 30min

------

In the example 2, I'd like to split the sentence into 2 parts including the delimiter. The original text file contains many lines like this:

bad hdd in PC1*replace it
network isn't stable *change the hub

I'd like to have it into 2 files as follow:

File 1 will have:
bad hdd in PC1
network isn't stable

File2 will have
*replace it
*change the hub

-------

In the mean time, I'm reading the Perl book, but still haven't found out what I want yet :(
Your help is greatly appreciated!

EDIT: either Perl or Shell script is fine as long as I can work it out