I'm debugging a shell script and trying to find out the task performed by the following command:
sed -i '1,+999d' /home/org_user/data.txt
I need to change this command as its failing with the following error:
illegal option sed -i
But before changing, I need to understand the BAU functioning.
Appreciate any inputs in this regard.
An applicable use of this is as follows. Say you have the following file file.txt:
1, 2, 6, 7, "p"
We want to replace "p" with 0.
sed 's/"p"/0/g' file.txt
Using the above simply prints the output into command line.
You'd think why not just redirect that text back into the file like this:
sed 's/"p"/0/g' file.txt > file.txt
Unfortunately because of the nature of redirects the above will simply produce a blank file.
Instead a temp file must be created for the output which later overwrites the original file something like this:
sed 's/"p"/0/g' file.txt > tmp.txt && mv tmp.txt file.txt
Instead of doing the long workaround above sed edit in place option with -i allows for a much simpler command:
sed -i 's/"p"/0/g' file.txt
If -i option given, sed edit files in place.
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if extension supplied)
from sed(1)
http://www.gnu.org/software/sed/manual/sed.html#Introduction
Some implementations of sed do not support the -i option. What it does can be simulated by
sed -e '...' file > tmp
mv tmp file
Related
I need to modify an xml file using Sed to replace the line
url="jdbc:oracle:thin:#//ttpdbscan.axel.net:1521/axel.telco.net"
with
url="jdbc:oracle:thin:#//ttpdbscan.axeltelecom.net:1598/axelPRD.telco.net"
I have stored the lines like this
ACTUAL_DB=$(sed -n 's#^.*url="\(.*\).*"#\1#p' $FILE.xml)
and
NEW_DB="jdbc:oracle:thin:#//ttpdbscan.axeltelecom.net:1598/axelPRD.telco.net"
And the replacing method is this one
sed -i "s#$ACTUAL_DB#$NEW_DB#g" $File.xml
The problem is that when I run the script the file stays the same.
I have echoed the variables and all of them return the correct values.
Assuming the file you have is File.xml (if it is not a variable), you may use
sed -i "s#${ACTUAL_DB}#${NEW_DB}#g" File.xml
Try also with other delimiters:
sed -i "s~${ACTUAL_DB}~${NEW_DB}~g" File.xml
If your sed does not support -i use
sed "s~${ACTUAL_DB}~${NEW_DB}~g" File.xml 1<> File.xml
See sed edit file in place
So I saved the output into another file and found out that the string had an extra space
so it looked like this
ACTUAL_DB= "jdbc:oracle:thin:#//ttpdbscan.axel.net:1521/axel.telco.net "
I removed the extra space with
"$(echo -e "${ACTUAL_DB}" | tr -d '[:space:]')"
And now the sed is working as intended
I have a property file mail.properties. The content of the file is
ErrorEmailTo.server1=abc_1#gmail.com,bcd_1#gmail.com,efg_1#gmail.com
ErrorEmailFrom.server1=abc_1#gmail.com,bcd_1#gmail.com,efg_1#gmail.com
ErrorEmailCC.server1=abc_1#gmail.com,efg_1#gmail.com,bcd_1#gmail.com
...and numerous such entries.
I want to find bcd_1#gmail.com and delete it wherever it is present. The output property file (mail.properties) should look like :
ErrorEmailTo.server1=abc_1#gmail.com,efg_1#gmail.com
ErrorEmailFrom.server1=abc_1#gmail.com,efg_1#gmail.com
ErrorEmailCC.server1=abc_1#gmail.com,efg_1#gmail.com
You can try this sed,
sed 's/bcd_1#gmail.com,\?//g; s/,*$//' mail.properties
Use -i option for in-place edit.
sed -i 's/bcd_1#gmail.com,\?//g; s/,*$//' mail.properties
sed -e 's/bcd_1#gmail\.com//g' -e 's/,,/,/g' -e 's/,$//g' -e 's/=,/=/g' mail.properties > mail.properties_new
I want to empty a file using sed command. I searched lot of forums and tutorial. There is no available to delete all contents of the file. How to delete all contents of the file using sed command.
It looks like a strange request. Anyway, this is a way:
sed -i '/^/d' file
sed -i does an in-place replacement.
/^/ matches lines, in this case all of them because ^ means "beginning of line".
/d deletes them.
Or shorter (thanks glenn jackman as always):
sed -i d file
You don't need sed for this. To empty a file:
> filename
with no command, that redirection will truncate the file.
Try this sed. It will remove all.
sed -ni '' file
n do not print if not told to do so.
i in place.
Since no code is given, file will be replaced by nothing.
I'm trying to write a script to swap out text in a file:
sed s/foo/bar/g myFile.txt > myFile.txt.updated
mv myFile.txt.updated myFile.txt
I evoke the sed program, which swaps out text in myFile.txt and redirects the changed lines of text to a second file. mv then moves .updated txt file to myFile.txt, overwriting it. That command works in the shell.
I wrote:
#!/bin/sh
#First, I set up some descriptive variables for the arguments
initialString="$1"
shift
desiredChange="$1"
shift
document="$1"
#Then, I evoke sed on these (more readable) parameters
updatedDocument=`sed s/$initialString/$desiredChange/g $document`
#I want to make sure that was done properly
echo updated document is $updatedDocument
#then I move the output in to the new text document
mv $updatedDocument $document
I get the error:
mv: target `myFile.txt' is not a directory
I understand that it thinks my new file's name is the first word of the string that was sed's output. I don't know how to correct that. I've been trying since 7am and every quotation, creating a temporary file to store the output in (disastrous results), IFS...everything so far gives me more and more unhelpful errors. I need to clear my head and I need your help. How can I fix this?
Maybe try
echo $updatedDocument > $document
Change
updatedDocument=`sed s/$initialString/$desiredChange/g $document`
to
updatedDocument=${document}.txt
sed s/$initialString/$desiredChange/g $document
Backticks will actually put the entire piped output of the sed command into your variable value.
An even faster way would be to not use updatedDocument or mv at all by doing an in-place sed:
sed -i s/$initialString/$desiredChange/g $document
The -i flag tells sed to do the replacement in-place. This basically means creating a temp file for the output and replacing your original file with the temp file once it is done, pretty much exactly as you are doing.
#!/bin/sh
#First, I set up some descriptive variables for the arguments
echo "$1" | sed #translation of special regex char like . * \ / ? | read -r initialString
echo "$2" | sed 's|[\&/]|\\&|g' | read -r desiredChange
document="$3"
#Then, I evoke sed
sed "s/${initialString}/${desiredChange}/g" ${document} | tee ${document}
don't forget that initialString and desiredChange are pattern interpreted as regex, so a trnaslation is certainly needed
sed #translation of special regex char like . * \ / ? is to replace by the correct sed (discuss on several post on the site)
Recently I came across following grep command:
/usr/xpg4/bin/grep -Ff grep.txt input.txt > output.txt
which as per my understanding means that from input.txt, grep the matter contained in grep.txt and output it to output.txt.
I want to do something similar for sed i.e. I want to keep the sed commands in a separate file (say sed.txt) and want to apply them on input file (say input.txt) and create a output file (say output.txt).
I tried following:
/usr/xpg4/bin/sed -f sed.txt input.txt > output.txt
It does not work and I get the following error:
sed: command garbled
The contents of files mentioned above are as below:
sed.txt
sed s/234/acn/ input.txt
sed s/78gt/hit/ input.txt
input.txt
234GH
5234BTW
89er
678tfg
234
234YT
tfg456
wert
78gt
gh23444
Your sed.txt should only contain sed commands: No prefixing with sed or suffixing with an input file. In your case it should probably be:
# sed.txt
s/234/acn/
s/78gt/hit/
When ran on your input:
$ /usr/xpg4/bin/sed -f sed.txt input.txt
acnGH
5acnBTW
89er
678tfg
acn
acnYT
tfg456
wert
hit
ghacn44
Rather than keeping the sed commands in a separate text file, you may want to try creating a sed script. The file below can run directly on your data files:
./myscript.sed inputfile.txt > outputfile.txt
#!/bin/sed -f
s/234/acn/
s/78gt/hit/