Solved

linux grep command + replace string ?

Posted on 2011-02-18
7
1,520 Views
Last Modified: 2012-05-11
Hello experts

In linux, i'm using the following command:

grep -ir "dbup" *  

in an directory "A", so it would display all files that contain the word dbup (ignore case)...What I want to do is to immediately replace all those results with another string

more or less like

grep -ir "dbup" * | replacetheresultwithstring"B"

Any suggestions? :(
0
Comment
Question by:Arrismog
7 Comments
 
LVL 14

Expert Comment

by:Deepak Kosaraju
ID: 34931323
u can use sed for that
sed -i 's/<search string>/<replace string>/gI' *
--- Be careful to use following command before apply overwriting to file. --
sed 's/<search string>/<replace string>/gI' *
0
 

Author Comment

by:Arrismog
ID: 34931354
-i option invalid :(:

executed -- sed -i 's/dbup/dbb/gI' *

sed: invalid option -- i
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

  -n, --quiet, --silent
                 suppress automatic printing of pattern space
  -e script, --expression=script
                 add the script to the commands to be executed
  -f script-file, --file=script-file
                 add the contents of script-file to the commands to be executed
      --help     display this help and exit
  -V, --version  output version information and exit

If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret.  All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.

E-mail bug reports to: bug-gnu-utils@gnu.org .
Be sure to include the word ``sed'' somewhere in the ``Subject:'' field.
0
 
LVL 14

Expert Comment

by:Deepak Kosaraju
ID: 34931621
which version of sed are you using. I am using GNU sed - /bin/sed and the pattern works for me.



# Original File #
$cat status.pl
#!/usr/bin/perl
my %status = ( 
	'OK' => 0,
	'WARNING' => 1,
	'CRITICAL' => 2,
	'UNKNOWN' => 3
);
foreach (sort keys %status){
print "$_ exit code is $status{$_}\n";
}

Modified:
$sed 's/ok/OKKKKKK/gI' status.pl  
#!/usr/bin/perl
my %status = ( 
	'OKKKKKK' => 0,
	'WARNING' => 1,
	'CRITICAL' => 2,
	'UNKNOWN' => 3
);
foreach (sort keys %status){
print "$_ exit code is $status{$_}\n";
}



# If I use -i with sed its going to replace OK with OKKKKK

sed -i 's/ok/OKKKKKK/gI' status.pl

Open in new window

0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 14

Expert Comment

by:Deepak Kosaraju
ID: 34931627
Following is my version of sed:

[root@pro01 ~]# which sed
/bin/sed

[root@pro01 ~]# rpm -qf /bin/sed
sed-4.1.5-5.fc6

[root@pro01 ~]# yum info sed
Installed Packages
Name   : sed
Arch   : i386
Version: 4.1.5
Release: 5.fc6
Size   : 321 k
Repo   : installed
Summary: A GNU stream text editor.

Description:
The sed (Stream EDitor) editor is a stream or batch (non-interactive)
editor.  Sed takes text as input, performs an operation or set of
operations on the text and outputs the modified text.  The operations
that sed performs (substitutions, deletions, insertions, etc.) can be
specified in a script file or from the command line.


[root@pro01 ~]#

Open in new window

[root@pro01 ~]# rpm -qf /bin/sed
sed-4.1.5-5.fc6

[root@pro01 ~]# yum info sed
Installed Packages
Name   : sed
Arch   : i386
Version: 4.1.5
Release: 5.fc6
Size   : 321 k
Repo   : installed
Summary: A GNU stream text editor.

Description:
The sed (Stream EDitor) editor is a stream or batch (non-interactive)
editor.  Sed takes text as input, performs an operation or set of
operations on the text and outputs the modified text.  The operations
that sed performs (substitutions, deletions, insertions, etc.) can be
specified in a script file or from the command line.

Open in new window

0
 
LVL 77

Accepted Solution

by:
arnold earned 350 total points
ID: 34931790
perl -i.bak -e -p 's/dbup/replacementpattern/g' *

-i.bak will create a bakup file of the original_filename.bak

you could use "grep -l "dbup" *" as the means by which to list the files for perl instead of the *.
0
 
LVL 19

Expert Comment

by:simon3270
ID: 34931998
"sed -i" is on GNU systems such as Linux - many UNIX variants do not support it.

The usual workaround is something like:
for ii in *
do
    sed 's/oldstring/newstring/g' $ii > /tmp/aa && mv /tmp/aa $ii
done

Open in new window

so for each file, it runs sed to a temporary file, and if the "sed" comamnd returns an OK response (i.e. it doesn't have a failure such as "file system full"), it overwrites the original file with the modified one.

If you want a backup (the "sed -i.bak" variant) then use:
 sed 's/xx/yy/' $ii > ${ii}.new && mv $ii ${ii}.bak && mv ${ii}.new $ii

Open in new window

0
 

Author Closing Comment

by:Arrismog
ID: 34933406
Made it work with

grep -irl "oldstring" * | xargs perl -pi -e 's/oldstring/newstring/'

:) thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will explain how to establish a SSH connection to Ubuntu through the firewall and using a different port other then 22. I have set up a Ubuntu virtual machine in Virtualbox and I am running a Windows 7 workstation. From the Ubuntu vi…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now