A collection based mostly on stackexchange posts along with a few I threw together myself. Later, I will have a .bashrc
file that contains a collection of regex patterns.
Note that the arguments for these functions are additional arguments passed to sed. Variables like $pattern
$find
$replace
must be pre-defined.
find_and_replace(){
sed 's/'$find'/'$replace'/g' $@
}
find_and_replace_on_lines() {
sed '/'$pattern'/'$find'/'$replace'/g' $@
}
find_and_replace_first_match(){
sed 's/'$find'/'$replace'/' $@
}
find_and_replace_last_match() {
sed 's/\(.*\)'$find'/'$replace'/' $@
}
find_and_replace_inside_range() {
sed '$range s/'$find'/'$replace'/g' $@
}
find_and_replace_outside_range(){
sed '$range !s/'$find'/'$replace'/g' $@
}
find_and_replace_nth_instance(){
sed 's/'$find'/'$replace'/'$nth_instance'' $@
}
find_and_replace_within_start_end(){
sed -e '/'$start'/,/^'$end'/s/'$find'/'$replace'/g' $@
}
print_line_matching_pattern(){
sed '/'$pattern'/!d' $@
}
print_line_preceeding_pattern() {
sed -n '/'$pattern'/{g;1!p;};h' $@
}
print_line_suceeding_pattern(){
sed -n '/'$pattern'/{n;p;}'
}
print_line_matching_pattern_and_all_subsequent(){
sed '/'$pattern'/,$!d' $@
}
print_line_matching_pattern_with_context(){
sed -n -e '/'$pattern'/{=;x;1!p;g;$!N;p;D;}'
}
print_lines_matching_patterns_in_any_order(){
sed '/'$pattern3'/!d; /'$pattern1'/!d; /'$pattern2'/!d' $@
}
print_lines_matching_patterns_in_specific_order(){
sed '/'$pattern1'.*'$pattern2'.*'$pattern3'/!d' $@
}
print_lines_min_nchars(){
sed -n '/^.\{$nchars\}/p' $@
}
print_lines_max_nchars(){
sed -n '/^.\{$nchars\}/!p' $@
}
print_substring_after_matching_pattern(){
sed -n -e 's/^.*'$pattern' //p' $@
}
print_word_after_matching_pattern(){
sed -n -e 's/^.*'$pattern' //p' $@
}
comment_all_lines(){
sed '/s/^/#/g' $@
}
comment_lines_from_start_to_end(){
sed "$start,$end {s/^/#/}" $@
}
uncomment_lines_from_start_to_end(){
sed "$start,$end {s/'^#'//}" $@
}
uncomment_lines_matching_pattern(){
sed '/'$pattern'/s/^/#/g' $@
}
uncomment_all_lines(){
sed 's/^#//g' $@
}
change_word_matching_pattern_to_uppercase(){
sed -r "s/\<'$pattern'[a-z]+/\U&/g" $@
}
join_two_lines_backslash(){
sed ':a; /\$/N; s/\\n//; ta' $@
}
set="[0-9]"
sed 's/'$set'//g'
sed '/'$pattern'/s@^.*$@'$before'&'$after'@g'
sort_paragraphs_alphabetically(){
(sed '/./{H;d;};x;s/\n/={NL}=/g'| sort | sed '1s/={NL}=//;s/={NL}=/\n/g')
}
print_paragraph_containing_pattern(){
sed '/./{H;$!d;};x;/'$pattern'/!d' $@
}
insert_linebreak_below_matching_lines(){
sed '/'$pattern'/G' $@
}
insert_linebreak_above_matching_lines(){
sed '/'$pattern'/{x;p;x;}' $@
}
insert_linebreak_between_matching_lines(){
sed '/'$pattern'/{x;p;x;G;}' $@
}
delete_last_line_of_each_paragraph(){
sed -n '/^$/{p;h;};/./{x;/./p;}' $@
}
print_every_nthline_starting_with_linex(){
sed -n '$nthline~$linexp' $@
}
delete_lines_matching_pattern(){
sed '/'$pattern'/d' $@
}
delete_all_blank_lines(){
sed '/./!d' $@
}
delete_all_blank_whitespace_lines(){
sed "/^\s*$/d" $@
}
delete_all_consecutive_blank_lines_except_end(){
sed '/./,/^$/!d' $@
}
delete_all_consecutive_blank_lines_except_start(){
sed '/^$/N;/\n$/D' $@
}
sed '/^$/N;/\n$/N;//D'
sed -e :a -e '/^\n*$/{$d;N;};/\n$/ba'
sed '"$start"~"$nth"d'
sed '/./,$!d'
read_lines_bottom_to_top(){
sed '1!G;h;$!d' $@
}
read_lines_right_to_left(){
sed '/\n/!G;s/\(.\)\(.*\n\)/&/;//D;s/.//' $@
}
number_lines_tab_delimited(){
sed = $@ | sed 'N;s/\n/\t/' $@
}
remove_html_tags(){
sed -e :a -e 's/<[^>]*>//g;/</N;//ba' $@
}
join_pathAbsolute_with_pathRelative(){
sed 's@'$pathAbsolute'@&'/$pathRelative'@g' $@
}