Linux / UNIX Tech Support Forum
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.
|
|||||||
| Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques |
![]() |
|
|
Thread Tools | Display Modes |
|
|||
|
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 |
|
|||
|
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 |
|
||||
|
Try sed
Code:
echo 'CorePrintQueue=lprinter13' | sed -e 's/\(CorePrintQueue=\)\(.*\)/\1MyNewValueHere/g' Code:
echo 'CorePrintQueue=lprinter1353463463' | sed -e 's/\(CorePrintQueue=\)\(.*\)/\1MyNewValueHere/g' Code:
#!/bin/bash
current=$(grep CorelPrintQueue /tmp/printer.txt)
newprinter="$1"
echo "$current" |sed -e "s/\(CorelPrintQueue=\)\(.*\)/\1${newprinter}/g" /tmp/printer.txt
Code:
./script.sh lprinter12
__________________
Vivek Gite Last edited by nixcraft; 11th March 2010 at 02:20 AM. |
| The Following User Says Thank You to nixcraft For This Useful Post: | ||
a31modela (11th March 2010) | ||
|
|||
|
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 regards, Steve Last edited by nixcraft; 11th March 2010 at 11:23 PM. |
![]() |
|
|
| 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 | |
|
|
|
||||
| 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 |