String Manipulation Utilities
Grep
grep myString myFileReturn lines containing myString.
grep -e myString1 -e myString2Match multiple patterns.
grep -EUse a Regex.
grep -iReturn lines that correspond to the string but is not case-sensitive.
grep -A 12Return 12 lines after the match.
grep -B 12Return 12 lines before the match.
grep -C 12Return 12 lines before and after the match.
grep -xReturn lines that correspond exactly to the string.
grep -vReturn lines that do not contain the string.
grep -cCounts the number of lines containing the string.
grep -nEach line containing the string is numbered.
grep -lReturn the names of files containing the string.
grep -hDo not return the folder prefix.
Cut
cut -c 1-12Return characters from 1 to 12 of the input string.
cut -c -12Return the first 12 characters of the input string.
cut -c 12-Return the last 12 characters of the input string.
cut -c 1,12Return characters 1 and 12 of the input string.
cut -d : -f 2Return the second field (
-f 2
). Field separator is defined with-d
.
Sed
sed -dRemove a line (
"/.../d"
).sed -sReplace a string (
"s/old/new/"
).
sed -gApply 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
byj
,b
byk
,c
bym
andd
byn
.
Sort
sort myFile.txtSort the contents of a file.
sort -d myFile.txtSort using only blanks and alphanumeric chars (dictionary order).
sort -k 2 myFile.txtSort on the second column in the file (separated by space).
File Manipulation Utilities
Uniq
uniqDeletes duplicate lines in a file (the comparison is done with the previous line, so use
sort -d
before).
uniq -uReturn lines that appear only once.
uniq -dReturn lines that appear more than once.
Diff
diff myFile1 myFile2Return differences between the two files.
Json Manipulation
JQ
Resources
- Try jq online: https://jqplay.org/#
- Interactive JSON filter : https://github.com/ynqa/jnv
Usage
cat myFile.json | jq
jq myFile.json
jq <<< { "player": { "name": "apple", "color": "blue" } }
Flags
-rOutput raw strings (without the quotes).
Filter Json Array
["myPlayer1", "myPlayer2", "myPlayer3"]
jq '.[]' myFile.jsonReturn elements line by line (without
[ ]
).jq '.[2]' myFile.jsonReturn an element by its ID.
jq '.[2:12]' myFile.jsonReturn a subset of elements.
Filter Json Object
{
"player": {
"name": "myPlayer",
"color": "blue"
}
}
jq '.player.name, .player.color' myFile.jsonReturn the
name
property.
jq '.player | keys' myFile.jsonReturn the keys of
player
(name
andcolor
).jq '.player | length' myFile.jsonReturn the length of
player
(here the number of properties is 2).