How to remove the multiple lines in the file using sed - linux

The test.txt file contains 3 lines:
STREET=main
PHONE=123
EMAIL=abc#xyz.com
To remove two lines with the STREET and EMAIL I run the sed twice in a row:
sed -i -- 's/STREET=.*//' test.txt
sed -i -- 's/EMAIL=.*//' test.txt
Instead of using the sed command twice I would rather remove both lines with a single sed command. How to do it?

To delete (d) lines which contain STREET= or EMAIL=.
sed -i -- '/STREET=/d; /EMAIL=/d' file

sed -i -- 's/STREET=.*//;s/EMAIL=.*//' test.txt

The following sed one-liners show how to remove/empty the target lines:
Empty the target lines:
kent$ sed 's/^\(EMAIL\|STREET\)=.*//' file
PHONE=123
Remove the target lines:
kent$ sed '/^\(EMAIL\|STREET\)=/d' file
PHONE=123
Using pattern ^\(EMAIL\|STREET\)= will avoid to touch lines like USER_EMAIL=... or SOME_STREET=

Related

Sed with tab and negate match

I have the following file
a
b c d e
all are tab separated.
my output should be the following
a,
b,c,d,e
i was trying following methods.
sed 's/[[:space:]]*/,/g' input.txt
this gives me the following
,a,
,b,c,d,e,
How should i remove the first comma of both lines. so the output should be like this
a,
b,c,d,e,
managed to do the following but with two sed commands.
cat input.txt | sed 's/[[:space:]]*/,/g' | sed 's/^,//' (sorry useless use of cat)
Is there a better way to do this with one sed command. (i'm not looking for awk/tr or any other method, only sed )
Add condition that there are some letters before the space:
sed 's/\([a-z]\+\)[[:space:]]*/\1,/g' input.txt
Chain two sed rules in one command (this way may be more readable):
sed 's/[[:space:]]*/,/g;s/^,//' input.txt
You could try the below sed command,
$ sed ':a;N;$!ba;s/\n/,\n/g;s/ \+/,/g' file
a,
b,c,d,e
OR
sed ':a;N;$!ba;s/\n/,\n/g;s/[[:blank:]]\+/,/g' file

How to remove lines from text file not starting with certain characters (sed or grep)

How do I delete all lines in a text file which do not start with the characters #, & or *? I'm looking for a solution using sed or grep.
Deleting lines:
With grep
From http://lowfatlinux.com/linux-grep.html :
The grep command selects and prints lines from a file (or a bunch of files) that match a pattern.
I think you can do something like this:
grep -v '^[\#\&\*]' yourFile.txt > output.txt
You can also use sed to do the same thing (check http://lowfatlinux.com/linux-sed.html ):
sed '^[\#\&\*]/d' yourFile.txt > output.txt
It's up to you to decide
Filtering lines:
My mistake, I understood you wanted to delete the lines. But if you want to "delete" all other lines (or filter the lines starting with the specified characters), then grep is the way to go:
grep '^[\#\&\*]' yourFile.txt > output.txt
sed -n '/^[#&*].*/p' input.txt > output.txt
this should work.
sed -ni '/^[#&*].*/p' input.txt
this one will edit the input file directly, be careful +
egrep '^(&|#|\*)' input.txt > output.txt

How to delete lines from file with sed\awk?

I have file, with lines, contains ip with netmask
a.b.c.d/24
w.x.y.z/32
etc
How to delete delete specific row?
i'm using
sed -ie "s#a.b.c.d/24##g" %filname%
but after the removal is an empty string in file.
It should run inside a script, with ip as parameter and also work in freebsd under sh.
Sed solution
sed -i '/<pattern-to-match-with-proper-escape>/d' data.txt
-i option will change the original file.
Awk solution
awk '!/<pattern-to-match-with-proper-escape>/' data.txt
Using sed:
sed -i '\|a.b.c.d/24|d' file
Command line arg:
For the input being command line argument, say 1st argument($1):
sed -i "\|$1|d" file
Replace $1 with appropriate argument number as is your case.
You should use d (delete) not g. Also do not use s (replacement).
sed -ie '/a.b.c.d\/24/d' %filename%
In a script you should using it in this way
IP=$1
IPA=${IP////\\/}
sed -i /"${IPA}"/d %filename%
And the script parameter should be called in this way:
./script.sh a.b.c.d/24
perl -i -lne 'print unless(/a.b.c.d\/24/)' your_file
or in awk if you donot want to do inplace editing:
awk '$0!~/a.b.c.d\/24/' your_file

What is the command to delete empty lines in a file using sed?

What is the sed command to delete empty lines in a file.
What is the command (sed command?) to delete empty files in a folder?
You have to 'd' which is used to delete.
Ex:
sed -i '/^$/d' test
-i - is used to affect the file.
^ - is a beginning of line
$ - is a end of line
d - delete if there is a empty line
I hope this will help you.
/^$/d should do it.
for example
sed -i -e "/^$/d" myfile.txt
if you want to do this to all the files in a folder:
sed -i -e "/^$/d" *
-i means "edit in place" without it, the file will be edited and sent to standard output. The original file will be unmodified.
sed -e 's/#.*//;/^\s*$/d' /etc/lvm/lvm.conf

Delete whitespace in each begin of line of file, using bash

How i can delete whitespace in each line of file, using bash
For instance, file1.txt. Before:
gg g
gg g
t ttt
after:
gg g
gg g
t ttt
sed -i 's/ //g' your_file will do it, modifying the file inplace.
To delete only the whitespaces at the beginning of one single line, use sed -i 's/^ *//' your_file
In the first expression, we replace all spaces with nothing.
In the second one, we replace at the beginning using the ^ keyword
tr(delete all whitespaces):
$ tr -d ' ' <input.txt >output.txt
$ mv output.txt input.txt
sed(delete leading whitespaces)
$ sed -i 's/^ *//' input.txt
use can use perl -i for in place replacement.
perl -p -e 's/^ *//' file
To delete the white spaces before start of the line if the pattern matches. Use the following command.
For example your foo.in has pattern like this
This is a test
Lolll
blaahhh
This is a testtt
After issuing following command
sed -e '/This/s/ *//' < foo.in > foo.out
The foo.out will be
This is a test
Lolll
blaahhh
This is a testtt
"Whitespace" can include both spaces AND tabs. The solutions presented to date will only match and operate successfully on spaces; they will fail if the whitespace takes the form of a tab.
The below has been tested on the OP's specimen data set with both spaces AND tabs, matching successfully & operating on both:
sed 's/^[[:blank:]]*//g' yourFile
After testing, supply the -i switch to sed to make the changes persistent-

Resources