Skip to content

Instantly share code, notes, and snippets.

@photonxp
Last active October 23, 2018 02:29
Show Gist options
  • Save photonxp/91a66eda4855ad64d20903157f5738f1 to your computer and use it in GitHub Desktop.
Save photonxp/91a66eda4855ad64d20903157f5738f1 to your computer and use it in GitHub Desktop.
add tagkey tag:: to data file in dir
#!/bin/bash
get_line_tag(){
tagval="$1"
fpath="$2"
# unable to check $? for this gawk
line_tag=$(gawk -v ptn="$tagval([[:space:]]|$)" '$0~ptn && NR >= 3 && NR <= 5 {print NR":"$0;exit;}' "$fpath")
#echo "line_tag: $line_tag"
}
check_is_target_line_tag(){
# return 0: is target tagline;
# return 1: is NOT target tagline;
line_tag="$1"
# check "" line_tag result
# gawk won't add line num to unmatched result
if [ "y" == "y$line_tag" ]
then
return 1
fi
# check "tag:" part in line_tag
printf "$line_tag" | grep -q 'tag:'
retval_grep=$?
if [ 0 -eq $retval_grep ]
then
return 1
fi
taglist_inline="$(echo ${line_tag##*:})"
#echo "taglist_inline:" $taglist_inline
arr_taglist_inline=($taglist_inline)
length_arr_taglist_inline=${#arr_taglist_inline[@]}
if [ 1 -eq $length_arr_taglist_inline ]
then
return 0
fi
match_tag_inline_to_infile(){
tag_inline=$1
flag_matched=1
for tag_infile in "${arr_taglist_infile[@]}"
do
if [ "$tag_infile" == "$tag_inline" ]
then
flag_matched=0
break
fi
done
}
match_first_2_tags_to_tagfile(){
# 0: all tags are matched in tag file
# 1: a tag is not matched in tag file
flag_tagline=0
for i in `echo 0 1`
do
match_tag_inline_to_infile ${arr_taglist_inline[$i]}
if [ 1 -eq $flag_matched ]
then
flag_tagline=1
break
fi
done
}
if [ 2 -le $length_arr_taglist_inline ]
then
match_first_2_tags_to_tagfile
return $flag_tagline
fi
}
add_tagkey_to_file(){
fpath="$1"
for tag_infile in "${arr_taglist_infile[@]}"
do
get_line_tag "$tag_infile" "$fpath"
line_num="${line_tag%%:*}"
check_is_target_line_tag "$line_tag"
retval=$?
#echo "line_tag:" $line_tag "retval: " $retval
if [ 0 -eq $retval ]
then
line_filter="$line_num"
tagkey="tag:: "
substitute_operation="s/(^.*$)/$tagkey\1/"
sed_directive="$line_filter $substitute_operation"
sed -r "$sed_directive" -i "$fpath"
break
fi
done
}
main(){
fpath_taglist="$1"
dpath_data="$2"
mapfile -t arr_fpath_list < <(find "$dpath_data" -maxdepth 1 -type f)
mapfile -t arr_taglist_infile < <(cat "$fpath_taglist")
for fpath in "${arr_fpath_list[@]}"
do
add_tagkey_to_file "$fpath"
done
}
main "$@"
#!/bin/bash
init_time_log(){
fpath_log=./time.log
if [ -f $fpath_log ]
then
rm $fpath_log
fi
touch $fpath_log
}
log_datetime(){
datef=`date '+%Y%m%d.%H%M%S'`
echo "$1$datef" >> $fpath_log
}
init_script(){
init_time_log
log_datetime "Start script : "
}
finish_script(){
log_datetime "Finish script : "
}
echo_start(){
echo "$1 ============================="
}
init(){
echo_start "Start $1 : "
log_datetime "Start $1 : "
}
finish(){
log_datetime "Finish $1 : "
}
run_tests(){
init_script
for test in "$@"
do
init "$test"
$test
finish "$test"
done
finish_script
}
sed_line_tag(){
tag_line="$1"
fpath="$2"
sed -r "3s/^.*$/$tag_line/" -i "$fpath"
}
test1(){
fname1="200-earthquakes-detected-yellowstone..2018-02-21T19.09.09.md"
fpath1=$dir_data/$fname1
fname2="115-million-year-old-theropod-footprint-vandalised-australia-dinosaur-dreaming..2017-12-21T17.51.23.md"
fpath2=$dir_data/$fname2
fname3="50g宁夏生甘草茶2018-05-14T20.25.21.md"
fpath3=$dir_data/$fname3
fname4="45th_Legislative_District_Senate_Debate..2017-12-24T21.31.36.md"
fpath4=$dir_data/$fname4
fname5="2017 P&G Championships - Women - Day 2 - NBC Broadcast..2018-01-11T17.33.58.md"
fpath5=$dir_data/$fname5
sed_line_tag "bb vocabulary sciencealert" "$fpath1"
sed_line_tag "vocabulary bb sciencealert" "$fpath2"
sed_line_tag "中草药 甘草 bb" "$fpath3"
sed -r "3s/^tag:: (.*)$/\1/" -i "$fpath4"
sed_line_tag "vocabulary sciencealert" "$fpath5"
./add_tagkey_in_dir.bash "$fpath_taglist" "$dir_data"
head "$fpath1"
head "$fpath2"
head "$fpath3"
head "$fpath4"
head "$fpath5"
}
test2(){
fname="45th_Legislative_District_Senate_Debate..2017-12-24T21.31.36.md"
fpath=$dir_data/$fname
sed -r "3s/^tag:: (.*)$/\1/" -i "$fpath"
head "$fpath"
./add_tagkey_in_dir.bash "$fpath_taglist" "$dir_data"
head "$fpath"
}
fpath_taglist=./tag_list.txt
dir_data=~/data_mnt/txt_data_test/QOwnNotes/Notes.test
run_tests "test1"
#run_tests "test2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment