Delete all contents of the file using sed - unix

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.

Related

Grep to find a pattern and replace in same line

I have a project directory with folders containing .html files. I want to find those files which have the pattern -
'btn-primary.*{.*Save'
And replace the
'btn-primary' word with 'btn-primary Save'
only in those lines.
What I have done:
grep -rl -e 'btn-primary.*{Save' . |xargs sed -i 's/btn-primary/btn-primary Save/g'
What this did:
This found all files that have that pattern, that's okay. Then, sed ran on all of those files and replaced 'btn-primary' with 'btn-primary save' wherever it got - which is not what I want
What I want: to replace on those lines where there is 'Save' somewhere after 'btn-primary'.
Any help will be very much appreciated.
Regards,
Rahul
Why are you using grep at all? Sed does pattern matching:
sed -e 's/btn-primary\(.*{.*Save\)/btn-primary Save\1/g'
or:
sed -e 's/\(btn-primary\)\(.*{.*Save\)/\1 Save\2/g'
If you are using grep to try to trim down the number of files that sed will operate on, you're fooling yourself if you believe that is more efficient. By doing that, you will read every file that doesn't match only once, but every file that does match will be read twice. If you only use sed, every file will be read only once.

Delete Special Word Using sed

I would like to use sed to remove all occurances of this line if and only if it is this
<ab></ab>
If this line, I would not want to delete it
<ab>keyword</ab>
My attempt that's not working:
sed '/<ab></ab>/d'
Thanks for any insight. I'm not sure what's wrong as I should not have to escape anything?
I'm using a shell script named temp to execute this. My command is this:
cat foobar.html | ./temp
This is my temp shell script:
#!/bin/sh
sed -e '/td/!d' | sed '/<ab></ab>/d'
It looks like we have a couple of problems here. The first is with the / in the close-tag. sed uses this to delimit different parts of the command. Fortunately, all we have to do is escape it with \. Try:
sed '/<ab><\/ab>/d'
Here's an example on my machine:
$ cat test
<ab></ab>
<ab></ab>
<ab>test</ab>
$ sed '/<ab><\/ab>/d' test
<ab>test</ab>
$
The other problem is that I'm not sure what the purpose of sed -e '/td/!d' is. In it's default operating mode, you don't need to tell it not to delete something; just tell it exactly what you want to delete.
So, to do this on a file called input.html:
sed '/<ab><\/ab>/d' input.html
Or, to edit the file in-place, you can just do:
sed -i -e '/<ab><\/ab>/d' input.html
Additionally, sed lets you use any character you want as a delimiter; you don't have to use /. So if you'd prefer not to escape your input, you can do:
sed '\#<ab></ab>#d' input.html
Edit
In the comments, you mentioned wanting to delete lines that only contain </ab> and nothing else. To do that, you need to do what's called anchoring the match. The ^ character represents the beginning of the line for anchoring, and $ represents the end of the line.
sed '/^<\/ab>$/d' input.html
This will only match a line that contains (literally) </ab> and nothing else at all, and delete the line. If you want to match lines that contain whitespace too, but no text other than </ab>:
sed '/^[[:blank:]]*<\/ab>[[:blank:]]*$/d' input.html
[[:blank:]]* matches "0 or more whitespace characters" and is called a "POSIX bracket expression".

Inserting a string using Sed that will not evaluate as a command

I'm trying to insert into a text file the string cd $var at the second line using sed, but it doesn't seem to work. I'm using the syntax for inserting a line at a specific line in a file,
sed -i '2icd $var' FILE
The format of which was found as the response to this question:
Insert a line at specific line number with sed or awk
My best guess is that sed is interpreting the command literally and evaluating it instead of copying it in. However, all of my attempts at forcing it to be evaluated simply as a string have failed. My attempts so far:
sed -i '2i\cd $var' FILE
sed -i '2i\cd \$var' FILE
sed -i "2i'cd $var'" FILE
and
Line='cd $var'
sed -i "2i$Line" FILE
I was fairly sure this last attempt would succeed, due to the hard quotes, but it still failed.
In fact, this also failed,
sed -i '2icd' FILE
Yet this succeeded (Just to confirm the general format):
sed -i '2ic' FILE
Just to be clear, all 5 of the failed attempts yielded the same error: A blank line was inserted at the desired location.
sed -i "2 i\\
$var" file
need a escape NewLine normaly after the i and depending the OS/sed a space before and/or after the i also. Finaly, with double quote, escape the \

What does sed -i option do?

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

sed -i option is not working on solaris

I am using sed to replace a line with NULL in a file. The command i used is
sed -i "s/.*shayam.*//g" FILE
This is working fine in linux. shayam is replaced with blank in the FILE. But when i used this in solaris it is showing some error.
sed: illegal option -- i
How to use -i functionality of sed in solaris. Kindly help.
The -i option is GNU-specific. The Solaris version does not support the option.
You will need to install the GNU version, or rename the new file over the old one:
sed 's/.shayam.//g' FILE > FILE.new && mv FILE.new FILE
I just answered a similar question sed -i + what the same option in SOLARIS, but for those who find this thread instead (I saw it in the related thread section):
The main problem I see with most answers given is that it doesn't work if you want to modify multiple files. The answer I gave in the other thread:
It isn't exactly the same as sed -i, but i had a similar issue. You
can do this using perl:
perl -pi -e 's/find/replace/g' file
doing the copy/move only works for single files. if you want to
replace some text across every file in a directory and
sub-directories, you need something which does it in place. you can do
this with perl and find:
find . -exec perl -pi -e 's/find/replace/g' '{}' \;
sed doesn't haven an -i option.
You are probably using some vendor-specific variant of sed. If you want to use the vendor-specific non-standardized extensions of your vendor-specific non-standardized variant of sed, you need to make sure that you install said vendor-specific non-standardized variant and need to make sure that you call it and don't call the standards-compliant version of sed that is part of your operating environment.
Note that as always when using non-standardized vendor-specific extensions, there is absolutely no guarantee that your code will be portable, which is exactly the problem you are seeing.
In this particular case, however, there is a much better solution: use the right tool for the job. sed is a stream editor (that's why it is called "sed"), i.e. it is for editing streams, not files. If you want to edit files, use a file editor, such as ed:
ed FILE <<-HERE
,s/.shayam.//g
w
q
HERE
See also:
Unable to use SED to edit files fast
How can I replace a specific line by line number in a text file?
Either cat the file or try <?
Then pipe (|) the result to a temp file and if all goes well (&&) mv the tempfile to the original file.
Example:
cat my_file | sed '!A!B!' > my_temp_file && mv my_temp_file my_file

Resources