String Manipulation Utilities


Grep

grep myString myFile
Return lines containing myString.

grep -e myString1 -e myString2
Match multiple patterns.

grep -E
Use a Regex.

grep -i
Return lines that correspond to the string but is not case-sensitive.

grep -A 12
Return 12 lines after the match.

grep -B 12
Return 12 lines before the match.

grep -C 12
Return 12 lines before and after the match.

grep -x
Return lines that correspond exactly to the string.

grep -v
Return lines that do not contain the string.

grep -c
Counts the number of lines containing the string.

grep -n
Each line containing the string is numbered.

grep -l
Return the names of files containing the string.

grep -h
Do not return the folder prefix.


Cut

cut -c 1-12
Return characters from 1 to 12 of the input string.

cut -c -12
Return the first 12 characters of the input string.

cut -c 12-
Return the last 12 characters of the input string.

cut -c 1,12
Return characters 1 and 12 of the input string.

cut -d : -f 2
Return the second field (-f 2). Field separator is defined with -d.


Sed

sed -d
Remove a line ("/.../d").

sed -s
Replace a string ("s/old/new/").

sed -g
Apply on all lines ("/old/new/g").


Tr

tr 'a-z' 'A-Z'
Replace lower case letters with upper case letters.

tr 'abcd' 'jkmn'
Replace a by j, b by k, c by m and d by n.


Sort

sort myFile.txt
Sort the contents of a file.

sort -d myFile.txt
Sort using only blanks and alphanumeric chars (dictionary order).

sort -k 2 myFile.txt
Sort on the second column in the file (separated by space).

File Manipulation Utilities


Uniq

uniq
Deletes duplicate lines in a file (the comparison is done with the previous line, so use sort -d before).

uniq -u
Return lines that appear only once.

uniq -d
Return lines that appear more than once.


Diff

diff myFile1 myFile2
Return differences between the two files.

Json Manipulation


JQ

Resources

Usage

cat myFile.json | jq

jq myFile.json

jq <<< { "player": { "name": "apple", "color": "blue" } }

Flags

-r
Output raw strings (without the quotes).

Filter Json Array

["myPlayer1", "myPlayer2", "myPlayer3"]

jq '.[]' myFile.json
Return elements line by line (without [ ]).

jq '.[2]' myFile.json
Return an element by its ID.

jq '.[2:12]' myFile.json
Return a subset of elements.

Filter Json Object

{
  "player": {
    "name": "myPlayer",
    "color": "blue"
  }
}

jq '.player.name, .player.color' myFile.json
Return the name property.

jq '.player | keys' myFile.json
Return the keys of player (name and color).

jq '.player | length' myFile.json
Return the length of player (here the number of properties is 2).