LinuxQuestions.org
Help answer threads with 0 replies.
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 01-18-2011, 01:24 PM   #1
BabySunny
LQ Newbie
 
Registered: Jan 2011
Posts: 4

Rep: Reputation: 0
Mass edit files using perl script


Hi,

I am new to shell scritpting and one of my assignments at job requires me to use perl script to search for a variable which is present in different functions, but just replace the value of the variable only in one of the functions. It will be great if I can have a clue on this since perl is completely new to me

Thanks
 
Old 01-18-2011, 02:11 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Hi, welcome to LQ!

What's the criteria to assess which function's variable needs to change?



Cheers,
Tink
 
Old 01-18-2011, 02:19 PM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,505

Rep: Reputation: 3993Reputation: 3993Reputation: 3993Reputation: 3993Reputation: 3993Reputation: 3993Reputation: 3993Reputation: 3993Reputation: 3993Reputation: 3993Reputation: 3993
Quote:
Originally Posted by BabySunny View Post
Hi,
I am new to shell scritpting and one of my assignments at job requires me to use perl script to search for a variable which is present in different functions, but just replace the value of the variable only in one of the functions. It will be great if I can have a clue on this since perl is completely new to me
Thanks
Check into regular-expressions in Perl. This is a very brief start:
http://www.regular-expressions.info/perl.html

As Tinkster said, alot depends on what the criteria are. For example, if you just have to replace the second occurance of a string, that's easy. If you have to find a particular string, that's at an arbitrary location in a file, and replace the next occurance, that's harder.
 
Old 01-19-2011, 11:40 AM   #4
BabySunny
LQ Newbie
 
Registered: Jan 2011
Posts: 4

Original Poster
Rep: Reputation: 0
The scenario is this

function1()
{
variable='xxx'
}

function 2()
{
variable='aaa bbb ccc ddd'
function1()
}
function1()


I have to append a new value ggg to the variable in function 2 after making sure the value ggg is not already present.Moreover the same variable variable which is also present in function 1 shudnt be modified. I tried this with perl one liner for search and replace text and some code, but value gets inserted at wrong positions and moreover modifies all functions which has the same variable. Any clue on this would be of great help.
 
Old 01-19-2011, 12:45 PM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,505

Rep: Reputation: 3993Reputation: 3993Reputation: 3993Reputation: 3993Reputation: 3993Reputation: 3993Reputation: 3993Reputation: 3993Reputation: 3993Reputation: 3993Reputation: 3993
Quote:
Originally Posted by BabySunny View Post
The scenario is this

function1()
{
variable='xxx'
}

function 2()
{
variable='aaa bbb ccc ddd'
function1()
}
function1()


I have to append a new value ggg to the variable in function 2 after making sure the value ggg is not already present.Moreover the same variable variable which is also present in function 1 shudnt be modified. I tried this with perl one liner for search and replace text and some code, but value gets inserted at wrong positions and moreover modifies all functions which has the same variable. Any clue on this would be of great help.
Based on what you posted, it seems simple, but I wouldn't do it as a one-liner.

I'd first grep for the pattern ggg in the file. If it's present, go to the next file. If not, then open the file for editing, then replace
Code:
variable='aaa bbb ccc ddd'
with
Code:
variable='aaa bbb ccc ddd ggg'
As you posted it, the first variable is only 'xxx', or something with just ONE field. So search for a space, which would then take you to variable 2, where you do an append to the string, and write it out to the file.
 
Old 01-19-2011, 01:40 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by TB0ne View Post
Based on what you posted, it seems simple, but I wouldn't do it as a one-liner.

I'd first grep for the pattern ggg in the file. If it's present, go to the next file. If not, then open the file for editing, then replace
Code:
variable='aaa bbb ccc ddd'
with
Code:
variable='aaa bbb ccc ddd ggg'
As you posted it, the first variable is only 'xxx', or something with just ONE field. So search for a space, which would then take you to variable 2, where you do an append to the string, and write it out to the file.

I think we understand the task at hand differently; I thought those
functions were all within one file, and the change is to be done to
the variable that's part of function2 only.

That means he needs to write a basic parser to determine the opening
and closing braces so he can isolate the code of function2, and then
check, and if required, modify that value.



Cheers,
Tink
 
Old 01-19-2011, 01:55 PM   #7
BabySunny
LQ Newbie
 
Registered: Jan 2011
Posts: 4

Original Poster
Rep: Reputation: 0
That is exactly what I need...Need to isolate function 2...check if the variable in function2 already holds the value ggg, if not append it to the variable string..and repeat this on all files in the directory
 
Old 01-19-2011, 02:30 PM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,505

Rep: Reputation: 3993Reputation: 3993Reputation: 3993Reputation: 3993Reputation: 3993Reputation: 3993Reputation: 3993Reputation: 3993Reputation: 3993Reputation: 3993Reputation: 3993
Quote:
Originally Posted by BabySunny View Post
That is exactly what I need...Need to isolate function 2...check if the variable in function2 already holds the value ggg, if not append it to the variable string..and repeat this on all files in the directory
I understand that.

Based on what you posted, the variables you don't want to change are single-variables, containing only 'xxx'. The ones you DO want to change are array-type variables, with white space/delimiters. What I'm suggesting is simple...look through the file for what you want to be there...if you find it, move on to the next file. If you DON'T find it, open the file up, and scan for the "variable=", then look at that for a whitespace delimiter. If you DO find it, then you know it needs to have the 'ggg' appended. I'm saying make the delimiter in the variable what you scan for, since that's really what you're after.

Again, I'm basing this on what you posted....
 
Old 01-19-2011, 03:53 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by BabySunny View Post
That is exactly what I need...Need to isolate function 2...check if the variable in function2 already holds the value ggg, if not append it to the variable string..and repeat this on all files in the directory
I'll suggest you install (or use if already installed) Text::Balanced for this.



Cheers,
Tink
 
Old 02-03-2011, 09:58 AM   #10
BabySunny
LQ Newbie
 
Registered: Jan 2011
Posts: 4

Original Poster
Rep: Reputation: 0
In the same scenario..I have another question to ask...how can I parse till the end of a function in a file and insert two variables just before the function ends..I am having problem in parsing till the function end..because the function itself has some blocks of statements with curly braces, so not sure how to parse excatly till function end
 
Old 02-03-2011, 11:30 AM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Same answer as above: use Text::Balanced
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Edit script to not remove certain files JavaNinja Linux - Newbie 10 01-06-2009 05:43 AM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-22-2008 11:25 PM
Help with script to batch edit text files OnoTadaki Programming 5 10-15-2007 02:44 PM
edit wav files via command line in a script legolin Linux - Software 4 12-21-2005 10:09 AM
Perl or Bash Mass File Edit redneon Programming 1 10-14-2004 11:43 AM


All times are GMT -5. The time now is 05:01 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration