Search code examples
shellkshcarriage-return

Deleting lines beginning with a CR in a file directly


I want to write a ksh script delete all lines of a file beginning by a carriage return. I want to specify that in the same script I want to reuse the modified file so I need to do the modification directly in the file.

For example here is my file in Notepad ++ (with the carriage return shown as CRLF as its a Windows format file):

CE1;CPr1;CRLF
CE2;CPr2;CRLF
CRLF
CE3;CPr3;CRLF
CRLF
CRLF

and I want to obtain:

CE1;CPr1;CRLF
CE2;CPr2;CRLF
CE3;CPr3;CRLF

The script I wrote so far is:

sed -i '/^\n/d' ListeTable.lst

I also tried with \r and \R but nothing is working.

As I specify there is a following script that reuse the modified file that looks like (but there is more):

echo -n "(CE = '$(tail -n 1 ListeTable.lst | cut -d$';' -f1)'and CPr = '$(tail -n 1 ListeTable.lst | cut -d$';' -f2)')"

Solution

  • Ok, so I found a regex that works for this problem : '/^\s*$/d' (\s = match any whitespace character (newlines, spaces, tabs); * = the character may repeat any times or be absent; $ = to the end of the last \s character found)

    So the working code is : sed -i '/^\s*$/d' ListeTable.lst