InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Keeping Only a Portion of a Line of Output in bash
Bash

Keeping Only a Portion of a Line of Output in bash

InfinityCoder February 19, 2017

You want to keep only a portion of a line of output, such as just the first and last words.

For example, you would like ls to list just filenames and permissions, without all of the other information provided by ls -l.

However, you can’t find any options to ls that would limit the output in that way.

Pipe ls into awk, and just pull out the fields that you need:

1
2
3
4
5
6
7
8
9
10
11
$ ls -l | awk '{print $1, $NF}'
total 151130
-rw-r--r-- add.1
drwxr-xr-x art
drwxr-xr-x bin
-rw-r--r-- BuddyIcon.png
drwxr-xr-x CDs
drwxr-xr-x downloads
drwxr-sr-x eclipse
...
$

Consider the output from the ls -l command. One line of it looks like this:

1
drwxr-xr-x 2 username group              176 2006-10-28 20:09 bin

so it is convenient for awk to parse (by default, whitespace delineates fields in awk).
The output from ls -l has the permissions as the first field and the filename as the last field.
We use a bit of a trick to print the filename.

Since the various fields are referenced in awk using a dollar sign followed by the field number (e.g., $1, $2, $3), and since awk has a built-in variable called NF that holds the number of fields found on the current line, $NF always refers to the last field.

(For example, the ls output line has eight fields, so the variable NF contains 8, so $NF refers to the eighth field of the input line, which in our example is the filename.)
Just remember that you don’t use a $ to read the value of an awk variable (unlike bash variables).

NF is a valid variable reference by itself. Adding a $ before it changes its meaning from “the number of fields on the current line” to “the last field on the current line.”

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Saving Output from a Command in bash
You want to keep the output from a command by …

Saving Output from a Command in bash

Getting bash for Mac OS X in bash
You want to get bash for your Mac, or you …

Getting bash for Mac OS X in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap