Solved

linux grep command + replace string ?

Posted on 2011-02-18
7
1,514 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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 76

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

1. Introduction As many people are interested in Linux but not as many are interested or knowledgeable (enough) to install Linux on their system, here is a safe way to try out Linux on your existing (Windows) system. The idea is that you insta…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

760 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

19 Experts available now in Live!

Get 1:1 Help Now