sed code to match http://www.domain.com/ and replace with just a / in all files in a directory - linux

I've been searching for this answer for three hours now and I still can't get anything to work. When I run things like this:
sed -i 's/http\:\/\/www\.domain\.org\//\//g checkout_*.php
It drops me into another command line (sorry, I'm very new to sed).
I just want to cd to a dir, grep the dir to see if the string is there then run a replace so I can change my paths from absolute to relative.

You need to close your '. You can also make your command cleaner by using a different sed delimiter to / so that you don't have to escape all those forward slashes in your URL. For example, you can use !, as shown below:
sed -i 's!http://www\.domain\.org/!/!g' checkout_*.php

You just appear to be missing the closing '
sed -i 's/http\:\/\/www\.domain\.org\//\//g' checkout_*.php
Should do what you want ok. But I'd warn you against doing the -i switch without first doing a dry run.

Related

Deleting all lines if pattern matches in sed linux mint 17

I am quite new to shell scripting.
I am scraping a website and the scraped text contains a lot of repetitions. Usually they are the menus on a forum, for example. Mostly, I do this in Python, but I thought that sed command will save me reading and printing the input, loops etc. I want to delete thousands of repeated lines from the same single file. I do not want to copy it to another file, because I will end up with 100 new files. The following is a shadow script which I run from the bash shell.
#!/bin/sed -f
sed -i '/^how$/d' input_file.txt
sed -i '/^is test$/d' input_file.txt
sed -i '/^repeated text/d' input_file.txt
This is the content of the input file:
how to do this task
why it is not working
this is test
Stackoverflow is a very helpful community of programmers
that is test
this is text
repeated text is common
this is repeated text of the above line
Then I run in the shell the following command:
sed -f scriptFile input_file.txt
I get the following error
sed: scriptFile line 2: untermindated `s' command
How can I correct the script, and what is the correct syntax of the command I should use to get it work?
Any help is highly appreciated.
assuming you know what your script is doing, it's very easy to put them into a script. in your case, the script should be:
/^how$/d
/^is test$/d
/^repeated text/d
that's good enough.
to make the script alone to be executable is easy too:
#!/usr/bin/env sed -f
/^how$/d
/^is test$/d
/^repeated text/d
then
chmod +x your_sed_script
./your_sed_script <old >new
here is a very good and compact tutorial. you can learn a lot from it.
following is an example from the site, just in case the link is dead:
If you have a large number of sed commands, you can put them into a file and use
sed -f sedscript <old >new
where sedscript could look like this:
# sed comment - This script changes lower case vowels to upper case
s/a/A/g
s/e/E/g
s/i/I/g
s/o/O/g
s/u/U/g
Wouldn't it be easier to do it with egrep followed by a mv, for example
egrep -v 'pattern1|pattern2|pattern3|...' <input_file.txt >tmpfile.txt
mv tmpfile.txt input_file.txt
Each pattern would describe the lines being deleted, much like in sed. You would not end up with additional files, because the mv removes them.
If you have so many pattern, that you don't want to specify them directly on the command line, you can store them in a file use the -f option of egrep.

Find and replace text on multiple files

I am trying to replace a specific link which exists on many html pages with its https version. I have tried:
grep -rl "http://server.iad.liveperson.net/hc/88956865/" ./ | xargs sed -i "s/http:\/\/server.iad.liveperson.net\/hc\/88956865\//https:\/\/server.iad.liveperson.net\/hc\/88956865\//g"
When I do this, even as sudo, I am getting
sed: couldn't open temporary file ./customers/sedTR3AMu: Permission denied
customers is just the first directory in ./. So, it is hanging on the first file I reckon, but not sure what is wrong beyond that.
Any help is appreciated!
First thing you should try is to run the sed command as stand alone, for a file that you previously know that contains that string. I have the feeling that the sed command might be complaining about the / characters...
You should try changing the sed command to something like:
sed -i 's;http://server.iad.liveperson.net/hc/88956865/;https://server.iad.liveperson.net/hc/88956865/;g'
That is, using ; instead of / as the delimiter, so you don't have to escape the / every time using \.
Had to run the command logged in as root because sed -i creates temporary files in /tmp and needed write access.
Thanks:Used jim's syntax with the semicolons which worked fine. ooga, I did not have to escape the literal periods.

shell script find and replace in file

I have a file postmaster.log, in which I need to find pattern and change its value
Pattern I need to find is
MaxValue=3 #this could be any value not just 3
I need to change its value to
MaxValue=0
Issue is there are also patterns like
"MaxValueSet=3" and "MaxValue is currently low"
Which are also getting replaced.I only has to change MaxValue=3 to MaxValue=0
I tried using sed
sed -i 's/MaxValue=3/MaxValue=0/g' /home/postmaster.log
But this only works if MaxValue=3 for anyother value it won't work.
use a regexp to catch MaxValue= followed by any number...
s/MaxValue=[0-9]+/MaxValue=0/g
should work.
It sounds like you want
sed -i 's/^MaxValue=.*/MaxValue=0/' /home/postmaster.log
which will find all lines that begin with MaxValue=, and replace each of those lines with MaxValue=0.
You can restrict the lines sed works on as well:
sed -i '/^MaxValue=/s/=[[:digit:]][[:digit:]]*/=0/' /home/postmaster.log

Replace Windows newlines in a lot of files using sed - but it doesn't

I have a lot of files that end in the classical ^M, an artifact from my Windows times. As this is all source code, git actually thinks those files changed, so I want to remove those nasty lines once and for all.
Here is what I created:
sed -i 's/^M//g' file
But that does not work. Of course I did not type a literal ^M but rather ^V^M (ctrl V, ctrl M). In vim it works (:%s/s/^M//g) and if I modify it like this:
sed -i 's/^M/a/g' file
It also works, i.e. it ends every line with an 'a'. It also works to do this:
sed -i 's/random_string//g' file
Where random_string exists in the file. So I can replace ^M by any character and I can remove lines but I cannot remove ^M. Why?
Note: It is important that it is just removed, no replacing by another invisible char or something. I would also like to avoid double execution and adding an arbitrary string and removing it afterwards. I want to understand why this fails (but it does not report an error).
That character is matched with \r by sed. Use:
sed -e "s/\r//g" input-file
For my case, I had to do
sed -e "s/\r/\n/g" filename.csv
After that wc -l filename Showed correct output instead of 0 lines.

Search and replace text in all files of a linux directory

I have a website directory where I need to change all hardcoded links from one domain to another. Looking for a single (grep? sed?) bash command that will allow me to change all occurrences of text in all files in the directory?
The following will do it:
sed -i 's/old_link/new_link/g' file...
Don't forget to escape any slashes, dots, and any other regex special chars in the link addresses with a backslash.
Also, try:
perl -p -i -e <regex> <folder>

Resources