Skip to content

Instantly share code, notes, and snippets.

@romiras
romiras / Readme.md
Created November 28, 2024 22:12
Convert TSV to CSV file

This shell script processes a tab-separated values (TSV) file and converts it into a CSV (Comma-Separated Values) format. The script handles cases where fields in the TSV file contain commas, ensuring proper CSV formatting by escaping and quoting such fields. Let's break it down step by step:

1. Shebang (#!/bin/sh)

This line specifies that the script should be run using the sh shell.

2. awk command

The main body of the script is an awk command. awk is a powerful text-processing tool that works line by line on input text.

BEGIN { FS="\t"; OFS="," }

  • BEGIN { ... }: This block is executed before any lines of input are processed.
@romiras
romiras / git-branch-sync-strategy.md
Created November 28, 2024 22:06
VCS: syncing strategy of latest updates from 'master' to 'myfeature' branch (git)

Task: sync latest updates from 'master' to 'myfeature' branch

git checkout myfeature
git merge -s ours master --no-commit

to take all changes from master into devel

@romiras
romiras / copy_dir_ssh.sh
Created November 28, 2024 12:55
send/receive directory over network as TAR via SSH
# send
tar czf - source_directory/ | ssh [email protected] tar xzf - -C ~/
# receive
ssh username@from_server "tar czf - directory_to_get" | tar xzvf - -C path_where_to_put
@romiras
romiras / macos-go-new_repo.sh
Created November 28, 2024 12:52
Snippets to prepare new Golang repository from template in MacOS
# for consumer
sed -i '' -e 's/internal\/my-consumer/internal/g' *
# for REST API
grep -ilr 'internal\/' internal/* | xargs -I@ sed -i '' 's/internal\/my-api/internal/g' @
grep -ilr 'internal\/' cmd/* | xargs -I@ sed -i '' 's/internal\/my-api/internal/g' @
mv internal/my-api/* internal/
mv cmd/my-api/* cmd/
rm -r cmd/my-api/
@romiras
romiras / db-tsv-export.sh
Created November 28, 2024 12:49
Export table column into TSV file in MySQL
mysql -C \
--raw --batch \
--default-character-set=utf8mb4 \
-e "SELECT col1 FROM tbl1" \
-u usr -p db_schema \
| pv > dump-$1.tsv
@romiras
romiras / youtube_transcript_to_md.py
Created October 4, 2024 23:08
Convert a JSON response from Youtube transcript to Markdown
"""
Parses JSON response from https://www.youtube.com/youtubei/v1/get_transcript
and converts to Markdown document
"""
import json
import sys
def get_segments(data):
return data['actions'][0]['updateEngagementPanelAction']['content']['transcriptRenderer']['content']['transcriptSearchPanelRenderer']['body']['transcriptSegmentListRenderer']['initialSegments']
@romiras
romiras / queue_dumper.cr
Created April 29, 2024 14:13
Drain Sidekiq' queue in Redis with matching args to stdout, otherwise push to another queue (Crystal lang)
# Run:
# crystal src/main.cr \
# -redis-url='redis://localhost:6379' \
# -namespace=sidekiq \
# -queue-name=foo \
# -class-name=Foo::BarProcessor \
# -arg1=123 \
# > filtered_jobs.jsonl
require "json"
@romiras
romiras / fix_raw_object_rails.rb
Created April 29, 2024 09:17
A hack to fix class dynamically in YaML-serialized attribute, Ruby on Rails
def raw_object
self[:raw_object]
rescue ArgumentError => e
raw = raw_object_before_type_cast
raw.gsub!(%r{!ruby/object:MyNamespace::Fixme}, '!ruby/object:MyNamespace::Corrected')
Psych.load(raw, symbolize_names: true)
end
@romiras
romiras / fix_yaml_column.rb
Created April 29, 2024 09:07
Fixing memoization issues in Ruby 2.4.x
# FILE: config/initializers/fix_yaml_column.rb
# serializes the columns that break because of memoized cache
#
# alternatively, we could have just removed the string for the memoized cache, but that sounded too dangerous
# hard-require to load the class
require 'active_record/coders/yaml_column'
class ActiveRecord::Coders::YAMLColumn
@romiras
romiras / loop_breaker.rb
Created April 27, 2024 21:28
Break a loop gracefully on Ctrl-C
Signal.trap("INT") { throw :exit_loop }
def aaa
catch :exit_loop do
loop do
puts 'foo'
p Benchmark.realtime { sleep 1 }
puts 'bar'
end
end
Signal.trap('INT', 'DEFAULT')