I want to remove multiple line of text on linux
sed is for doing s/old/new/ on individual lines, that is all. For anything else you should be using awk:
$ awk -v RS= -v ORS='\n\n' '/[[:alpha:]]/' file
1
19:22
abcde
3
19:24
abbff
4
19:25
abbc
The above is simply this:
RS=
tells awk the input records are separated by blank lines.ORS='\n\n'
tells awk the output records must also be separated by blank lines./[[:alpha:]]/
searches for and prints records that contain alphabetic characters.
Simple way to remove multi-line string using sed
tldr;
I think this is the simplest way, ad no need for the assembly like micro commands
sed '/^tomcat\.util.*$/,/^.*[^\]$/d' /tmp/foobar.txt
which produces
this is line 1
this is line 3
the end leave me
and me
if you wanted to remove the lines in the file rather than spit out the output to stdout then use the inline
flag, so
sed -i '/^tomcat\.util.*$/,/^.*[^\]$/d' /tmp/foobar.txt
So... how does this work?
sed
commands, like vi
commands operate on an address
. Normally we don't specify an address and that simply applies the command to all lines of the file, eg when replacing the
for that
in a file we'd normally do
sed -i 's/the/that/g' /tmp/foobar.txt
ie applying the substitute or s
command to all lines in the file.
In this case you want to delete some lines so we can use the delete or d
command. But we need to tell it where to delete. So we need to give it an address.
The format of a sed
command is
[addr][!]command[options]
(see the docs )
If no address is specified then the command is applied to all lines, if the !
is specified then it is applied to all lines that don't match the pattern. So far so good.
The trick here is that addr
can be a single address or a range of addresses. The address can be a line number or a regex pattern. You use a ,
between two addresses to to specify a range.
so to delete line 5 to 8 inclusive you could do
sed -i '5,8d' /tmp/foobar.txt
in this case rather than knowing the line number we know some "markers" and we can use Regex instead, so the first marker, a line starting with tomcat.util
is found by the regex
/^tomcat\.util.*$/
The second marker is a bit more tricky but if we look we can see that the final line to remove is the first one that does not end with a \
, so we can match a line that consists of "anything but does not end with \
"
/^.*[^\]$/
While the second marker could match a whole bunch of lines if we make a range out of these two regexes, the range means that the second "address" is the first line after the first address that matches the regex.
Putting that all together, we want to delete (d
) all lines in the range from the address that is found by the regex matching a line starting with tomcat.util
and ending with a line that does not end in \
ie
sed '/^tomcat\.util.*$/,/^.*[^\]$/d' /tmp/foobar.txt
hope that helps ;-)
Cheers
Karl
Remove line of text from multiple files in Linux
If your version of sed allows the -i.bak
flag (edit in place):
sed -i.bak '/line of text/d' *
If not, simply put it in a bash loop:
for file in *.txt
do
sed '/line of text/d' "$file" > "$file".new_file.txt
done
delete text on multiple lines between first pair of characters
$ awk '!c && /^>/{f=1; c=1; next} /^>/{f=0} !f' ip.txt
>123
blah bla
blah bla
>456
blah bla
!c && /^>/
ifc
has falsey value (which it is initially) and line starts with>
- set flags
f
andc
(so, this condition will never be satisfied after the first match) next
so that other statements are skipped
- set flags
/^>/{f=0}
clear the flag if another line starts with>
!f
print lines only if flag is not set
If you are okay with perl
and input file is small enough to fit memory, then you can use:
perl -0777 -pe 's/>[^>]+//' ip.txt
this slurps entire input as a single string, so that you can match across lines
This is possible with GNU sed
too (assuming input doesn't have NUL character):
sed -zE 's/>[^>]+//' ip.txt
remove multiple lines from text file using grep
|
is only treated as a a regex character when grep
is working in extended regex mode. So you need to do one of the following:
# Escape the | so that it's treated as a regex control character
grep -v 'error\|fault\|unkownn' input.txt > out.txt
# -E enables extended regex mode
grep -vE 'error|fault|unkownn' input.txt > out.txt
# egrep = grep -E
egrep -v 'error|fault|unkownn' input.txt > out.txt
How to remove multiple lines in multiple files on Linux using bash
He meant removing two adjacent lines.
you can do something like this, remember to backup your files.
find . -name "*.js" | xargs sed -i -e "/^;var/N;/^;var\nO0l='=sTKpUG/d"
Since sed processes input file line by line, it does not store the newline '\n' character in its buffer, so we need to tell it by using flag /N to append the next line, with newline character.
/^;var/N;
Then we do our pattern searching and deleting.
/^;var\nO0l='=sTKpUG/d
Related Topics
Passing Main Script Variables into Perl Modules
Graphical Diff Programs for Linux
I Want to Contribute to the Linux Kernel
How to Resume Interrupted Download Automatically in Curl
Remove the Last Page of a PDF File Using PDFtk
Linux: Screen Desktop Video Capture Over Network, and Vnc Framerate
How to Convert Pe(Portable Executable) Format to Elf in Linux
Convert a Fixed Width File from Text to CSV
Running Apt-Get for Another Partition/Directory
Why Do I Get 'Permission Denied' After Using ./File2 in Linux
How to Measure Separate CPU Core Usage for a Process
Skipping Acquire of Configured File 'Main/Binary-I386/Packages'
Adding Any Current Directory './' to the Search Path in Linux
Listen on a Network Port and Save Data to a Text File
How to Install Maven into Red Hat Enterprise Linux 6