4

I want to cat /proc/uptime into cut -f1 in a Bash script.

I've tried;

cat /proc/uptime | cut -f1
cat /proc/uptime > cut -f1
cut -f1 < cat /proc/uptime

Do I need to use echo or something else to make this happen?

5
  • What are you trying to do, exactly? It's difficult to tell from that one-liner. The first one is correct but unnecessary as cut takes [FILE] as command-line parameter. Jun 18, 2015 at 16:12
  • Sorry I am trying to cat the results of uptime in seconds. I only want the first field of output from /proc/uptime
    – brenguy
    Jun 18, 2015 at 16:14
  • Figured it out! I needed to specify the delimiter.... I thought that was set default to space. My one-liner looks like this now: cat /proc/uptime | cut -d' ' -f1
    – brenguy
    Jun 18, 2015 at 16:16
  • It made sense after I fixed your markdown. :) You can also do this with Awk, and is IMO, easier to remember. awk '{ print $1 }' /proc/uptime Jun 18, 2015 at 16:17
  • 1
    smallo.ruhr.de/award.html
    – dmourati
    Jun 18, 2015 at 16:36

3 Answers 3

7

The default field delimiter for cut is a tab. Since your file has a space instead, you need to specify the delimiter:

-d ' '

And you really don't need to use cat or a pipe at all. Just read the file directly.

cut -f 1 -d ' ' /proc/uptime
2
  • Bonus: How would I then do a if [ output -lt specified time] then do this in bash...
    – brenguy
    Jun 18, 2015 at 16:26
  • 2
    @brenguy That would be an entirely separate question. Here we prefer distinct questions to be asked separately, as this is not a forum and not intended to be. Jun 18, 2015 at 16:38
3
cat /proc/uptime | cut -f1 -d' '

is correct

< /proc/uptime  cut -f1 -d' '

is correct and more efficient as it reads from /proc/uptime directly without creating a pipe (not that it matters here much).

It's generally advisable to use the second form on forums, or else you'll get purists coming after you shouting "useless use of cat".


cut -f1 -d' ' < cat /proc/uptime

is wrong. It's the same as

cut -f1 -d' ' /proc/uptime < cat

If you're in bash, you can also use <():

cut -f1 -d' ' < <(cat /proc/uptime)

This creates an anonymous named pipe for reading and the output of cat /proc/uptime will be piped into it. But again—useless use of cat.

Other than that, cut can also take a file argument so all the redirect versions will also work without the < (it shouldn't matter efficiency-wise):

cut -f1 -d' '/proc/uptime

Or with the <() pipe:

cut -f1 -d' ' <(cat /proc/uptime)
0

By default the delimiter for cut is TAB, you could change it to be whitespace with

cat /proc/uptime | cut -f1 -d " " 

Although in those cases I prefer to use awk:

cat /proc/uptime | awk '{print$1}' 
1
  • 2
    I am going to create a shell. You'll have to opt-in of course, but every pipe results in a donation to charity from $USER's bank account. Jun 18, 2015 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.