Want line terminators in input text to just Go Away? Ruby has a mode for that!
Episode Script
In a previous episode we used Ruby’s -n
mode with autosplit and an explicit record separator to sum up CSV data in a one-liner.
ruby -F',' -nae 'BEGIN{total=0}; total+=$F[4].to_i; END{puts total}' snapshot.csv
One complication we saw in that episode was that with a record separator of just a comma, we wound up with CRLF sequences at the end of the last field in each row.
ruby -F',' -nae 'p $F' snapshot.csv
We dealt with that by adding to the field separation regular expression.
ruby -F',|\r\n' -nae 'p $F' snapshot.csv
But there’s another way to handle this problem.
We can enable Ruby’s line-ending processing mode with -l
.
ruby -F',' -nale 'p $F' snapshot.csv
This automatically strips off the line-ending characters as records are read in.
It feels good to go back to specifying a simple comma as the delimiter for comma-separated values.
In addition to automatically lopping off line-ending characters, line-ending mode also alters the output record-separator to match the input-record separator global variable. But these variables are a topic unto themselves, and we’ll talk about them in a separate video. For now, enjoy the ability to ignore line terminators in input lines. Happy hacking!