Use grep in Mac Terminal - unix

I use Terminal in Mac with the following command:
df -lak | grep File||disk02
what I want to use this script to get the header of df command (disk space) and the line with disk02 only. I think '|' is a char in grep as or logic. However, since I am using grep in Terminal, the char '|' also means pipe. Therefore I tried to use '||' to avoid piping, but it does not get what I want. Only the header with "File" is back.
Not sure how I can use this script command in Terminal?

df -lak | grep "File\|disk02"

use awk
df -lak | awk 'NR==1 || /disk02/'
Or
df -lak | grep -E "File|disk02"

df -lak | grep -E '(^File|disk02)'
You can shorten grep -E to egrep.

df -lak | grep -e File -e disk02

Just display the mount points you're interested in!
df -lak /

Related

sort -t $'\t' equivalent compatible with POSIX sh?

I am trying use a for loop for multiple files in my directory with a pipe command,but it does not seem to work. When am running the same command on a single file it seems to work. Where am I getting it wrong?
for x in *summary-FDR0.05 ; do sort -t $'\t' -k8,8rn $x | head -n 50000 | sortBed -i > sorted_top_50k_$x.bed; done
All my files end with summary-FDR0.05. When I run
sort -t $'\t' -k8,8rn sample13-summary-FDR0.05 | head -n 50000 | sortBed -i > sorted_top_50k_S_13_O1_122*K27ac.bed
This seems to work well. May I know where I am getting it worng
Error:
sort: multi-character tab `$\\t'
Thanks
For POSIX compatibility, replace $'\t' with "$(printf "\t")".

Splitting unix output

I'm trying to extract an address from a file.
grep keyword /path/to/file
is how I'm finding the line of code I want. The output is something like
var=http://address
Is there a way I can get only the part directly after the = i.e. http://address , considering the keyword I'm greping for is both in the var and http://address parts
grep keyword /path/to/file | cut -d= -f2-
Just pipe to cut:
grep keyword /path/to/file | cut -d '=' -f 2
You can avoid the needless pipes:
awk -F= '/keyword/{print $2}' /path/to/file

Grep OR Operator Not Working

I need help with Unix.
I am trying to see if one of two statements (printf and fprintf) are in a file. I used the command:
search=`cat $file | grep -w "fprintf\|printf"`
For some reason, it doesn't find either in files where one of those two exists. Why?
You have two problems.
First, standard grep doesn't support the | operator. You need to use egrep or the -E flag.
Second, inside double-quotes, \| means \|. The backslash gets passed through to the grep command, so even if grep understood the | operator, the backslash would turn it into a normal character.
Try this:
search=`cat $file | egrep -w "fprintf|printf"`
Or you can provide each alternative as a separate argument to grep:
search=`cat $file | grep -w -e fprintf -e printf
grep -w "fprintf\|printf" $file
This works fine in my shell. Still, here are some alternatives:
egrep -w 'fprintf|printf' $file
grep -wE 'fprintf|printf' $file
grep -we 'fprintf\|printf' $file

get only the value...not what your grepping for

pdftk file.pdf dump_data output | grep NumberOfPages:
gives me:
NumberOfPages: 5
I don't want it to output NumberOfPages. I want to get in this case just 5. Is there a flag I can say in grep to get just that? I did a man grep and nothing seemed to do the trick.
I think grep doesn't know about how to parse strings in different formats. But other utilities like awk will help you:
pdftk file.pdf dump_data output | grep NumberOfPages: | awk '{print $2}'
pdftk file.pdf dump_data output | grep NumberOfPages: | sed 's\NumberOfPages:\\'
Yes, in GNU Grep you can use the -o operator to get "only" the matching portion of your expression. So something like;
pdftk file.pdf dump_data output | grep -o ' .*'
Could work for you. As other answers have pointed out, if you want only the number you'd be better off using something in addition to grep.
For example:
$ echo 'NumberOfPages: 5' | grep -o ' .*'
5
Notice the space before the 5 being included.

egrep issue- need to search different patterns?

Need a grep one liner [ without pipe ] which would check multiple expressions in a single command
cat FILE | egrep OH|OI is not working
What you're looking for is a simple:
egrep 'OH|OI' FILE
The command you have (without the quotes):
cat FILE | egrep OH|OI
will attempt to cat the file through egrep looking for OH then pipe the results of that through an executable OI (which probably doesn't exist).
The quotes will fix that for you so that OH|HI is a single argument to the egrep rather than something the shell processes.
Just try:
cat FILE | egrep 'OH|OI'
Eliminate the pipe by supplying the filename.
egrep 'OH|OI' filename

Resources