Some very useful grep tips for a UNIX or Linux user
This is a very useful example, this is how to stop grep finding letters before the start of the search term “file*[s-t]”. The \b operator is an end of word marker.
┌──[jason@11000000.10101000.00000001.00000011]─[~] └──╼ ╼ $ grep -n "\bfile*[s-t]\b" /usr/share/dict/words 46437:files 46438:filet 46439:filet's |
This is how to use the same search pattern and show the line numbers.
┌──[jason@11000000.10101000.00000001.00000011]─[~] └──╼ ╼ $ grep -n "\bfile*[s-t]\b" /usr/share/dict/words 46437:files 46438:filet 46439:filet's |
This can also be very useful.
Another example.
┌──[jason@11000000.10101000.00000001.00000011]─[~] └──╼ ╼ $ grep "\bope*[n-x]\b" /usr/share/dict/words open open's opt |
How to recursively search text files in folders for a certain string. This will only search text files and binary files will be skipped.
┌──[jason@11000000.10101000.00000001.00000011]─[/usr/share] └──╼ ╼ $ grep -rn -InH --include "*.*" "address" /usr/include/net/ /usr/include/net/if.h:46: IFF_BROADCAST = 0x2, /* Broadcast address valid. */ /usr/include/net/if.h:58: IFF_NOARP = 0x80, /* No address resolution protocol. */ /usr/include/net/if.h:79: IFF_DYNAMIC = 0x8000 /* Dialup device with changing addresses. */ /usr/include/net/if.h:83:/* The ifaddr structure contains information about one address of an /usr/include/net/if.h:84: interface. They are maintained by the different address families, /usr/include/net/if.h:85: are allocated and attached when an address is set, and are linked /usr/include/net/if.h:86: together so all addresses for an interface can be located. */ /usr/include/net/if.h:97: struct ifaddr *ifa_next; /* Next address for interface. */ /usr/include/net/if.h:100:# define ifa_broadaddr ifa_ifu.ifu_broadaddr /* broadcast address */ /usr/include/net/if.h:152:# define ifr_hwaddr ifr_ifru.ifru_hwaddr /* MAC address */ /usr/include/net/if.h:153:# define ifr_addr ifr_ifru.ifru_addr /* address */ /usr/include/net/if.h:155:# define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */ /usr/include/net/if.h:185:# define ifc_buf ifc_ifcu.ifcu_buf /* Buffer address. */ /usr/include/net/route.h:34: struct sockaddr rt_dst; /* Target address. */ /usr/include/net/if_packet.h:25:/* This is the SOCK_PACKET address structure as used in Linux 2.0. /usr/include/net/if_arp.h:55: unsigned short int ar_hrd; /* Format of hardware address. */ /usr/include/net/if_arp.h:56: unsigned short int ar_pro; /* Format of protocol address. */ /usr/include/net/if_arp.h:57: unsigned char ar_hln; /* Length of hardware address. */ /usr/include/net/if_arp.h:58: unsigned char ar_pln; /* Length of protocol address. */ /usr/include/net/if_arp.h:63: unsigned char __ar_sha[ETH_ALEN]; /* Sender hardware address. */ /usr/include/net/if_arp.h:64: unsigned char __ar_sip[4]; /* Sender IP address. */ /usr/include/net/if_arp.h:65: unsigned char __ar_tha[ETH_ALEN]; /* Target hardware address. */ /usr/include/net/if_arp.h:66: unsigned char __ar_tip[4]; /* Target IP address. */ /usr/include/net/if_arp.h:140: struct sockaddr arp_pa; /* Protocol address. */ /usr/include/net/if_arp.h:141: struct sockaddr arp_ha; /* Hardware address. */ /usr/include/net/if_arp.h:149: struct sockaddr arp_pa; /* Protocol address. */ /usr/include/net/if_arp.h:150: struct sockaddr arp_ha; /* Hardware address. */ /usr/include/net/if_arp.h:162:#define ATF_DONTPUB 0x40 /* Don't answer this addresses. */ /usr/include/net/if_arp.h:174: uint32_t ip; /* IP address of entry. */ /usr/include/net/if_arp.h:178: unsigned char ha[MAX_ADDR_LEN]; /* Hardware address. */ /usr/include/net/ethernet.h:31:/* This is a name for the 48 bit ethernet address available on many |
Find all jpeg files in a folder by looking for the JPEG file header.
┌──[jason@11000000.10101000.00000001.00000011]─[/usr/share/backgrounds/ubuntu-mate-common] └──╼ ╼ $ grep -obUaP '\x4a\x46\x49\x46' *.* Green-Jazz.jpg:6:JFIF Grey-Jazz.jpg:6:JFIF Ubuntu-MATE-Splash-Grey.jpg:6:JFIF Ubuntu-MATE-Splash.jpg:6:JFIF |
Some awesome tricks with awk, grep and sed.
https://securitronlinux.com/debian-testing/some-awesome-tricks-with-awk-grep-and-sed/.
The proper way to use grep without cat. And some nice tricks.
https://securitronlinux.com/debian-testing/the-proper-way-to-use-grep-without-cat-and-some-nice-tricks/.