Solved

linux grep command + replace string ?

Posted on 2011-02-18
7
1,528 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

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Coding C# in Linux 8 131
awk file 6 78
sqlldr issue 1 15
Setup Ubuntu 16.04 to use LDAP for user login and authentication 2 81
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…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

828 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