onlyforbopi
5/15/2017 - 10:45 AM

GENERAL HOW TO

GENERAL HOW TO

#Delete/Replace a Carriage Return (CR) with sed
#There are a few ways to remove or replace a carriage return with sed.

sed 's/.$/<replace with text>/'
sed 's/\x0D$/<replace with text>/'
sed 's/\r/<replace with text>/'

#The first method assumes all lines end with CRLF, while the 
#second and third method depend on the version of sed.

################

#Delete/Replace a Linefeed (LF) with sed
#By default, sed strips the newline as the line is placed into the pattern space. 
#However, the workaround is to read the whole file into a loop first.

sed ':a;N;$!ba;s/\n/<text>/g'

#where

:a       - create a label 'a'
N        - append the next line to the pattern space
$!       - if not the last line
ba       - branch (go to) label 'a'
s        - substitute
/\n/     - regex for new line
/<text>/ - with text "<text>"
g        - global match (as many times as it can)

##############

#Example: Delete Newlines (CRLF) with sed

Delete the carriage return first, and then delete the linefeed.

# Delete carriage return
sed -i 's/\r//g' example.txt

# Delete all LF but last (it will turn separate lines into one)
sed -i ':a;N;$!ba;s/\n/<text>/g' example.txt

##############

#tr: A Simpler Way to Replace or Delete a Newline (CRLF)
#The tr utility is a preferred and simpler method for 
#replacing end of line characters as the pattern buffer 
#is not limited like in sed.

tr '\r\n' '<replace with text>' < input_file > output_file

#or to delete the newline (CRLF),

tr -d '\r\n' < input_file > output_file

#where

#\r - carriage return
#\n - linefeed
#Delete All Lines Before a Keyword with sed
#A range from the “KEYWORD” to the last line in the input stream can be located and not deleted it with following command,

sed '/KEYWORD/,$!d'

#where

#,    - defines a range
#$    - special character representing the last line in the input stream
#!    - NOT
#d    - delete command


###########################


#Delete All Lines After a Keyword with sed
#After finding a line containing the “KEYWORD” the stream can be quit, with the following command,

sed '/KEYWORD/q'

#where

#q    - quit

######################################

#Delete All Lines Including the Keyword and After the Keyword with sed
#A range from the “KEYWORD” to the last line in the input stream can be located and deleted by,

sed '/KEYWORD/,$d'

#where

#,    - defines a range
#$    - special character representing the last line in the input stream
#d    - delete command

1. CRLF.bash        Delete / Replace CRLF or LF with sed / tr
2. KeywordDel.bash  Delete all lines before or after a keyword