• You are not authorized to post comments.
  • You are not authorized to post comments.

Unix tricks - Useful but unusual

By Sandra Henry-Stocker  Add a new comment

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.

ITworld LIVE

IT Management/StrategyWhite Papers & Webcasts

White Paper

The Cloud: Reinventing Enterprise Collaboration

Collaboration and content sharing are not, of course, new concepts. But cloud computing has changed the nature of collaboration, content sharing, document storage and project management to enable more efficient, faster-acting and cost-effective enterprises. According to a new study by IDG Research, the vast majority of knowledge workers (86%) placed a very high level of importance on collaborating with internal coworkers and external stakeholders, and having access to the most up-to-date corporate information. Read how organizations are realizing massive productivity gains by transitioning their content management solutions to cloud-based models.

White Paper

Empowering Your Mobile Worker

Today's most productive employees are mobile, and your company's IT strategy must be ready to support them with 24/7 access to the business information they need across a range of mobile devices.See how corporations are meeting the many needs of their mobile workers with the help of Box.

White Paper

Market Landscape Report: Online File Sharing and Collaboration in the Enterprise

The trend toward "consumerization" marches onward in IT; more and more end-users are choosing their own hardware plaforms and software applications in lieu of the IT-sanctioned business tools provided by their companies. These end-users are looking to tackle issues like data sharing, portability, and access from multiple intelligent endpoint devices, creating a conundrum for IT as it needs to balance business enablement, ease of access, and collaborative capacity with the need to maintain control and security of information assets. This need for balance is one of the drivers of the fast growing online file sharing and collaboration segment of the SaaS market. This paper examines the market drivers, inhibitors, and top vendors in this segment, including Box, Citrix Sharefile, Dropbox, Egnyte, Nomadesk, Sugarsync, Syncplicity and YouSendIt.

White Paper

Sharing Simplified - Consolidating File-sharing Technologies

Employees need to share content with colleagues within their organization and outside. Yet, ECMs make it hard to share content within a business and impossible between organizations. Read how one company consolidated multiple file sharing technologies to increase productivity and reduce complexity.

White Paper

Content Sharing 2.0: The Road Ahead

A growing number of companies are taking advantage of the natural synergies that exist between cloud-based IT services and content access and sharing. Legacy content management and collaboration systems simply weren't designed to meet the evolving requirements of today's IT and business managers, as well as the needs of content users. Box provides cloud-based content storage, access and collaboration services that require virtually no user training and supports file access and delivery on almost all popular PC and mobile devices. Read how Box let companies rapidly implement a cost-effective and secure content storage and sharing system that can easily expand to accommodate any size and number of files.

See more White Papers | Webcasts

Ask a question

Ask a Question