Tag Info

New answers tagged

7

Use recode, e.g.: recode /cr file Note: the fact that you can see the contents in the terminal with cat file is that the Mac end-of-line is CR, which puts the cursor at the beginning of the line without going to the next line, so that everything gets overwritten.


2

If you want an exact equivalent to chomp, the first method that comes to my mind is the awk solution that LatinSuD already posted. I'll add some other methods that don't implement chomp but implement some common tasks that chomp is often used for. When you stuff some text into a variable, all newlines at the end are stripped. So all these commands produce ...


1

Since you already have your portable sed answer, I'll just mention that sed is rarely the best tool if you need to somehow manipulate newlines. Perl is very nearly as portable as sed, is installed by default on most *nix systems and can deal with this very easily: echo "aa\nbb" | perl -pe 's/\\n/\n/g' Another very portable choice is awk: echo "aa\nbb" | ...


7

sed 's/\\n/\ /g' Notice the backslash just before hitting return in the replacement string.


3

You can use perl without chomp: $ printf "one\ntwo\n" | perl -0 -pe 's/\n\Z//'; echo " done" one two done $ printf "one\ntwo" | perl -0 -pe 's/\n\Z//'; echo " done" one two done But why not use chomp itself: $ printf "one\ntwo\n" | perl -pe 'chomp if eof'; echo " done"


5

This should work: printf "one\ntwo\n" | awk 'NR>1{print PREV} {PREV=$0} END{printf("%s",$0)}' ; echo " done" The script always prints previous line instead of current, and the last line is treated differently. What it does in more detail: NR>1{print PREV} Print previous line (except the first time). {PREV=$0} Stores current line in PREV variable. ...


0

A POSIX portable means of viewing non-printable characters in a text file might be: sed -n l <file Besides being portable, sed will also default to printing the line twice if you eschew the -n - once with nonprintable characters represented with C-style or octal escapes immediately followed by another printing of the line as it would normally display. ...


2

You can try: open my $fh, '>', 'test.txt' or die "$!"; binmode $fh; print $fh "QWERTY\n"; You only see $ in vim because by default, listchars for end of line only contains $. From :help listchars: 'listchars' 'lcs' string (default "eol:$") global {not in Vi} Strings to use in 'list' ...



Top 50 recent answers are included