sed '' BSD
sed -n 'p' BSD
sed -n '1p' BSD
sed -n '1,5p' BSD
sed -n '1,+4p' BSD
If we want to print every other line, we can specify the interval after the "~" character. The following line will print every other line starting with line 1:
sed -n '1~2p' BSD
Deleting Text
delete every other line starting with the first:
sed '1~2d' BSD
sed -i '1~2d' everyother.txt
To create a backup file prior to editing, add the backup extension directly after the "-i" option:
sed -i.bak '1~2d' everyother.txt
Substituting Text
's/old_word/new_word/'
sed 's/on/forward/' annoying.txt
We will provide the "g" flag to the substitute command by placing it after the substitution set.
sed 's/on/forward/g' annoying.txt
If we only wanted to change the second instance of "on" that sed finds on each line, then we could use the number "2" instead of the "g".
sed 's/on/forward/2' annoying.txt
see which lines were substituted
sed -n 's/on/forward/2p' annoying.text
Ignore case:
sed 's/SINGING/saying/i' annoying.txt
Referencing Matched Text
put parentheses around the matched text:
sed 's/^.*at/(&)/' annoying.txt
sed 's/\([a-zA-Z0-9][a-zA-Z0-9]*\) \([a-zA-Z0-9][a-zA-Z0-9]*\)/\2 \1/' annoying.txt
sed 's/\([^ ][^ ]*\) \([^ ][^ ]*\)/\2 \1/' annoying.txt
How to keep only every nth line of a file
bash - How to keep only every nth line of a file - Super User
awk 'NR == 1 || NR % 3 == 0' yourfile
sed -n '1p;0~3p' input.txt
Read full article from The Basics of Using the Sed Stream Editor to Manipulate Text in Linux | DigitalOcean
sed -n 'p' BSD
sed -n '1p' BSD
sed -n '1,5p' BSD
sed -n '1,+4p' BSD
If we want to print every other line, we can specify the interval after the "~" character. The following line will print every other line starting with line 1:
sed -n '1~2p' BSD
Deleting Text
delete every other line starting with the first:
sed '1~2d' BSD
sed -i '1~2d' everyother.txt
To create a backup file prior to editing, add the backup extension directly after the "-i" option:
sed -i.bak '1~2d' everyother.txt
Substituting Text
's/old_word/new_word/'
sed 's/on/forward/' annoying.txt
We will provide the "g" flag to the substitute command by placing it after the substitution set.
sed 's/on/forward/g' annoying.txt
If we only wanted to change the second instance of "on" that sed finds on each line, then we could use the number "2" instead of the "g".
sed 's/on/forward/2' annoying.txt
see which lines were substituted
sed -n 's/on/forward/2p' annoying.text
Ignore case:
sed 's/SINGING/saying/i' annoying.txt
Referencing Matched Text
put parentheses around the matched text:
sed 's/^.*at/(&)/' annoying.txt
sed 's/\([a-zA-Z0-9][a-zA-Z0-9]*\) \([a-zA-Z0-9][a-zA-Z0-9]*\)/\2 \1/' annoying.txt
sed 's/\([^ ][^ ]*\) \([^ ][^ ]*\)/\2 \1/' annoying.txt
How to keep only every nth line of a file
bash - How to keep only every nth line of a file - Super User
awk 'NR == 1 || NR % 3 == 0' yourfile
sed -n '1p;0~3p' input.txt
Read full article from The Basics of Using the Sed Stream Editor to Manipulate Text in Linux | DigitalOcean
No comments:
Post a Comment