Operating System - HP-UX
1832489 Members
4782 Online
110043 Solutions
New Discussion

In perl, how to modify all but 1st 2 lines in text file ?

 
SOLVED
Go to solution
Sammy_2
Super Advisor

In perl, how to modify all but 1st 2 lines in text file ?

I have done this.
perl -pi -e 's/^/{/g' datafile.txt
perl -pi -e 's/$/}/g' datafile.txt
It modifies all lines.

Need a perl command to insert { before each line and } after end of each line for all lines beginning and ending with "




======================
#cat datafile.txt

Table Contact
id, c_contact_num, c_notes
"11510474","adrian4"," "
"192031","roberto"," "
....
....

should look like this after executing the perl command. Prefer not to use sed as I have to redirect the output to another file

#cat datafile.txt
Table Contact
id, c_contact_num, c_notes
{"11510474","adrian4"," "}
{"192031","roberto"," "}
....
....
good judgement comes from experience and experience comes from bad judgement.
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor
Solution

Re: In perl, how to modify all but 1st 2 lines in text file ?

Hi Sammy:

# perl -pi.old -e 's/^"/("/;s/"$/")/' file

...and in "file.old" will be a backup copy of your original (unmodified) file. If you don't want a copy retained, do:

# perl -pi -e 's/^"/("/;s/"$/")/' file

Regards!

...JRF...

James R. Ferguson
Acclaimed Contributor

Re: In perl, how to modify all but 1st 2 lines in text file ?

Hi (again) Sammy:

Ooops you wanted curly braces, not parentheses:

# perl -pi.old -e 's/^"/{"/;s/"$/"}/' file

...and in "file.old" will be a backup copy of your original (unmodified) file. If you don't want a copy retained, do:

# perl -pi -e 's/^"/{"/;s/"$/"}/' file

Regards!

...JRF...


Sammy_2
Super Advisor

Re: In perl, how to modify all but 1st 2 lines in text file ?

Thanks JRF. As with most of your perl solution, this one also works like a charm.

For my perl knowledge, how would you convert following sed to perl.

sed -e '1,3s/^/TEST/g' file > file1

In perl, need to know how to modify just 1 thru 3 lines


The command below failed:
perl -pi -e '1,3s/^/TEST/g' file
good judgement comes from experience and experience comes from bad judgement.
Sammy_2
Super Advisor

Re: In perl, how to modify all but 1st 2 lines in text file ?

JRF, I caught curly braces but thanks.
good judgement comes from experience and experience comes from bad judgement.
James R. Ferguson
Acclaimed Contributor

Re: In perl, how to modify all but 1st 2 lines in text file ?

Hi Sammy:

Perl's motto is "TMTOWTDI" [ Google that if you don't know :-) ], so here's one

# perl -pe 's/^/TEST/ if $. <= 3' file

...in lieu of:

# sed -e '1,3s/^/TEST/g' file

Regards!

...JRF...
Sammy_2
Super Advisor

Re: In perl, how to modify all but 1st 2 lines in text file ?

Thanks JRF. I had googled "search and replace sed/perl" before I put this post out but did not get hits on specific lines. But once again, thanks for the tip . I will save this commands as I am using shell scripts but using perl commands (which seem easier) within script.

Appreciate it.
good judgement comes from experience and experience comes from bad judgement.
Sammy_2
Super Advisor

Re: In perl, how to modify all but 1st 2 lines in text file ?

solution found by JRF.
good judgement comes from experience and experience comes from bad judgement.