Удалить пустую строку linux

Delete empty lines using sed

You may have spaces or tabs in your «empty» line. Use POSIX classes with sed to remove all lines containing only whitespace:

A shorter version that uses ERE, for example with gnu sed:

(Note that sed does NOT support PCRE.)

@BernieReiter ^\s*$ will match all «empty» lines, empty here means, the line contains no chars, or the line contains only empty strings (E.g. spaces). All matched lines will be removed by sed, with the d command.

I am missing the awk solution:

How does this work? Since NF stands for «number of fields», those lines being empty have 0 fields, so that awk evaluates 0 to False and no line is printed; however, if there is at least one field, the evaluation is True and makes awk perform its default action: print the current line.

sed

grep

awk

These show up correctly in your online tool, but [] should not be escaped in a bracket expression, so the code here isn’t correct for \[\[:space:\]\] or \[ \t\] — should be [[:space:]] and [ \t] .

@BenjaminW. Thanks for catching that. Those were not from the original author, but came from Edit 3 when it was changed from regular text to «code», which then «exposed» the `\` escaping. I have fixed them now.

sed ‘/^$/d’ should be fine, are you expecting to modify the file in place? If so you should use the -i flag.

Maybe those lines are not empty, so if that’s the case, look at this question Remove empty lines from txtfiles, remove spaces from start and end of line I believe that’s what you’re trying to achieve.

I believe this is the easiest and fastest one:

If you need to ignore all white-space lines as well then try this:

s="\ \ a\ b\ \ Below is TAB:\ \ Below is space:\ \ c\ \ "; echo "$s" | grep . | wc -l; echo "$s" | grep '\S' | wc -l 

Yes, I know, but the initial question did not mention whether the source is a file or something else, so the solution is what comes after «|», and before it just an example of a source. Simply to distinguish the solution from the source of lines.

grep ‘\S’ is definitely not portable. If you have grep -P then you can use grep -P ‘\S’ but it’s not supported on all platforms, either.

The downside of grep . compared to the other solutions is that it will highlight all the text in red. The other solutions can preserve the original colors. Compare unbuffer apt search foo | grep . to unbuffer apt search foo | grep -v ^$

@wisbucky grep does not default to color output, but often it’s enable via a shell alias or environment variable. Use grep —color=never . to override.

Читайте также:  Переименовать файл через консоль linux

Another option without sed , awk , perl , etc

strings — print the strings of printable characters in files.

«For each file given, GNU strings prints the printable character sequences that are at least 4 characters long. » so very short lines might give you a surprise if you’re unaware of this. There is a —bytes=min-len option to allow shorter lines.

With help from the accepted answer here and the accepted answer above, I have used:

$ sed 's/^ *//; s/ *$//; /^$/d; /^\s*$/d' file.txt > output.txt `s/^ *//` => left trim `s/ *$//` => right trim `/^$/d` => remove empty line `/^\s*$/d` => delete lines which may contain white space 

This covers all the bases and works perfectly for my needs. Kudos to the original posters @Kent and @kev

The command you are trying is correct, just use -E flag with it.

-E flag makes sed catch extended regular expressions. More info here

sed -n '/ / p' filename #there is a space between '//' 

You are most likely seeing the unexpected behavior because your text file was created on Windows, so the end of line sequence is \r\n . You can use dos2unix to convert it to a UNIX style text file before running sed or use

to remove blank lines whether or not the carriage return is there.

Hi, what is the -r flag doing and is it possible to combine it with -i to modify the file directly and avoid printing to screen. In addition, I think that this command would also work as sed -r «/^\r$/d»

This works in awk as well.

awk '!/^$/' file xxxxxx yyyyyy zzzzzz 

You can do something like that using «grep», too:

My bash -specific answer is to recommend using perl substitution operator with the global pattern g flag for this, as follows:

$ perl -pe s'/^\n|^[\ ]*\n//g' $file xxxxxx yyyyyy zzzzzz 

This answer illustrates accounting for whether or not the empty lines have spaces in them ( [\ ]* ), as well as using | to separate multiple search terms/fields. Tested on macOS High Sierra and CentOS 6/7.

FYI, the OP’s original code sed ‘/^$/d’ $file works just fine in bash Terminal on macOS High Sierra and CentOS 6/7 Linux at a high-performance supercomputing cluster.

Источник

Remove blank lines with grep

The single quotes around ^$ makes it work for Cshell. Other shells will be happy with either single or double quotes.

UPDATE: This works for me for a file with blank lines or «all white space» (such as windows lines with \r\n style line endings), whereas the above only removes files with blank lines and unix style line endings:

That egrep would only work for files with zero or 1 space on the line, not for files with 2 or more spaces. Change ? to *.

grep -v -e ‘^[[:space:]]*$’ -e ‘^#’ file will give you all non-blank, non-comment lines in a script or config file (or any file type that uses the hash character for comments).

Читайте также:  Linux kernel driver support

«The -e option allows regex patterns for matching.» That is very misleading. -e is a (POSIX-)definition for: This can be used to specify multiple search patterns, or to protect a pattern beginning with a hyphen (-). (from the manual). Grep already expects a (basic) regular expression by default. For this pattern, you may leave out -e entirely: grep -v ‘^[[:space:]]*$’ foo.txt .

