Main Menu

Search

Showing posts with label grep command. Show all posts
Showing posts with label grep command. Show all posts

LINUX: Command To Print All Characters In Lines After "/" BackSlash (Grep)

Below is the grep command to use.

grep -o '/[^/]*$' <filename> | cut -c2-

For e.g. if contents of file is as follows:

/registry/pods/kube-system/kube-flannel-ds-abcde
/registry/pods/kube-system/kube-flannel-ds-xyzab

Above command will print below

kube-flannel-ds-abcde
kube-flannel-ds-xyzab

LINUX: How to Remove/Ignore/Not Display Blank Lines in A File Using grep command

Below grep command can be used for removing/ignoring and not displaying blank lines in a file.
grep -v -e '^$'  <filename>

Products to which Article Applies

All Linux Operating Systems

LINUX: GREP Command To Extract IP Addresses From Command Output Or File (How To Doc)

Below grep command can be used to extract IP addresses from a file.
grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' <filename>

To extract IP addresses from command output below grep command can be used.

<command> | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'

For example, below is grep command to extract IP addresses from "ip addr" command output.

ip addr | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'

Below is sample output of above command.

10.10.10.137
10.20.11.12


Products to which Article Applies

All Linux Operating Systems


Additional Reference

https://www.shellhacks.com/regex-find-ip-addresses-file-grep/

LINUX: "sed" Command to Find and Replace All Occurrences of a string inside a Directory & its Sub directories(How To Doc) ("grep" command)

Below command can be used to find and replace all occurrences of a string in files inside a directory and sub directories.

grep -rl 'string1' /tmp | xargs sed -i 's/string1/string2/g'

Above command looks for string 1 in all files inside /tmp and replaces it with string2. You can change the directory name as needed from /tmp to any directory where you want to find and replace a string.

Products to which Article Applies


All Linux Operating Systems

Additional References


https://stackoverflow.com/questions/6758963/find-and-replace-with-sed-in-directory-and-sub-directories