Linux - replace a string - linux

I need to replace a string / line for each instance of that string / line.
In the following line:
set output "/DS/tmp/2.gnuplot.ps"
I need it to be:
set output "./gnuplot.ps"
I want to use:
grep -rl 'stringONE' ./ | xargs sed -i 's/stringONE/stringTWO/g'
but when I try to use it, it has a conflict with the / in the string...
grep -rl '/DS/tmp/2.gnuplot.ps' ./ | xargs sed -i 's//DS/tmp/2.gnuplot.ps/./gnuplot.ps/g'
Any help will be greatly appreciated!!!

If the string to be replaced contains slash (/) then you can delimit arguments of sed's s command with something else, in this case I used comma:
grep -rl '/DS/tmp/2.gnuplot.ps' ./|xargs sed -i 's,/DS/tmp/2.gnuplot.ps,./gnuplot.ps,g'

Related

removing empty lines from a pipe using sed

$ find ~/AppData/Local/atom/ -name atom.sh -type f | grep -FzZ 'cli/atom.sh' | sed '/^\s*$/d' | cat -n
1 /c/Users/devuser/AppData/Local/atom/app-1.12.1/resources/cli/atom.sh
2 /c/Users/devuser/AppData/Local/atom/app-1.12.2/resources/cli/atom.sh
3
I tried a number of sed/awk-based options to get rid of the blank line. (#3 in the output). But I can't quite get it right...
I need to get the last line into a variable...
The below actual command I am working with fails to give an output...
find ~/AppData/Local/atom/ -name atom.sh -type f | grep -FzZ 'cli/atom.sh' | sed '/^$/d' | tail -n 1
Below sed command would remove all the empty lines.
sed '/^$/d' file
or
Below sed command would remove all the empty lines and also the lines having only spaces.
sed '/^[[:blank:]]*$/d' file
Add -i parameter to do an in-place edit.
sed -i '/^[[:blank:]]*$/d' file
The problem is the -Z flag of grep. It causes grep to terminate matched lines with a null character instead of a newline. This won't work well with sed, because sed processes input line by line, and it expects each line to be terminated by a newline. In your example grep doesn't emit any newline characters, so as far as sed is concerned, it receives a block of text without a terminating newline, so it processes it as a single line, and so the pattern /^\s*$/ doesn't match anything.
Furthermore, the -z flag would only make sense if the filenames in the output of find were terminated by null characters, that is with the -print0 flag. But you're not using that, so the -z flag in grep is pointless. And the -Z flag is pointless too, because that should be used when the next command in the pipeline expects null-terminated records, which is not your case.
Do like this:
find ~/AppData/Local/atom/ -name atom.sh -type f -print0 | grep -zF 'cli/atom.sh' | tr '\0' '\n' | tail -n 1
Not all sed's support the token \s for any whitespace character.
You can use the character class [[:blank:]] (for space or tab), or [[:space:]] (for any whitespace):
sed '/^[[:blank:]]*$/d'
sed '/^[[:space:]]*$/d'

for each line of a file, grep a specific string and make string substitution

I have a file containing more than 14000 records.
What I want to do is to process this file line by line and replace a String by anodher string returned by grep command.
For example:
Line :
/xxxxx/xxxxx/Class.java:67: Logger.w(TAG, "message");
My grep command to get Class.java string is (Class.java is juste an example):
grep -o '[a-zA-Z]*"*\.java"*'
I must, for each line, replace the TAG string by the class.java string return by grep command
You can use sed and do the following:
sed -r 's#(.*)/(.*)\.java(.*)(TAG)#\1\/\2\3\2#g'
Characters surrounded with parenthesis are groups that you can use in the second part to get their content.
In order to modify the file in-place, you should:
sed -ir 's#(.*)/(.*)\.java(.*)(TAG)#\1\/\2\3\2#g' your_file.txt
This is where sed comes in:
sed -i 's/TAG/class.java/g' Class.java
do it for all java files in current directory (assuming bash here):
sed -i 's/TAG/class.java/g' *.java
-i means in-place, so replacing takes place inside the file and is saved immediately. For the rest I suggest you google about sed.
You can use:
grep -rl 'old_string' ./ | xargs sed -i 's/old_string/Relacement_String/g'

Search and Replace SED Linux

I need to convert my all url relative path
https://www.example.com/image/abc.gif to /image/abc.gif
i tried this command but not worked for https:\\ section . How can i use https\\ in this command .
grep -rl "http://www.example.com" /root/ | xargs sed -i 's/http://www.example.com//g'
The following will do the job:
echo "https://www.example.com/image/abc.gif" | sed 's/https:\/\/www\.example\.com//g'
OUTPUT
/image/abc.gif
You can use regular expressions, the -r indicates extended regexes. The ? says 0 or 1 occurrence of 's'.
echo "http://www.example.com/image/abc.gif" | sed -r 's/https?:\/\/www\.example\.com//g'

Using xargs with Special characters

I have the following problem.
Got a file which includes certain paths/files of a FS.
These for some reason do include the whole range of special characters, like space, single/double quotes, even sometimes the Copyright ASCII.
I need to run each line of the file and pass it to another command.
What I tried so far is:
<input_file xargs -I % command %
Which was working until I got this message from xargs
xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option
But usinf this option did not work at all for me
xargs: argument line too long
Does anybody have a solution which does work ok with special characters.
Doesn't have to be with xargs, but I need to pass the line as it is to the command.
Many thanks in advance.
You should separate the filenames with the \0 NULL character for processing.
This can be done with
find . <args> -print0 | xargs -0
or if you must process the file with filenames, change the '\n` to '\0', e.g.
tr '\n' '\0' < filename | xargs -0 -n1 -I% echo "==%=="
the -n 1 says,
-n max-args
Use at most max-args arguments per command line.
and you should to use "%" quotes to enclosing %
The xargs -0 -n1 -I% echo "==%==" solution didn't work for me on my Mac OS X, so I found another one.
<input_with_single_quotes cat | sed "s/'/\\\'/" | xargs -I {} echo {}
This replaces the ' character with \' that works well as an input to the commands in xargs.

Replace string using grep and sed

I have bunch of files in a directory,
I need to change prefix of lines in file like "AB_" to "YZ_"
how can i do it?
i have used grep and sed like,
grep -nr "AB_" ./ | xargs -0 sed -i 's/AB_/YZ_/g'
but giving error,
: File name too long
example string in a file are: Hello AB_WORLD! and Hello WORLD_AB!
Thanks.
sed will take multiple files as arguments, so this should work:
sed -i '/AB_/s//YZ_/g' *
(Note that -i is non-standard)
You mean grep -lr not grep -nr
-l gives you the file name; -n gives you the matching line with line number prepended
I like Perl for this one:
The -i option will save the original file with a.bak extension.
$ perl -i.bak -pe 's/^AB_/YZ_/' *.txt
grep -lr "AB_" ./ | while read file
do
echo "Change file $file ..."
sed -i 's/AB_/YZ_/g' ${file}
done
sed one-liner answer
Find php files in the directory containing string "foo" and replace all occurences with "bar"
grep -l foo *.php | xargs sed -i '' s/foo/bar/g
To recurse through directories
grep -rl foo * | xargs sed -i '' s/foo/bar/g
(just done successfully on 8100 files)
grep -rl bar * | wc -l
8102

Resources