If you’re dealing with files that might have windows-style CR+LF line breaks but don’t want to also exclude lines with other whitespace, then use this regex: ‘^[[:cnrl:]]?$’ .

This works for me on files from a linux based system but not on files from Windows. Presumably because of Windows line-ending characters.

I’m upvoting this even though it doesn’t quite solve the OP’s problem of handling a file with Windows line endings, but since I don’t have that issue, this turned out to be the perfect solution for me.

$ dos2unix file $ grep -v "^$" file 

If you don’t have dos2unix, then you can use tools like tr:

Good point about converting to UNIX-style line endings otherwise regular expressions may not work as expected. Nothing here worked for me until I converted the line endings.

grep -v "^[[:space:]]*$" The -v makes it print lines that do not completely match ===Each part explained=== ^ match start of line [[:space:]] match whitespace- spaces, tabs, carriage returns, etc. * previous match (whitespace) may exist from 0 to infinite times $ match end of line 
$ echo " > hello > > ok" | > grep -v "^[[:space:]]*$" hello ok 

To understand more about how/why this works, I recommend reading up on regular expressions. http://www.regular-expressions.info/tutorial.html

How and why does this work? Your answer would be much better if you could explain. For instance your regular expression matches the beginning of the string then one or more spaces using the POSIX standard then the end of the string, i.e. with grep -v it removes all lines that are only spaces. Right? What happens if there are no spaces; it’s simply a newline character?

As my example shows, even only an empty line is removed (the first line). I added more information, so hopefully that helps. 🙂

If you have sequences of multiple blank lines in a row, and would like only one blank line per sequence, try

grep -v "unwantedThing" foo.txt | cat -s 

cat -s suppresses repeated empty output lines.

Your output would go from

The three blank lines in the original output would be compressed or «squeezed» into one blank line.

The same as the previous answers:

Here, grep -e means the extended version of grep. ‘^$’ means that there isn’t any character between ^(Start of line) and $(end of line). ‘^’ and ‘$’ are regex characters.

So the command grep -v will print all the lines that do not match this pattern (No characters between ^ and $).

This way, empty blank lines are eliminated.

-e does not mean «the extended version of grep», maybe you are confused with -E ? The manual clearly says that -e just explicitly says that a pattern follows. Since the pattern does not start with a dash, and you are only defining one pattern anyway, you might as well leave it out as by default grep expects one regex pattern: grep -v ‘^$’ foo.txt (no need for extended regex functionality). Also it is worth mentioning that this does not eliminate the blank lines in the file, only that which is piped through the output. For that case, sed -i would be the right tool.

Читайте также:  Ставить линукс на флешку

Do lines in the file have whitespace characters?

I prefer using egrep , though in my test with a genuine file with blank line your approach worked fine (though without quotation marks in my test). This worked too:

This code removes blank lines and lines that start with «#»

 grep -v "^#" file.txt | grep -v ^[[:space:]]*$ 

Источник

Как удалить пустые строки в файле в Linux

Favorite

Добавить в избранное

Главное меню » Как удалить пустые строки в файле в Linux

Bash Heredoc

П устые строки не всегда желательны, и вы можете почувствовать необходимость их удалить и оставить только те строки, которые содержат текст. Linux предлагает несколько выражений манипулирования текстом, которые вы можете использовать для опускания или удаления пустых строк. Давайте рассмотрим некоторые инструменты командной строки, которые вы можете использовать для удаления пустых строк в текстовом файле.

Мы использовали CentOS 8 в демонстрационных целях.

Удалите пустые строки с помощью команды grep

Grep — один из самых мощных и универсальных инструментов, который может помочь вам удалить ненужные пустые строки в ваших текстовых файлах. Обычно команда grep используется для проверки строк или шаблонов символов в текстовом файле, но, как вы вскоре увидите, она также может помочь вам избавиться от нежелательных пустых строк.

При использовании с параметром -v команда grep помогает удалить пустые строки. Ниже представлен образец текстового файла sample.txt с альтернативными непустыми и пустыми строками.

Чтобы удалить или удалить все пустые строки в образце текстового файла, используйте команду grep, как показано.

Кроме того, вы можете использовать следующий синтаксис.

Более того, вы можете сохранить или перенаправить вывод в другой файл, используя, например, оператор «больше» (>).

$ grep -v ‘^$’ sample.txt > output.txt

Удалите пустые строки с помощью команды sed

Сокращенная как редактор потока, команда sed в Linux — популярный инструмент, который выполняет широкий спектр функций, включая замену строк в файле.

Более того, вы также можете использовать sed для удаления пустых строк в файле, как показано ниже.

Удалите пустые строки с помощью команды awk

Наконец, у нас есть команда awk. Это еще один инструмент командной строки для управления текстовыми сообщениями, который также может избавиться от пустых строк. Чтобы удалить пустой файл с помощью awk, выполните команду ниже.

Заключение

Мы предоставили 3 способа, которые могут быть полезны для удаления пустых строк в текстовых файлах. Есть ли другие идеи о том, как удалить эти ненужные пустые строки? Не стесняйтесь связаться с нами в разделе комментариев.

Если вы нашли ошибку, пожалуйста, выделите фрагмент текста и нажмите Ctrl+Enter.

Источник

Оцените статью
Adblock
detector