Tuesday, March 22, 2022
Friday, March 11, 2022
sed command on OSX not supporting '+' or '?' by default
Regex pattern has 3 ways to indicate the occurrence of a character:
"c*" matching letter 'c' 0 to N times$ echo $INSTR | sed 's/p\+c/_/'
aaabbbcccpppccc
$ echo $INSTR | sed 's/p+c/_/'
aaabbbcccpppccc
$ echo $INSTR | sed -E 's/p+c/_/'
aaabbbccc_cc
$ echo $INSTR | sed -E 's/p*c/_/'
aaabbb_ccpppccc
$ echo $INSTR | sed -E 's/p?c/_/'
aaabbb_ccpppccc
$ echo $INSTR | sed -E 's/b?c/_/'
aaabb_ccpppccc
Thursday, December 02, 2021
MacOS: install Jupyter-Lab (next gen notebook) in virtual environment
The classic Jupyter notebook, according to jupyter.org, opens each file on separate web browser tab, while the new Jupyter-lab runs like an IDE on one web browser tab.
OSX default to python 2, so I use virtual environment for python3.
(activate venv, see bottom)
# Installation, reference: jupyter.org
python3 -m pip install -U pip
python3 -m pip install jupyterlab
# launch server
$ jupyter-lab
# if web browser doesn't start up automatically, manually copy-and-paste the http link from the html to your web browser
...
This page should redirect you to a Jupyter application. If it doesn't,
<a href="http://localhost:8888/lab?token=..."
searched jupyter keyboard shortcuts , found some good ones in github.
NOTE that on OSX, use 'command' instead of 'ctrl' key
* * * * * *
if first time using virtual environment, create one is simple:
# create a new virtual environment
$ python3 -m venv ~/.venv/py3
# enter the new environment
$ source ~/.venv/py3/bin/activate
# play inside the environment, for example:
(py3) $ python --version
# exit the environment
(py3) $ deactivate
Monday, October 11, 2021
中文檔在bash出亂碼,可能unicode須轉成utf-8
mac osx 與 linux bash 普遍支援 UTF-8 編碼,為 1~4 個bytes不定長度(variable-length) characters。例如ascii 是 1byte,中文字兩bytes。
unicode 係 定長編碼 (fixed-length),unicode 文字檔一般在檔頭加上 Byte order mark (BOM, 字節順序標記)。這種檔在GUI 文件編輯器開無問題,但在 bash 會出亂碼,不能用cat, sed 等命令。
例如有字幕ssa檔
00000000: fffe 5b00 5300 6300 7200 6900 7000 7400 ..[.S.c.r.i.p.t.
00000010: 2000 4900 6e00 6600 6f00 5d00 0d00 0a00 .I.n.f.o.].....
檔頭32bytes 為BOM,標記含義可參考 https://zh.wikipedia.org/wiki/位元組順序記號。
解決辦法 -- 強大轉碼(convert text encoding)工具:
$ iconv -f unicode -t utf-8 in.txt > out.txt
* * *
順帶一提,如果要刪除每行末 ^M ,可用 sed
s/^M//g
注意^M 是一個 character,輸入方法是 ctrl-v ctrl-m。
Sunday, September 26, 2021
Search document content in Mac & Linux
If there's one feature I like most about Mac, it must be the spotlight search 'summoned' via the keyboard shortcut: command-space.
It searches not only by filenames, but also the content text; not only in text files, but also in RTF, DOC, etc. It makes folder boundaries disappear, and file names unimportant. The files indexing is automatically done in background.
In Windows, about 10 years ago, there was a Google desktop search sidebar tool, but discontinued.
In Linux, we have multiple built-in tools, eg. updatedb, then locate pattern for file names; or ascii content by grep -HERin pattern path; or by more search criteria, 'find' command
Ubuntu has its all-in-one search works somewhat similarly.
For other distro's like mxlinux, an option maybe a tool: search monkey. To install:
sudo apt update
sudo apt upgrade
sudo apt install searchmonkey
For usage, 3 different ways:
(A) bottom-left menu, type search. prefilled search location $HOME
(B) terminal command line:
$ searchmonkey
or
$ searchmonkey -d directory -f filename_regex -t text_regex
eg.
searchmonkey -d /path/to/directory -f txt -t howto
(C) the handy way, configure in thunar preference i.e. the explorer
file preference > customized action
select 'find files here'
origin default is catfish
replace it with
searchmonkey -d %f
then setup keyboard shortcut for this action.
Note that it's real time search, not from pre-processed database. Therefore, searching in a big folder takes long time.
How to Split a Subdirectory to a New Git Repository and Keep the History
(modified from: https://ao.ms/how-to-split-a-subdirectory-to-a-new-git-repository-and-keep-the-history/)
Say, original local path is ~/allMyCode
1) origin repo to check out the branch with all the subdirectory history, eg. master
git checkout master
2) clone a copy
git clone ~/allMyCode ~/newSubset
cd ~/newSubset
3) in new clone, remove all but the sub directory (below is a one-liner)
git filter-branch --prune-empty --subdirectory-filter relative/path/to/subdirectory current_branch_name
4) if the new clone is a branch other than 'master', make it the new 'master'
git branch -m master
5) remove untracked leftovers, such as ignored/hidden files/folders (WARNING: make sure you have backup)
git clean -fdx
6) fix remote repo URL // assume new repo empty
git remote set-url origin {newRepoURL}
7) push to new repo
git push -u origin master
Saturday, August 07, 2021
Git clone to replace a dead repo
A network machine hosting the git repo was dead.
The checkout still exists in local.
Setup a new machine, planning to use it as the new host for repo.
ssh user@remote
cd /path/to/allprojects/root
git init --bare new_proj.git
exit
# at local
git remote set-url origin user@remote:/path/to/allprojects/root/new_proj.git
git push --all origin
Saturday, July 31, 2021
Sunday, July 11, 2021
Python virtual environment in MacOS
On mac osx, default python is version 2, to support system pre-installed packages.
To try python3 environment, one option is using virtual environment, which allows installing different packages in isolated environment.
ref: https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/
With python2, install virtualenv
With python3, built-in module is 'venv'
$ python3 -m pip install --user --upgrade pip # update pip3
$ python3 -m pip --version # check version
## assume venv for python3
# reuse ~/.virtualenv as the root for all new environments
cd ~/.virtualenv
# (one time only) create new environment for python3
$ python3 -m venv py3
## use of environment from now on
# 1) using environment
$ source py3/bin/activate
$ which python # should be within env now
# now can install diff python3 packages for testing
# 2) leaving venv
$ deactivate
Labels: environment, osx, pip, python, venv
Sunday, June 27, 2021
NTFS external drive some folders not showing on Mac OSX; but okay on Linux
Not all files/folders, but only a few ones not showing.
Since NFTS is read-only (by default) in Mac, as long as not reformatting the drive/ partition, those files should be safe, in theory.
This seems to be a real problem encountered by many people.
https://discussions.apple.com/thread/252158724
Reason unknown yet. Some said chipset not fully compatible.
Some other people said the file system might be corrupted somehow. Because NTFS is Microsoft's proprietary file system.
In case one wants to repair the drive, it must be done in windows, using the following command.
chkdsk /f /r
WARN: backup your drive before making changes.
Sunday, April 25, 2021
HOWTO batch processing images on linux or mac osx
# batch enhance images or batch making thumbnails
for file in *.jpg; do
convert -enhance -equalize -contrast $file "${file%.jpg}_new.jpg"
done
# other options calling convert
# convert -auto-gamma -auto-level -normalize original.jpg improved.jpg
# convert dragon.gif -resize 64x64 resize_dragon.gif
# ref: http://www.imagemagick.org/Usage/resize/
Labels: batch, enhancement, image, jpeg, linux, mac, osx, picture, process, unix
Thursday, April 15, 2021
dictionary word lists
every unix-based os has a dictionary word file
$ wc /usr/share/dict/words
235886 235886 2493109 /usr/share/dict/words
http://wordlist.sourceforge.
Labels: dictionary, osx, ubuntu, unix, words
Sunday, March 07, 2021
macOS 10.15 to install tensorflow 2.4 for python
Tensorflow is a popular framework for machine learning.
The latest version tensorflow 2 runs on python 3.
My default python version in OSX is 2.7.18
To avoid messing the system, I am going to install tensorflow in a python virtual environment.
* * * * *
# get the latest python 3
$ brew install python3 # or
$ brew upgrade python3
# mine installed to /usr/local/opt/python@3.9@ -> ../Cellar/python@3.9/3.9.2_1
# upgrade pip
/usr/local/opt/python@3.9/bin/python3.9 -m pip install --upgrade pip
(pls refer to official doc for installing virtualenv)
# upgrade virtualenv
/usr/local/opt/python@3.9/bin/python3.9 -m pip install --upgrade virtualenv
# if permission error, append '-user' option to the end
/usr/local/opt/python@3.9/bin/python3.9 -m pip install --upgrade virtualenv -user
# also upgrade the wrapper
/usr/local/opt/python@3.9/bin/python3.9 -m pip install --upgrade virtualenvwrapper
# create new virtual environment
mkvirtualenv -p python3 tf
# enter venv
workon tf
# upgrade pip
~/.virtualenvs/tf/bin/python -m pip install --upgrade pip
# install tensorflow
$ ~/.virtualenvs/tf/bin/python -m pip install tensorflow
# launch python; note version 3.x will be in greeting text
$ python
# check tensorflow installation successful
>>> import tensorflow as tf
>>> tf.__version__
'2.4.1'
# first tensorflow (v1) program
>>> import tensorflow.compat.v1 as tf
>>> tf.disable_v2_behavior()
>>> a = tf.constant(1, name='a')
>>> b = tf.constant(1, name='b')
>>> c = a + b
>>> with tf.compat.v1.Session() as sess:
... print(sess.run(c))
...
2021-03-07 12:05:15.104500: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set
2021-03-07 12:05:15.106227: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:196] None of the MLIR optimization passes are enabled (registered 0 passes)
2
>>>
>>> print(a)
Tensor("a:0", shape=(), dtype=int32)
when done,
ctrl+d to quit python
then deactivate venv
$ deactivate
Labels: macOS, osx, python, tensorflow
Sunday, November 08, 2020
osx terminal less source highlight
osx terminal less source highlight
- brew install source-highlight
/usr/local/Cellar/source-highlight/3.1.9_3
- download a script and put into
mv src-hilite-lesspipe.sh /usr/local/bin/ - add to .bashrc
export LESSOPEN="| /usr/local/bin/src-hilite-lesspipe.sh %s" export LESS=" -R "
alias less='less -m -N -g -i -J --underline-special --SILENT'
Sunday, July 14, 2019
HOWTO - on osx convert WAV to MP3 for car audio system
Labels: audio, car, conversion, ffmpeg, howto, lame, linux, mp3, music, osx, stereo, wav
Sunday, May 13, 2018
HOWTO convert ubuntu 18.04 LTS ISO to bootable USB installation disk on Mac OSX
Labels: bootable, dd, diskutil, image, img, installation, iso, osx, ubuntu, usb
Friday, March 30, 2018
sed in-place substitute strings
# default stdout
$ sed 's/OLD_STR/NEW_STR/g' file.txt
or
$ cat file.txt | sed 's/OLD_STR/NEW_STR/g'
output could be redirect to another file using pipe
$ !! > output.txt
if care not keeping original file, can substitute in-place using option '-i'
on linux, simply
$ sed -i 's/OLD_STR/NEW_STR/g' file.txt
on mac osx,add an empty string after '-i'
$ sed -i '' 's/OLD_STR/NEW_STR/g' file.txt
Labels: in-place, linux, osx, replace, search, sed, string, substitute, text
Tuesday, March 28, 2017
UTF8碼中文字排序錯誤/ 'sort' failed for zh_TW.UTF-8 on mac OSX (linux, at least mxlinux, is good)
中文字排序並非以筆劃,而是內碼排列。但是在linux 下內定不懂UTF8 排序。
I tried sort the following but failed.
嘗試排序以下文字,完全忽略不加變更。
看語言已經支援UTF-8了...
guessing it's the 'sort' command default encoding is ascii, not working on utf8. must use
必須更改 LC_ALL
now works
Saturday, October 08, 2016
Highlight syntax in 'less' command on source code. Colorful!!
sudo apt-get install source-highlight
# osx
brew install source-highlight
# osx only, for compatibility i have,
# if the file 'src-hilite-lesspipe.sh' not included, write it without line number:
# add the following into .bashrc
# example print-outs
# c code:
# python code
sweet. isn't it?
Labels: bash, less, linux, osx, syntax-coloring, syntax-detection
