Solved

linux grep command + replace string ?

Posted on 2011-02-18
7
1,525 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

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.

Question has a verified solution.

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

In my business, I use the LTS (Long Term Support) versions of Linux. My workstations do real work, and so I rarely have the patience to deal with silly problems caused by an upgraded kernel that had experimental software on it to begin with from a r…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
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.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

803 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