How to display trailing whitespaces in the text file?
gnu cat
has the -E
switch for that so with an input like
printf " one \ntwo \t\n"
it will print
one $
two $
There are many ways to emulate that behaviour e.g. use sed
in "raw" mode:
sed -n l infile
or
paste -d '$' infile /dev/null
or use any text processing tool to add a $
before each newline...
You might also try something like
less -p '[[:blank:]]+$' infile
which will highlight the pattern (in this case the trailing blanks)...
Related videos on Youtube
kenorb
I'm a DevOps guy, also IT engineer programming in anything that has syntax. "Life is about making a big impact in the other people's lives."
Updated on September 18, 2022Comments
-
kenorb over 1 year
I'd like to be able to print and see the trailing whitespaces in the text file (just display, not remove).
I've tried with
cat -v
which suppose to display non-printing characters, but it doesn't display them as expected (no visual difference between the lines). E.g.$ printf "foo\nbar \t\n" > file.txt $ cat -v file.txt foo bar
By print/display whitespaces I mean some visual human indication that the trailing whitespace is there in comparison to other lines without, either by some special character, color (like when files are displayed using
git diff
) or something similar.Is there any other way?
Note: I'm on macOS Sierra, however I've got access to both GNU and BSD
cat
commands:$ type -a cat cat is /usr/local/opt/coreutils/libexec/gnubin/cat cat is /bin/cat $ cat --version cat (GNU coreutils) 8.28 $ /bin/cat --version /bin/cat: illegal option -- - usage: cat [-benstuv] [file ...]
-
kenorb over 6 yearsThanks, seems to work fine, at least better than
-v
.-E
doesn't exist for/bin/cat
, but I've got GNUcat
installed as well. -
Stephen Kitt over 6 years
cat -v
does work, it just doesn’t do what you think it does — trycat -v /dev/urandom | head
to see how it affects output. -
Sanket over 6 years@kenorb
-E
does exist for/bin/cat
, which version are you talking about -
kenorb over 6 years
cat -v
prints non-printing characters fine, however whitespace is still a whitespace, so it's not really human visible in the terminal. -
kenorb over 6 years@Sanket $
/bin/cat -E file.txt
->/bin/cat: illegal option -- E
. -
Sanket over 6 years
# /bin/cat -E firstOne.txt
It outputs the content "kkjkl" my case
-