hyperreal.coffee

1sed 'Nq;d' file.txt

Prepend text to beginning of file

1echo "new content" | cat - file.txt > temp
2mv temp file.txt
1sed -i '1s/^/new content\n/' file.txt

Remove lines containing only whitespace

1sed -i '/^\s*$/d' file.txt

Delete nth line from file

1# N should be the target line number
2sed -i 'Nd' file.txt

Replace an entire line in file by line number

1# N should be the target line number
2sed -i 'Ns/.*/replacement-line/' file.txt

Heredoc

1cat << EOF > file.txt
2The current working directory is $PWD.
3You are logged in as $(whoami).
4EOF

Plain-print the difference between two files

Suppose we have two files: packages.fedora and packages.

packages.fedora:

 1autossh
 2bash-completion
 3bat
 4bc
 5borgmatic
 6bzip2
 7cmake
 8curl
 9diff-so-fancy
10diffutils
11dnf-plugins-core

packages:

1bash-completion
2bc
3bzip2
4curl
5diffutils
6dnf-plugins-core

To plain-print the lines that exist in packages.fedora but do not exist in packages:

1comm -23 <(sort packages.fedora) <(sort packages)

Output:

autossh
bat
borgmatic
cmake
diff-so-fancy

Split large text file into smaller files with equal number of lines

1split -l 60 bigfile.txt prefix-

Loop through lines of file

1while read line; do
2    echo "$line";
3done </path/to/file.txt

Use grep to find URLs from HTML file

1cat urls.html | grep -Eo "(http|https)://[a-zA-Z0-9./?=_%:-]*"

Use Awk to print the first line of ps aux output followed by each grepped line

To find all cron processes with ps aux:

1ps aux | awk 'NR<2{print $0;next}{print $0 | grep "cron"}' | grep -v "awk"

Reply to this post by email ↪