Shell

Text Processing

awk

Prepend a newline in a file in the Makefile

    @awk BEGIN{print "Header"} {print $0}' foo.txt; \

I have tried sed '1i' but in Mac it need a newline after i, like:

sed -e '1i\
Header' foo.txt

But Makefile couldn't input the newline, even if I have read
this.
If someone know please let me know.

sed

tail without last 100 line

Sometime, I may want to see a file last some lines without last some lines(let's say 100).
A direct solution is wc -l get the line count then minus 100 use sed. But I have seen
a more graceful solution.

tac file | sed '1,100d' | tac`

grep

Only print specified grouping that match

GET /app/path?foo=bar HTTP

You need only get the /app/path?foo=bar HTTP when GET is front of it and
HTTP is behind it.

grep -oP '(?<=GET )[^ ]+(?= HTTP)' file
A.csv
1
2
3
5
B.csv
2
3
4

You want to print the line only appear in left(right) file, like this:

1
5
diff --unchanged-line-format= --old-line-format='%L' --new-line-format= A.csv B.csv

Or if you need print the line only appear in right file, use this:

diff --unchanged-line-format= --old-line-format= --new-line-format='%L' A.csv B.csv

Bash

Pure bash bible

https://github.com/dylanaraps/pure-bash-bible

Iterate recursively

# Iterate recursively.
shopt -s globstar
for file in ~/Pictures/**/*; do
    printf '%s\n' "$file"
done
shopt -u globstar