?
Solved

linux grep command + replace string ?

Posted on 2011-02-18
7
Medium Priority
?
1,537 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 79

Accepted Solution

by:
arnold earned 1400 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

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…
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 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 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…
Suggested Courses
Course of the Month11 days, 15 hours left to enroll

752 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