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?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityI 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?
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
if [ output -lt specified time]
then do this in bash...
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)
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}'
cut
takes [FILE] as command-line parameter.awk '{ print $1 }' /proc/uptime