How can i grep the first 3 letters of the below output and echo the result.
example:
more /etc/group |grep -i 1900
i get the below result.
h10shm:x:1900:h10adm
I just want the first 3 letters (in above output h10) of the about output and echo the output.
Regards,
Satvik
If you have GNU grep, try:
$ grep -iPo '^...(?=.*1900)' /etc/group
h10
If you don't havec GNU grep, try:
$ grep -i 1900 /etc/group | grep -o '^...'
h10
Or:
$ sed -n 's/^\(...\).*1900.*/\1/p' /etc/group
h10
Or:
$ awk '/1900/{print substr($0,1,3)}' /etc/group
h10
What happened to cut?
more /etc/group | grep 1900 | cut -c-3
or
more /etc/group | grep 1900 | cut -c1-3
output: h10
Works with fields as well, -d is your divider, -f is field number
more /etc/group | grep 1900 | cut -d":" -f1
output: h10shm
Related
Why does grep -v POLYGON remove many more lines than those matching grep POLYGON?
$ cat BOUNDARIES3D_LV03.nt | grep -v POLYGON | wc
249 782 137001
$ cat BOUNDARIES3D_LV03.nt | grep POLYGON | wc
2441 2753697 51833677
$ cat BOUNDARIES3D_LV03.nt | wc
73078 2975809 91746795
Is this a bug in grep (using: grep (GNU grep) 2.23) or am I misunderstanding something?
Update
It seems that grep aborts at the first matching line containing an invalid character.
The problem was that grep aborts at the first line containing a byte sequence that doesn't evaluate to a character in the current encoding. The following resolved the issue for me:
export LC_ALL="en_US.UTF-8"
I use grep -L to get a list of files that do not contain a certain string. How can I see the content of those files? Just like:
grep -L "pattern" | cat
You can use xargs:
grep -L "pattern" | xargs cat
As read in man xargs --> build and execute command lines from standard input. So it will cat to those file names that grep -L returns.
You can use cat and use the output of grep -L...
cat $(grep -L "pattern" *.files )
I'm running this command on OS X to pull the logic board ID:
ioreg -l | grep board-id
which gives me this output:
| "board-id" = <"Mac-FC02E91DDD3FA6A4">
The only part I'm interested in is the "Mac-FC02E91DDD3FA6A4". Is there a way to filter the results from grep to only show me this part? OR is there a second step I could do to clean up the grep results?
Using awk you can do this
ioreg -l | awk -F\" '/board-id/ {print $4}
Mac-FC02E91DDD3FA6A4
This search for board-id, divide output by " and then print part 4
ioreg -l | grep "board-id" | cut -d \" -f 4
one way still with grep, try this line:
ioreg -l|grep -Po 'board-id".*<"\K[^"]*'
I have a file name clfile.me that looks like this;
44433430,"FALSE"
33095934,"TRUE"
41549968,"TRUE"
37945528,"FALSE"
18284764,"FALSE"
15007934,"FALSE"
The number is AIX PID. I have a command that will match the PIDs to a running process.
while read p; do
ps -ef | grep $p | grep 'myproram' | grep -v grep | awk "{ print \$2 }" >> clout.me
done < clfile.me
THe above works but only shows me the PID that matched from the grep command. I want to be able to see the matching PID and the TRUE or FALSE value from the original file. I guess I am asking how I filter the original file by PIDs that match my grep command.
Any thoughts?
Thanks
Chris
Took me a while, but I have it!
cat /dev/null > clout.me
while read p; do
x=$(awk '{ print $1 }')
ps -ef | grep x | grep 'myprogram' | grep -v grep | awk "{ print \$2 }" >> clout.me
done < clfile.me
awk 'FNR==NR{A[$1]=1;next} A[$1]' clout.me clfile.me
zgrep -i XXX XXX | grep -o "RID=[0-9|A-Z]*" |
uniq | cut -d "=" -f2 |
xargs -0 -I string echo "RequestID="string
My output is
RequestID=121212112
8127127128
8129129812
But my requirement is to have the request ID prefixed before all the output.
Any help is appreciated
I had a similar task and this worked for me. It might be what you are looking for:
zgrep -i XXX XXX | grep -o "RID=[0-9|A-Z]*" |
uniq | cut -d "=" -f2 |
xargs -I {} echo "RequestID="{}
Try -n option of xargs.
-n max-args
Use at most max-args arguments per command line. Fewer than max-args arguments will be used if the size (see the -s option)
is exceeded,
unless the -x option is given, in which case xargs will exit.
Example:
$ echo -e '1\n2' | xargs echo 'str ='
str = 1 2
$ echo -e '1\n2' | xargs -n 1 echo 'str ='
str = 1
str = 2