The main task: the file has blocks of text beginning with the current date ( date +"%d/%m/%y" ) and ending with an empty line or lines. My final script should erase this block if it matches the current date. I planned to go in a loop. When coinciding with the date - check the box (stop output to a file). If the line is blank and the checkbox is checked, uncheck the checkbox. For now, I was just trying to keep track of empty lines, but it doesn't work.

 #!/bin/bash for line in `cat ./test2`; do if [[ "$line" != "" ]] then echo $line else echo fi done 

View file:

 abc1 abc2 abc3 

Here is how these blank lines get lost in the loop.

  • line feeds are already tracked: you get a line at each iteration of the loop you write. and output you already "with line breaks". the essence of both questions is incomprehensible. and these questions do not correspond to the title (in which we are already talking about empty lines). - aleksandr barakin
  • File type: <br/> abc1 <br/> <br/> abc2 <br/> <br/> abc3 <br/> <br/> These are the empty lines that are lost in the loop. So ... formatting does not work. - keltkelt
  • It is done in one line of sed , do not invent crutches. - 0andriy

3 answers 3

These blank lines are lost in a loop.

because you are design

 for line in `cat ./test2` 

you are not reading the lines, but the entire file as one line, breaking it into a so-called. “Tokens”, that is, groups of characters between which separators are inserted (by default - “whitespace” - spaces, tabs, line breaks), one token at each iteration of the loop.

if you need to read the line at each iteration of the loop, then make it “more natural” for the shell by:

 cat file | while read line; do ... done 
  • Thank you thank you! - keltkelt

Incomprehensible task. You obviously did not want this. But this is not a place for discussion, not a place for personal opinions, here is one question - one answer

1) Print it with all translations

cat file.txt

If it is necessary without separator translations, then:

cat file.txt | grep -v ^$

Understand how to track line feeds in bash

cat file.txt | sed "s.^$.АААААААААА ТУТ БЫЛА ПУСТАЯ СТРОКА.g"

  • If it is necessary without translations, then - and further you propose to print just along with the line breaks. - aleksandr barakin
  • cat file.txt | sed "s.^$.АААААААААА ТУТ БЫЛА ПУСТАЯ СТРОКА.g" cat file.txt | sed "s.^$.АААААААААА ТУТ БЫЛА ПУСТАЯ СТРОКА.g" ”and what does this blank line have to do with line cat file.txt | sed "s.^$.АААААААААА ТУТ БЫЛА ПУСТАЯ СТРОКА.g" (which are after each line)? - aleksandr barakin
  • Clarified. Focused on the author and his question. They didn’t explicitly ask about deletion here - bukkojot
  • Thanks for the answer. The main task: the file has blocks of text beginning with a date and ending with a blank line or lines. My final script should erase this block if it matches the current date. I planned to go in a loop. When coinciding with the date - check the box. With a blank line and a checkbox - remove it. - keltkelt
  • in my opinion, it got worse. What is a "delimiter transfer"? I think you rushed to answer the question (more precisely, a few questions), the essence of which is not clear at the moment. - aleksandr barakin

If the block starts on the date: /^\d{4}\.\d{2}\.\d{2}$/ , and ends with an empty string /^\s*$/ , then you can use the flip-flop operator to remove such blocks:

 $ perl -ne 'print unless /^\d{4}\.\d\d\.\d\d$/../^\s*$/' tests2 

An example .

To delete blocks only with the current date in the format %Y.%m.%d :

 $ perl -MTime::Piece -ne 'BEGIN{$today=localtime->ymd(".")}; print unless ($_ == $date) .. /^\s*$/' test2 

An example .

You can do this using awk :

 awk -v today=$(date +'%Y.%m.%d') '$0 == today,!NF {next} {print}' test2 

An example .

  • Thank you very much! I need to cut with the current date, in bash, as I found out, this is date = eval date +"%d/%m/%y" . And I don’t know a pearl at all ... - keltkelt
  • @keltkelt: updated the response so that only blocks with the current date were deleted - jfs
  • Thank you Yeah, you have to deal with the pearl ... - keltkelt
  • @keltkelt is not necessarily perl, for example, you can use awk. - jfs