nixCraft Linux / UNIX / Shell Scripting Forum

nixCraft

Linux / UNIX Tech Support Forum

Replacing a line of text with perl question

This is a discussion on Replacing a line of text with perl question within the Shell scripting forums, part of the Development/Scripting category; I am looking for a way to replace a line of text in a file using perl ( or another ...


Register free or login to your account to remove all advertisements.

Go Back   nixCraft Linux / UNIX / Shell Scripting Forum > Development/Scripting > Shell scripting

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

Thread Tools Display Modes
  #1 (permalink)  
Old 10th March 2010, 08:05 PM
Junior Member
 
Join Date: Oct 2009
OS: suse
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
Rep Power: 0
a31modela is on a distinguished road
Default Replacing a line of text with perl question

I am looking for a way to replace a line of text in a file using perl ( or another way if easier) . What I need to do is take a line that starts with CorePrintQueue= and replace it with CorePrintQueue=lprinterXX ( the xx is user specified from a menu. )

Is there any way to use a wild card to replace a line that STARTS WITH CorePrintQueue= and change it to the value the users sets from a menu ?

I have tried this :

perl -pi -e 's/CorePrintQueue=lprinter13/CorePrintQueue=lprinter10/g' /tmp/printer.txt

and it works but only if the line contains lprinter13. I need to be able to replace the entire line regardless of what is after the = or just replace whatever is after the = and leave the CoreprintQueue= alone

Thanks,
Steve
Reply With Quote
  #2 (permalink)  
Old 11th March 2010, 12:35 AM
Junior Member
 
Join Date: Oct 2009
OS: suse
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
Rep Power: 0
a31modela is on a distinguished road
Default I think I found a way

So after much trial & error, I think I found a way to do this & thought I'd pass it along.

I have a script running as such :

#!/bin/bash

currentprinter=`grep -F CorePrintQueue /tmp/printer.txt`

newprinter=lprinter12
echo $currentprinter
echo $newprinter

perl -pi -e 's/'$currentprinter'/CorelabelPrintQueue='$newprinter'/g' /tmp/printer.txt

The file printer.txt looks like this :

CorelPrintQueue=steve
CorelPrintFont=18

The actual value for newprinter will be the result of the user menu.

Anyone see any flaws in this ??

Thanks,

Steve
Reply With Quote
  #3 (permalink)  
Old 11th March 2010, 02:11 AM
nixcraft's Avatar
Never say die
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash, Perl, Python
Posts: 3,195
Thanks: 13
Thanked 394 Times in 292 Posts
Rep Power: 10
nixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond repute
Default

Try sed
Code:
echo 'CorePrintQueue=lprinter13' | sed -e 's/\(CorePrintQueue=\)\(.*\)/\1MyNewValueHere/g'
OR
Code:
echo 'CorePrintQueue=lprinter1353463463' | sed -e 's/\(CorePrintQueue=\)\(.*\)/\1MyNewValueHere/g'
And bash code
Code:
#!/bin/bash
current=$(grep CorelPrintQueue /tmp/printer.txt)
newprinter="$1"
echo "$current" |sed -e "s/\(CorelPrintQueue=\)\(.*\)/\1${newprinter}/g" /tmp/printer.txt
Run this script as:
Code:
./script.sh lprinter12
__________________
Vivek Gite
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Do you run a Linux? Let's face it, you need help!
Cricket & IPL News Blog

Last edited by nixcraft; 11th March 2010 at 02:20 AM.
Reply With Quote
The Following User Says Thank You to nixcraft For This Useful Post:
a31modela (11th March 2010)
  #4 (permalink)  
Old 11th March 2010, 11:01 PM
Junior Member
 
Join Date: Oct 2009
OS: suse
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
Rep Power: 0
a31modela is on a distinguished road
Default

Vivek,

Thanks very much for the followup. I used what you provided & came up with the following that works as desired. Thanks again !

Code:
#!/bin/bash

current=$(grep CorelabelPrintQueue /tmp/printer.txt)
newprinter="$1"

perl -pi -e 's/CorelabelPrintQueue=.*/CorelabelPrintQueue='$newprinter'/g' /tmp/printer.txt
if I run ./printer_script.sh lprinter12 I get the desired result.

regards,

Steve

Last edited by nixcraft; 11th March 2010 at 11:23 PM.
Reply With Quote
Reply

Tags
file printer, linux, perl, perl -pie, perl find replace, perl update file, sed, sed replace, sed update file, send find replace, trial error, unix, wild card


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
perl replace text kasimani CentOS / RHEL / Fedora 3 10th November 2009 07:56 PM
[Solved] How I can delete an entire line from an text (or other) file? vampire Shell scripting 2 29th September 2009 06:19 PM
Shell Script To change strings / text in a text file jaysunn Shell scripting 1 8th May 2009 05:58 PM
HPUX Unix comparing 2 large files line by line raj HP-UX 1 11th February 2008 05:20 PM
Replacing text in a file using awk postyrus Shell scripting 4 2nd May 2005 03:31 PM


All times are GMT +5.5. The time now is 09:04 PM.


Powered by vBulletin® Version 3.8.6 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40