Tag Info

New answers tagged

0

I ended up using gsar as described in this answer like this: gsar -F '-s>:x0A' '-r>'


1

$'\n' is basically the same as typing Enter. So in your Perl example, it turns into* if(/ab cd/) Which isn't what you wanted. Reasons I can think of to use $'...': You want to put a single quote inside a single-quote delimited string, e.g. logger $'Can\'t open file' To make non-printable characters clearer, e.g. $'a\tb' rather than 'a<TAB ...


1

You use $'...' when you want escape sequences to be interpreted by the shell. $ echo 'a\nb' a\nb $ echo $'a\nb' a b In perl, -e option get a string. If you use $'...', the escape sequences in string are interpreted before passing to perl. In your case, \r had gone and never passed to perl. With $'...': $ perl -MO=Deparse -w -e $'binmode STDIN;undef ...


3

You can use the read built-in to accomplish this: $ IFS='' read -d '' -r foo < <(echo bar) $ echo "<$foo>" <bar > For a script to read STDIN, it'd simply be: IFS='' read -d '' -r foo   I'm not sure what shells this will work in though. But works fine in both bash and zsh.


1

You can do like: input | { var=$(sed '$s/$/./'); var=${var%.}; } Whatever you do $var disappears as soon as you step outside of that { current shell ; } grouping anyway. But it could also work like: var=$(input | sed '$s/$/./'); var=${var%.}


3

The trailing newlines are stripped before the value is stored in the variable. You may want to do something like: var=`cat; echo x` and use ${var%x} instead of $var. For instance: printf "%s" "${var%x}" Note that this solves the trailing newlines issue, but not the null byte one (if standard input is not text), since according to POSIX command ...


0

A portable shell function that will do this: u2dos() (set -f; IFS=' '; printf '%s\r\n' $(cat "$1")) With that you can do: u2dos file >dosfile


0

In awk you can try awk '{print $0 "\r"}' Or awk -v r=$'\r' '{print $0 r}' The $'\r' is an example of ANSI-C style quoting. It offers a general way to express weird characters, try this, for example: awk -v r=$'\U1F608' '{print $0 r}'


8

You can use unix2dos (which found on Debian): unix2dos file Note that this implementation won't insert a CR before every LF, only before those LFs that are not already preceded by one (and only one) CR and will skip binary files (those that contain byte values in the 0x0 -> 0x1f range other than LF, FF, TAB or CR). or use sed: sed "s/$/$(printf '\r')/" ...


4

This is exactly what unix2dos does: $ unix2dos file.txt That will replace file.txt in-place with a version with CRLF line endings. If you want to do it with sed, you can insert a carriage return at the end of every line: sed -e 's/$/\r/' file.txt This replaces (s) the zero-size area right before the end of the line ($) with \r. To do in-place ...



Top 50 recent answers are included