Back (Current repo: scraps)

random scraps and notes that are useful to me
To clone this repository:
git clone https://git.viktor1993.net/scraps.git
Log | Download | Files | Refs

commit 1814824a2e55a2bd088c2040b9ac4edd7e36627e
parent 9ba75222b8ca59ab61ed77993a59cde8fc84fae7
Author: root <root>
Date:   Mon, 21 Apr 2025 15:02:13 +0200

add some functions

Diffstat:
Ased/flatten_newlines.sh | 18++++++++++++++++++
Ased/replace_match_and_next2.sh | 33+++++++++++++++++++++++++++++++++
2 files changed, 51 insertions(+), 0 deletions(-)

diff --git a/sed/flatten_newlines.sh b/sed/flatten_newlines.sh @@ -0,0 +1,18 @@ +function flatten_newlines() { + + FILE=$1 + + if [ -z $FILE ]; then + echo "A file is needed" + return 1 + fi + + sed ':m;$!{N;s/\n/ /;bm;}' "$FILE" + + #Explanation for the random wanderer looking in here: + #:m -> define label m + #$!{} -> If current line is not the last line + #N -> append next line to pattern space, aka join the lines + #s\n/ / -> replace the newline between them with a space + #b m -> loop back to label m +} diff --git a/sed/replace_match_and_next2.sh b/sed/replace_match_and_next2.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +function replace_match_and_next2() { + + PAT=$1 + FILE=$2 + + if [ -z $PAT ]; then + echo "A pattern is needed"; + return 1; + fi + + if [ -z $FILE ]; then + echo "A file is needed"; + return 1; + fi + + sed -n -e '/'$PAT'/!p;: m' -e '//{' -e '$!{' -e 'n;n;b m' -e '}' -e'}' $FILE + + #Explanation for the random wanderer looking in here: + #sed -n \ + # -e '/'$PAT'/!p' print lines that do NOT match $PAT + # -e ':m' define label "m" + # -e '//{' if line matches $PAT again + # -e ' $!{' and it's NOT the last line + # -e ' n; n; b m' skip next two lines and go back to label "m" + # -e ' }' end inner conditional + # -e '}' end outer conditional + + #sometimes this can be useful for cutting parts from responses that are not needed + #but have no control over + +}