User Tools

Site Tools


sed_notes

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
sed_notes [2021/01/28 20:16] – [backup with timestamp] rajused_notes [2021/01/28 21:50] (current) raju
Line 1: Line 1:
  
 ==== replace text between two lines with a file ==== ==== replace text between two lines with a file ====
 +tags | line numbers, sed command to insert a file
  
 Replace lines between \$start and \$end in main.txt with contents of part.txt Replace lines between \$start and \$end in main.txt with contents of part.txt
Line 52: Line 53:
 j k l j k l
 </code> </code>
 +
 +==== insert file at the beginning of another file ====
 +This will insert part.txt at the beginning of main.txt.
 +
 +<code>
 +sed -e "1 e cat part.txt" main.txt
 +</code>
 +
 +Example:
 +<code>
 +$ cat main.txt
 +a b c
 +d e f
 +g h i
 +j k l
 +
 +$ cat part.txt
 +x y z
 +p q r
 +s t u
 +
 +$ sed -e "1 e cat part.txt" main.txt
 +x y z
 +p q r
 +s t u
 +a b c
 +d e f
 +g h i
 +j k l
 +</code>
 +
 +Found it in: https://unix.stackexchange.com/questions/337435/sed-insert-file-at-top-of-another
  
 ==== backup with timestamp ==== ==== backup with timestamp ====
-tags | sed create backup+tags | sed create backup, YYYYMMDD_HHMMSS
  
 <code> <code>
-sed "-i_asof_`date +%Y%m%d_%M%S`" -e 's/foo/bar/g' main.txt+sed "-i_asof_`date +%Y%m%d_%H%M%S`" -e 's/foo/bar/g' main.txt
 </code> </code>
-will backup to something like main.txt_asof_20210128_2231+will backup to something like main.txt_asof_20210128_151656
  
 <code> <code>
Line 65: Line 98:
 </code> </code>
 will backup to something like main.txt.2021-01-28 will backup to something like main.txt.2021-01-28
 +
 +==== sed do not change line endings ====
 +<code>
 +sed -b
 +</code>
 +
 +Ref:- https://stackoverflow.com/questions/4652652/preserve-line-endings
 ==== replace double quote with single quote ==== ==== replace double quote with single quote ====
   sed "s/\"/'/g"   sed "s/\"/'/g"
sed_notes.1611864980.txt.gz · Last modified: 2021/01/28 20:16 by raju