DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Reproducible SadTalker Pipeline in Google Colab for Single-Image, Single-Audio Talking-Head Generation
  • How to Introduce a New API Quickly Using Micronaut
  • Spring Boot GoT: Game of Trace!
  • Data Pipeline Techniques in Action

Trending

  • Adding Graph Retrieval to a RAG Pipeline With Python
  • Why Kotlin or Java Is the Wrong Question For Enterprises
  • Batching Strategies: Throughput vs Latency in Practice
  • Fix the Target, Precompute Once: A Backend-Free Word-Ladder Solver With a BFS Distance Field

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. Working With Long Lines From the Shell With Cut

Working With Long Lines From the Shell With Cut

Learn how to use the ''cut'' Shell command when working with long lines in Linux

By 
John Cook
·
Sep. 03, 19 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
4.6K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Cut Shell command

Learn how to use the cut Shell command  when working with long lines in Linux

Suppose you have a data file with obnoxiously long lines, and you'd like to preview it from the command line. For example, the other day, I downloaded some data from the American Community Survey and wanted to see what the files contained. I ran something like:

  head data.csv


... to look at the first few lines of the file and got this back:

That was not at all helpful. The part I was interested was at the beginning, but that part scrolled off the screen quickly. To see just how wide the lines are, I ran the following:

    head -n 1 data.csv | wc


I found that the first line of the file is 4822 characters long.

How can you see just the first part of long lines? Use the cut command. It comes with Linux systems and you can download it for Windows as part of GOW.

You can see the first 30 characters of the first few lines by piping the output of head to cut.

    head data.csv | cut -c -30


This shows:


"GEO_ID","NAME","DP05_0001E","
"id","Geographic Area Name","E
"8600000US01379","ZCTA5 01379"
"8600000US01440","ZCTA5 01440"
"8600000US01505","ZCTA5 01505"
"8600000US01524","ZCTA5 01524"
"8600000US01529","ZCTA5 01529"
"8600000US01583","ZCTA5 01583"
"8600000US01588","ZCTA5 01588"
"8600000US01609","ZCTA5 01609"


This is much more useful. The syntax -30 says to show up to the 30th character. You could do the opposite with 30- to show everything starting with the 30th character. And you can show a range, such as 20-30 to show the 20th through 30th characters.

You can also use cut to pick out fields with the -f option. The default delimiter is tab, but our file is delimited with commas so we need to add -d, to tell it to split fields on commas.

We could see just the second column of data, for example, with:

    head data.csv | cut -d, -f 2


This produces:


"NAME"
"Geographic Area Name"
"ZCTA5 01379"
"ZCTA5 01440"
"ZCTA5 01505"
"ZCTA5 01524"
"ZCTA5 01529"
"ZCTA5 01583"
"ZCTA5 01588"
"ZCTA5 01609"


You can also specify a range of fields, say by replacing 2 with 3-4 to see the third and fourth columns.

The humble cut command is a good one to have in your toolbox.

Further Reading

Functions in Shell Script

shell Data file

Published at DZone with permission of John Cook. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Reproducible SadTalker Pipeline in Google Colab for Single-Image, Single-Audio Talking-Head Generation
  • How to Introduce a New API Quickly Using Micronaut
  • Spring Boot GoT: Game of Trace!
  • Data Pipeline Techniques in Action

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook