I am writing a script that iterates through all the files and replaces line feeds at the beginning of each file.For a file like this,\n\nA line \r\nAnother line \r\n\r\f\n\nFew more lines \r\n\r\nI need to replace all line feeds at the beginning of the file with CRLF, i.e.\r\n\r\nA line \r\nAnother line \r\n\r\f\n\nFew more lines \r\n\r\nI am new to the use of sed. I tried using,sed -i 's/^[\n]/\r\n/' file.txtBut it doesn't seem to work.Edit: I am able to replace a range of lines with,sed '1,2s/^/\r/'But is there a way to identify if the first character in the file is line feed?
Replace LF with CRLF at the beginning of a file
sed
If you have GNU sed, then you can use the special address form 0,/./ to make substitutions only in the portion of a file up to the first non-empty line. To illustrate:~$ cat -e file.txt$$non empty line$non empty line$$$non-empty line$$(the $ signs indicate literal line endings: see man cat); then~$ sed '0,/./ s/^$/\r/' file.txt | cat -e^M$^M$non empty line$non empty line$$$non-empty line$$where the ^M characters indicate the inserted CRs.