Sed is a very powerful tool when used correctly but can also be a source of frusteration at times. One problem with it shows up when you are trying to identify and replace non-printable characters. Luckily sed has a nice command line switch that will help you debug certain problems with sed.
When you just use cat to display a file, you’ll see it as you are normally used to but this isn’t how sed sees files.
cat hello.c
When you use the -n ‘l’, you’ll see the file how sed sees it. (It’s a lower case “L” in the single quotes)
sed -n ‘l’ hello.c
Now you can see the new line characters at the end of the lines and the tabs that proceed some of the lines.
If you like the content on this site, please support it by using this link to order from Amazon. You know you were going to go there and buy stuff anyhow so why not help me pay the hosting bill.
another /trick/ you can do with sed which can be quite handy, especially if you are reading a config file.
sed -n p to get a specific line from a file.
for example
config.cfg
nocursor
nomouse
fullscreen
then sed -n 2p config.cfg
will give you nomouse
—
there are other ways to do this as well, such as sourcing a file but this method is quite handy if you have full control of read/writing the file.