sed_notes
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
sed_notes [2020/10/02 20:18] – created raju | sed_notes [2021/01/28 21:50] (current) – raju | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | |||
+ | ==== 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 | ||
+ | < | ||
+ | sed -e " | ||
+ | </ | ||
+ | |||
+ | Sample command: | ||
+ | < | ||
+ | sed -e "1 e cat part.txt" | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | $ 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 | ||
+ | |||
+ | $ start=1; end=2; sed -e " | ||
+ | x y z | ||
+ | p q r | ||
+ | s t u | ||
+ | g h i | ||
+ | j k l | ||
+ | |||
+ | $ start=2; end=4; sed -e " | ||
+ | a b c | ||
+ | x y z | ||
+ | p q r | ||
+ | s t u | ||
+ | |||
+ | $ start=2; end=3; sed -e " | ||
+ | a b c | ||
+ | x y z | ||
+ | p q r | ||
+ | s t u | ||
+ | j k l | ||
+ | |||
+ | $ start=2; end=2; sed -e " | ||
+ | a b c | ||
+ | x y z | ||
+ | p q r | ||
+ | s t u | ||
+ | g h i | ||
+ | j k l | ||
+ | </ | ||
+ | |||
+ | ==== insert file at the beginning of another file ==== | ||
+ | This will insert part.txt at the beginning of main.txt. | ||
+ | |||
+ | < | ||
+ | sed -e "1 e cat part.txt" | ||
+ | </ | ||
+ | |||
+ | Example: | ||
+ | < | ||
+ | $ 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" | ||
+ | x y z | ||
+ | p q r | ||
+ | s t u | ||
+ | a b c | ||
+ | d e f | ||
+ | g h i | ||
+ | j k l | ||
+ | </ | ||
+ | |||
+ | Found it in: https:// | ||
+ | |||
+ | ==== backup with timestamp ==== | ||
+ | tags | sed create backup, YYYYMMDD_HHMMSS | ||
+ | |||
+ | < | ||
+ | sed " | ||
+ | </ | ||
+ | will backup to something like main.txt_asof_20210128_151656 | ||
+ | |||
+ | < | ||
+ | sed " | ||
+ | </ | ||
+ | will backup to something like main.txt.2021-01-28 | ||
+ | |||
+ | ==== sed do not change line endings ==== | ||
+ | < | ||
+ | sed -b | ||
+ | </ | ||
+ | |||
+ | Ref:- https:// | ||
==== replace double quote with single quote ==== | ==== replace double quote with single quote ==== | ||
sed " | sed " | ||
Ref:- https:// | Ref:- https:// | ||
+ | |||
+ | Sample run: | ||
+ | < | ||
+ | % sed " | ||
+ | a | ||
+ | a | ||
+ | " | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | </ |
sed_notes.1601669915.txt.gz · Last modified: 2020/10/02 20:18 by raju