Pages

Saturday, January 2, 2016

Regular Rxpressions One Liners

Home

To make a copy of file

# cp /path/to/file /path/to/file_move
# cp /path/to/file{,_copy}

To move or rename a file

# mv /path/to/file_name /path/to/file_name_old
# mv /path/to/file_name{,_old}

To generate Small letter a to z (with spaces between characters)

# echo {a..z}

To generate Capital letter A to Z (with spaces between characters)

# echo {A..Z}

To generate Small letter a to z (without spaces between characters)

# printf "%c" {a..z}

To generate Capital letter A to Z (without spaces between characters)

# printf "%c" {a..Z}
Above with result in output is without a terminating newline because the format string was "%c" and it doesn't include \n.

To have it newline terminated, just add $'\n' to the list of chars to print:

# printf "%c" {a..z} $'\n'
# echo $(printf "%c" {a..z})

To generate all letters in a column output and store it in a variable

# printf "%c\n" {a..z}
# variable=$( printf "%c" {a..z})
# variable=`printf "%c" {a..z}`
# printf –v variable "%c" {a..z}

To generate numbers from 1 to 100 (with spaces between numbers)

# echo {1..100}

To generate numbers from 1 to 100 (in column format)

# seq 1 100

To generate numbers 0 to 9 with a leading zero as pad

# printf "%02d " {0..9}
# echo {00..09}

To generate n number of words

# echo {w,t,}h{e{n{,ce{,forth}},re{,in,fore,with{,al}}},ither,at}

To generate alphanumeric strings

# echo {a,b,c}{1,2,3}

To generate 5 copies of the same string

# echo potato{,,,,}


To search lines starting with a string

grep '^string' files

To search lines ending with a string

grep 'smug$' files

To search lines containing only the string

grep '^smug$' files

To search lines starting with a special character ^ which needs to be escaped

grep '\^s' files

To search lines containing string with capital and small letter combination

grep '[Ss]mug' files

grep 'B[oO][bB]' files

To search empty lines

grep '^$' files

To search lines containing pair of digits

grep '[0-9][0-9]' file

To search lines containing at least one letter

grep '[a-zA-Z]'

To search lines containing letter or digit

grep '[^a-zA-Z0-9]

To search lines similar to 999-9999, like phone numbers

grep '[0-9]\{3\}-[0-9]\{4\}’

To search lines with exactly one character

grep '^.$'

To search lines with string within quotes (Single/Double)

grep '"string"'        {'string' within double quotes}
grep '"*string"*'        {'string', with or without quotes}

To search lines starting with special character “.”

grep '^\.'     {any line that starts with a Period "."}



Back To Top
Home

No comments:

Post a Comment