Grep lines but let the first line through
Solution 1
I would use a slightly more sophisticated approach than simple grep
:
-
awk
df -h | awk 'NR==1 || /^\/dev/'
NR
is the current line number so theawk
scriptlet above will print if this is the first line or if the current line begins with/dev
. And after posting this I see it is the same as @1_CR's answer. Oh well... -
Perl
df -h | perl -ne 'print if (/^\/dev/ || $.==1)'
The same idea here, in Perl the special variable
$.
is the current line number. An alternative way would bedf -h | perl -pe '$_="" unless /^\/dev/ || $.==1'
The
-p
switch will print all lines of the input file. The value of the current line is held in$_
so we set$_
to empty unless we want the current line. -
sed
df -h | sed -n '1p; /^\/dev/p'
The
-n
suppresses normal output so no lines are printed. The1p
means print the first line and the/^\/dev/p
means print any line that starts with/dev
.As pointed out in the comments below, in the unlikely case where the locale on your current system causes the header line to start with
/dev
, the command above will print it twice. Stephane Chazelas points out that this one will not have that problem:df -h | sed -e 1b -e '/^\/dev/!d'
-
grep
df -h | grep -E '^(/dev|File)'
This might not be portable because of LOCALE problems as you said. However, I am reasonably certain that no locale or
df
version will give a path in the first line, so searching for lines that contain no/
should also work:df -h | grep -E '^[^/]*$|^/dev'
Solution 2
You are going to run into issues attempting to parse df output, however for simple cases, the following may work
LC_ALL=C df -P | awk 'NR == 1 || /^\/dev/'
Solution 3
This should work as well (not tested, though):
df | (read a; echo "$a"; grep /dev)
or
df | (head -n 1; grep ^/dev)
Solution 4
df -h | tee >(head -1) >(sleep 0.5;grep ^/dev) > /dev/null;sleep 1.0
Solution 5
df | head -n 1; df | grep ^/dev
Related videos on Youtube
John Ryan
Updated on September 18, 2022Comments
-
John Ryan over 1 year
So,
I'm trying Facebook.api("/me/friends?fields=installed",handleFunc); with no joy, I get a bad request error in Firebug.
However, I have no problem getting just a list of friends: Facebook.api("/me/friends", handleFriendsLoad);
I know there is a FQL way of doing this. But, why does the above not work for me? If FQL is the only way of doing this, what is the code needed?
Many thanks
-
Totor over 10 yearslocale dependant maybe, but your can force the language with
LANG=C df
. -
terdon over 10 years@Totor see 1_CR's answer, that's exactly what he suggested.
-
Mikel over 10 yearsThis has been answered several times previously. See e.g.
body
from unix.stackexchange.com/a/11859/3169
-
-
Jashwant about 12 yearsMark as answer if it helps you :)
-
Stéphane Chazelas over 10 yearsYour sed one would print the 1st line twice if it started with /dev. Unlikely, but
sed -e 1b -e '/^\/dev/!d'
wouldn't have the problem. -
Keith Thompson over 10 yearsI just tried that; it printed the header line first.
-
Keith Thompson over 10 yearsNot bad, but it has the disadvantage that it invokes
df
twice. -
Keith Thompson over 10 yearsNot particularly, but there are solutions like terdon's
df -h | sed -e 1b -e '/^\/dev/!d'
that only invokedf
once. Thedf
command can hang in some circumstances; in that case, invoking it just once is probably better. -
l0b0 over 10 years@KeithThompson Why is that a problem? The accepted answers also print the header line first.
-
Keith Thompson over 10 years@l0b0: Sorry, I mistyped; it printed the header line last.
-
terdon over 10 yearsNice trick but it will not work as expected if you are mounting by UUID instead of device name. The OP wants
df
style output which means/dev/foo
your command will return/dev/disk/by-uuid/
format output unlessfstab
contains the/dev
name. -
mahendra yadav over 10 yearsWorked correcly on my machine. Try df -h | tee >(head -1) >(sleep 0.5;grep ^/dev) > /dev/null;sleep 1.0
-
Chaim Geretz over 10 yearsvery nice. ^/dev will limit this to lines that start with /dev. Wasn't able to do that as an an edit since I was short 9 characters
-
derobert over 10 yearsOP has said the heading is locale-dependent, so you can't match it like that. And what is the
sda1
doing at the end? -
LovelyI over 10 yearsThe
mount
command uses/etc/mtab
, which is updated dynamically, and while I am not sure wheredf
gets its data from, I suspect that on Linux it reads/proc/mounts
. Either way I don't understand howfstab
is relevant and could not reproduce the problem with UUID-mounted disks. Can you elaborate on it? -
derobert over 9 years@syntaxerror the heading is locale-dependent, as is sorting. So that won't work either, for the same reason. Try
LANGUAGE=ja df -h
, for example. Or Spanish, or French, or many others. -
mahendra yadav over 8 yearsAnonymous edited my post and it does not work for me. df -h | tee >(head -n 1) | grep ^/dev
-
steveayre over 8 yearsFirst one works, but you should echo "$a" (note quotes) or any spacing in the header will shrink down to single spaces (eg with column aligned output). Second one does not.
-
bnikhil over 8 years@steveayre Thanks for the suggestion. Integrated it into answer.
-
pabouk - Ukraine stay strong over 4 years@steveayre even
echo "$a"
is not completely safe (possible different interpretation of-
or\
). It is always better to useprintf %s\\n "$a"
to print a string which could possibly contain the mentioned characters.