User Tools

Site Tools


notes:sed

Sed Stuff

I use this a lot in my scripts - it is about time I keep a proper reference page for it.

Basic Manipulation

Append specific line

sed '1a this_line' test.txt

Remove all comments before 'start' and after 'stop' lines

sed -e '1,/start/ s/#.*//' -e '/stop/,$ s/#.*//'

Insert text this_line in file test.txt BEFORE a match (e.g. line starting with find=). Note: use -i to actually change the file

sed '/^find=.*/i this_line' test.txt

Insert text this_line in file test.txt AFTER a match (e.g. line starting with find=).

sed '/^find=.*/a this_line' test.txt

Insert text this_line in file test.txt IN-PLACE of a match (e.g. line starting with find=).

sed 's/^find=.*$/this_line/' test.txt

Slightly Advanced

Modify the line AFTER a match

sed '/^find=.*/!b;n;s/^\(.*\)=\(.*\)$/\1=changed/' test.txt

Note The !b tells sed to break current processing and n starts the next line

Prints lines in between two lines starting with – CLUS (excluding matching lines)

sed -n "/^-- CLUS:{$pick/,/^-- CLUS/{/^-- CLUS/!p}" test.txt

more… coming soon…

Sed codes

Dumped

sed

:  # label
=  # line_number
a  # append_text_to_stdout_after_flush
b  # branch_unconditional             
c  # range_change                     
d  # pattern_delete_top/cycle          
D  # pattern_ltrunc(line+nl)_top/cycle 
g  # pattern=hold                      
G  # pattern+=nl+hold                  
h  # hold=pattern                      
H  # hold+=nl+pattern                  
i  # insert_text_to_stdout_now         
l  # pattern_list                       
n  # pattern_flush=nextline_continue   
N  # pattern+=nl+nextline              
p  # pattern_print                     
P  # pattern_first_line_print          
q  # flush_quit                        
r  # append_file_to_stdout_after_flush 
s  # substitute                                          
t  # branch_on_substitute              
w  # append_pattern_to_file_now         
x  # swap_pattern_and_hold             
y  # transform_chars                   

sed -n '/^\[[0-9]*\]$/{h;:l;n;s/^$//;td;s/^{[0-9]*}$//;td;H;bl;:d;x;p;q}'

sed -n '/^\[[0-9]*\]$/{h};/^{[0-9]*}$/{x;p;x;p}'

sed -n '/^\[[0-9]*\]$/{h};/^{[0-9]*}$/{x;s/\n/ /g;Ts;p;s/^\(\[[0-9]*\]\).*$/\1/;:s;G;x;d};/^$/{x;s/\n/ /g;p;d};${x;s/\n/ /g;p;d};/^@/{H};/^#/{H};/^+/{H}'

My old notes - will sort this later

sed-notes
# convert text file into c string
sed -e 's/\\/\\\\/g;s/"/\\"/g;s/\t/\\t/g;s/^/"/;s/$/\\n"/' <file>
 
# something else... with explanation
sed -n '/## Screensh/,/##/{/Scree/{p;n};/##/{q};p}' file
 
-n                 -> not print
'/## Screen/, /##/ -> match range, I guess you knew it already
{                  -> if in this range
    /Scree/        -> and line matches /Screenshot/
        {p;n};     -> do print line, and read next row (skip doing rest processing)
    /##/           -> if line matches "##"
        q;         -> quit, we have done all printing
    p              -> if we come to here, print the line
}
notes/sed.txt · Last modified: 2023/08/29 10:43 by 127.0.0.1