Unix tricks - Useful but unusual

By Sandra Henry-Stocker  12 comments

Let's explore a few unusual Unix tricks that you might not have seen before.

First, let's look at the paste command. It's more or less the opposite of the cut command which you have probably seen or used to slice some text from its input. The paste command, on the other hand, splices together input from multiple files, lining the text from each source in a side-by-side fashion. Let's look at an example.

If you have three files, each with a small amount of text on each line, for example, you can paste the files together like this:

boson> paste file1 file2 file3
alligators       1,345   xenops
bears    2,890   yaks
cats     3,154   zebras

The output of paste will be more or less columnar, depending on the width of each file's data. Of course, if you want to stash the output in a fourth file, you only have to redirect it.

boson> paste file1 file2 file3 > file4

Another useful "trick" is to make use of the options that come with the cat command to display unusual data in files. The -v option, for example, will display unprintable characters. If you have octal 13 characters in your file, for example, cat -v will display them as ^K characters and octal 7s as ^G.

boson> cat -v file4
A hen is only an egg's way of making another egg.
        Samuel Butler
^G^G^G^G^G
-----
boson> cat file4
A hen is only an egg's way of making another egg.
        Samuel Butler

-----

They would otherwise appear as a blank line at the end of the file.

If you use a -e (-ve on Solaris), line endings will be displayed with a $ as shown here:

boson> cat -ve file4
A hen is only an egg's way of making another egg.$
        Samuel Butler$
^G^G^G^G^G$

A -t (-vt on Solaris) will display tabs as ^I characters.

boson> cat -vt file4
A hen is only an egg's way of making another egg.
^ISamuel Butler
^G^G^G^G^G

And, of course, you can combine these options if you like:

boson> cat -vet file4
A hen is only an egg's way of making another egg.$
^ISamuel Butler$
^G^G^G^G^G$
-----$

One more. There are numerous ways to omit blank lines from text displays or to remove them from files. I've often used the beginning of line and end of line markers together like this "^$" to indicate a line with no content.

grep -v "^$" file
grep -v "^$" file > newfile

An even easier way to do the same thing is to match on any character like this:

cat file | grep .
cat file | grep . > newfile

Better yet, you can avoid the pipe and save a few CPU cycles:

grep . file

The "." will match on any character (but not linefeeds), so any line containing any type of content will be displayed. Plus, if you only want to see lines with more than one character or at least three characters, you can expand your matching string to "..", "..." and so on.

12 comments

    Anonymous 45 weeks ago
    So many people typecat file
    fsteves
    fsteves 45 weeks ago in reply to Anonymous
    It was a standard practice in the past. Now, it is just as acceptable for small operations, but against larger files, it takes much longer due to the processing of the ENTIRE file once by cat, then reprocessing the STDIN from the pipe to grep. If I am searching short phrases on the command line, I still cat
    Anonymous 45 weeks ago in reply to Anonymous
    Because, it's a consistency thing. In general you can do:a
    Sandra Henry-Stocker
    Sandra Henry-Stocker TRUSTED USER 2 years ago in reply to Anonymous
    Old habits die hard.
    Anonymous 2 years ago in reply to Anonymous
    If you do this in a shell script, you'll be repeatedly starting an extra process for no reason at all.
    Anonymous 51 weeks ago
    In the past, while setting up servers, I constantly need to use Google for unix command. Due to time constraint, I had no time to digest the unix command I used, hence forgetting all those command. After getting useful Unix resources (like one of yours), I got small driblet of unix tips/hints from where I can learn on my leisure time - giving it a chance to stay in my memory for longer. Keep up the good work. Cheersweb development
    Anonymous 1 year ago
    If you do this in a shell script, you'll be repeatedly starting an extra process for no reason at all. loan problems
    Anonymous 2 years ago
    UGG Boots is one of the most popular boots in the fashion realm, and it also the must-have item in the cold winter to keep your foot warm, have a pair of sheepskin UGG Boots as the gift also the great point, so, why you still hesitate, follow me!
    Anonymous 2 years ago
    I have used the "grep ." command before and coincidentally, it was to eliminate some extraneous blank lines from a file that was creating issues with a poorly written file parser; the parser was choking on non-printable characters. I was using vi to check for those non-printables, but using cat is much simpler. Thanks...
    Anonymous 2 years ago
    The idea is not to "find" something in the file, rather to omit what you don't want. In this example the idea is to see everything that is 'not' a blank line.
    Anonymous 2 years ago
    It just goes to show you that in Unix, there is more than one way to skin a "cat".
    Anonymous 2 years ago in reply to Anonymous
    When you start dog training, you need know how to train a puppy can use dog behavior training and dog training tips.

      Add a comment

      Post a comment using one of these accounts
      Or join now
      At least 6 characters

      Note: Comment will appear soon after you have activated your account.
      Obscene/spam comments will be removed and accounts suspended.
      The information you submit is subject to our Privacy Policy and Terms of Service.

      ITworld LIVE

      IT Management/StrategyWhite Papers & Webcasts

      White Paper

      Evaluator Group: Storage Federation - IT Without Limits (Analysis of HP Peer Motion with Storage Federation)

      As the role of IT increases within organizations, the need to move data when and where it is needed is critical to support emerging business requirements. This has become increasingly difficult due to the huge growth of data volumes. This white paper sponsored by HP + Intel evaluates a solution that aims to enable the movement of data without physical limitations. Read now and see how this could enable agility and efficiency.

      White Paper

      ESG Lab Validation Report: HP Data Protector & Deduplication Solutions

      Many organizations have deployed disk-to-disk backup technologies to improve the speed and reliability of their backup and disaster recovery operations. A growing number of these now look to data deduplication to enhance retention periods and reduce costs. This ESG Lab Validation Report sponsored by HP + Intel examines a number of backup and recovery solutions and evaluates their ease of implementation as well as their ability to improve reliability and reduce costs.

      White Paper

      Business Value of Blade

      The nature of the blade platform makes system management, monitoring and provisioning easy and efficient. Access this resource to learn how blade migration will save your data center time and money while increasing performance.

      White Paper

      Accelerate time to application value

      For your IT organization to keep pace with the business, you need a new, faster approach to infrastructure deployment-an approach that increases agility and accelerates time to application value. That's HP Converged Systems. Built on Converged Infrastructure, these systems deliver the industry's first portfolio of pre-integrated, tested, and optimized infrastructure solutions for applications running in virtual, cloud, dedicated, or hybrid environments.

      White Paper

      Converged Infrastructure for Dummies

      As you know, everything is mobile, connected, interactive, and immediate. This is exactly why organizations need a highly agile IT infrastructure in order to keep pace with extreme fluctuations in business demand. This book will help you understand why infrastructure convergence has been widely accepted as the optimal approach for simplifying and accelerating your IT to deliver services at the speed of business while also shifting significantly more IT resources from operations to innovation.

      See more White Papers | Webcasts

      Ask a question

      Ask a Question