-
Notifications
You must be signed in to change notification settings - Fork 0
Text
To display text on standard output
echo "Path is $PATH"To create a file with text
echo "foo" > file.txtTo append text at the end of an existing file
echo "bar" >> file.txtReplace some character by another
tr <FROM_SET> <TO_SET>To replace all as by bs
echo aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa | tr a b
# bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbbTo replace [a-c] (a,b,c) by d
echo abcd | tr a-c d
# dddddReturns 0 if a match happens (read until match, stop afterwards)
echo Foobar | grep --max-count=1 --ignore-case foo | echo $?https://github.com/coreutils/coreutils/blob/master/src/cat.c
To produce a file containing both file content
cat file1.txt file2.txt > file3.txtTo display a file content (showing special characters and line number)
cat --show-all --number README.startup.mdTo edit a file interactively (hit Ctrl-D after keying in, you can key in several lines) Does this works ?
cat file.txt - > file.txtTo create a file and supply text interactively (hit Ctrl-D after keying in, you can key in several lines)
cat > file.txtShow first lines of a huge file, or just the first
head --lines=1Show last lines of a huge file (or a fd)
tail $FILE
Show last lines of a file which is currently written
tail --follow --lines=10Split one files into several files (per lines)
Take this half-million lines and split them into 500k chunks named chunk-<ID_DECIMAL>.csv
head -n 5000000 $RAW_DATA_FILE_PATH | split -d -l 100000 --additional-suffix=.csv - chunkcut pick fields from a list of rows, eg. CSV values
jane;doe;scientist
mary;shelley;writer
Pick the second occurence, eg. a folder in a path
echo 'a/b/c' | cut --delimiter=/ --fields=2
bStrip the first occurence, eg. removing a leading folder in a path
echo 'a/b/c' | cut --delimiter=/ --fields=1 --complement
/b/cShow differences between two files.
diff $FILE_ONE $FILE_TWOShow differences between two commands.
Show the files which are modified but not staged
diff <(git diff --name-only) <(git diff --name-only --staged)Show common lines of two files
Show only common fines: use parameters -1 -2
comm -1 -2 <(git diff --name-only $BACKEND_DIRECTORY) <(git diff --name-only --staged $BACKEND_DIRECTORY)Count words
awk take text as input and transform it.
Basically, you can split a line into token when multiple whitespace occurs. Cut can handle a fixed whitespace, while awk can handle many.
echo "Hello world" | cut --delimiter=" " --fields=2
echo "Hello world" | cut --delimiter=" " --fields=2
echo "Hello world" | awk '{print $2}'
echo "Hello world" | awk '{print $2}'world
world world
uuidgenIt can be time-consuming:
- 10s for 10k
- 1 min 30 for 100k
- 30 minutes for 3 millions
time (1>/dev/null for i in {1..10000}; do uuidgen; done)
( for i in {1..10000}; do; uuidgen; done > /dev/null; ) 5,69s user 2,95s system 99% cpu 8,670 totalRandom or time-based does not matter much
> time (1>/dev/null for i in {1..10000}; do uuidgen --random; done)
( for i in {1..10000}; do; uuidgen --random; done > /dev/null; ) 5,71s user 2,86s system 99% cpu 8,620 total
> time (1>/dev/null for i in {1..10000}; do uuidgen --time; done)
( for i in {1..10000}; do; uuidgen --time; done > /dev/null; ) 5,84s user 3,03s system 92% cpu 9,621 totalConvert from one format to another
pandoc --from <SOURCE_TYPE> --to <TARGET_TYPE> $TARGET_FILE>From mediawiki to markdown, removing the original file.
FILE_NAME=<FILE>; pandoc --from mediawiki --to markdown $FILE_NAME.mediawiki > $FILE_NAME.startup.md; rm $FILE_NAME.mediawikiFormats are supported (from and to):
- Microsoft Word:
docx - Markdown:
markdown
Key-value / property
{
"name": "Jane",
"job" : "Database administrator"
}https://jqlang.github.io/jq/manual/
. is identify filter, use it to check file is valid JSON
> echo '[{"foo":"bar"},{"foobar":"barfoo"}]' | jq .
[
{
"foo": "bar"
},
{
"foobar": "barfoo"
}
]> echo '{"foo":"bar"}' | jq '.foo'
"baréhttps://jqlang.github.io/jq/manual/#basic-filters
Get value of first element's key
> echo '[{"foo":"bar"},{"foobar":"barfoo"}]' | jq '.[0].foo'
"bar"Get value of all element's key
> echo '[{"foo":"foo"},{"foo":"bar"}]' | jq '.[].foo'
"foo"
"bar"Get several values of all element's key
echo '{ "team" : [{"name": "Jane","job" : "Database administrator"},{"name": "Mattie","job" : "Back-end developer"}] }' | jq '.team | keys[] as $k | [$k, (.[$k] .name), (.[$k] .job)]'[
0,
"Jane",
"Database administrator"
]
[
1,
"Mattie",
"Back-end developer"
]https://www.markhneedham.com/blog/2021/05/19/jq-select-multiple-keys/
mlr --csv cat $FILENAMEDataset /tmp/team.yml
members:
- name: Jane
job: Database administrator
- name: Mattie
job: Back-end developer Get value of all element's key
echo /tmp/team.yml | yq '.members[].name'Returns
"Jane"
"Mattie"