Table of Contents
If you want to send us your comments, please do so. Thanks
More on comments
sed
Manuals
man sed
info sed
sed FAQ
sed cheatsheet
Tokens
Token | Explanation |
---|---|
[ | Beginning of a character group |
] | End of a character group |
^ | Do not match the following characters |
* | Repeat the preceding item zero or more times |
$ | Match the end of line |
Commands
In a script
Command | Remark |
---|---|
sed 's/x/ /g' | Replace x with 7 spaces. Do not forget the “ ' ” 's! |
sed 's/x/\t\t\t/g' | Relace x with 3 tabs. Do not forget the “ ' ” 's! |
On the commandline
Command | Remark |
---|---|
sed 's/somestring//' file.txt | Delete the string somestring from file.txt and and display the output on the standard output i.e. the terminal |
sed '10 d' text123.txt | Delete the 10th line in text123.txt and display the output on the standard output i.e. the terminal |
sed '/^\./ d' text123.txt | Delete all lines starting with a . (dot) in file text123.txt and display to the standard output |
sed --quiet '/somestring/!p' somefile.txt > otherfile.txt | --quiet: suppress automatic printing of pattern space; p: Print the current pattern space. Remove all lines containing somestring from somefile.txt and save the result in otherfile.txt |
sed s/text123/TEXT456/g file.txt > outfile.txt | Replace text123 with TEXT456 in file.txt and put the output in outfile.txt. < file.txt also works |
echo 'hello.txt' | sed 's|txt$|org|' | Replace txt with org |
echo 'hello.txt' | sed 's/txt$/org/' | Replace txt with org |
echo one.two.three | sed 's/../-/' | Replace the first any two characters with a '-'. Result: -e.two.three |
echo one.two.three | sed 's/two…/-/' | Replace the 'two' and the three characters following it with a '-'. Result: one.-ree |
sed 's/^[ \t]*//' sed 's/^[[:space:]]*//g' | Remove all spaces on the beginning of a line |
sed -i '/^$/d' file.txt | Delete all blank lines including blank lines containing spaces |
sed -i "/^ *$/d" file.txt | Delete all lines only containing spaces |
sed -i "/^\s*$/d" file.txt | Delete all blank lines including blank lines containing spaces and tabs |
sed -i '1 i\text_to_be_put_on_line_1' somefile | Add the text_to_be_put_on_line_1 on line 1 of somefile |
sed 's/[^| \t\n]//g' fille.in.txt > file.out.txt | Remove all characters from a file except spaces and newline characters |
Insert / Add
Insert a newline character
Replace the , between the { and } with a new line character in the whole file
cat somefile | sed 's/},{/}\n{/g' > someotherfilename.txt sed 's/,/"\n"/g' < file1.txt > file2.txt
Add text beginning line
Add text at the beginning of a line
echo line | sed 's/^/sometext/'
Add text end line
Add text at the end of a line
echo line | sed 's/$/sometext/'
Add dashes to a date
echo 20200326 | sed 's/./&-/4' | sed 's/./&-/7'
Probaly this can be done easier. If you know how please let us know
To use this in Calc or Excel paste a column in to a text editor and save the file to /tmp/filename.txt
In a terminal on Linux (on Windows t.b.d.) run
for i in `cat /tmp/filename.txt`; do echo $i | sed 's/./&-/4' | sed 's/./&-/7' >> otherfile.txt; done
Open otherfile.txt and paste the column back in the spreadsheet
Replace
Search and replace examples
A non breaking space
Replace an UTF-8 non braking space (hex: c2a0) with a “;” (semi colon)
sed 's/\xC2\xA0/;/g'
A newline character
file.txt is a file you can make up wich contains multiple lines. The newline character will be replaced by a space
-z --null-data : separate lines by NUL characters
This also works when the input is from a pipe ( | )
sed --null-data 's/\n/ /g' file.txt # Also works when the input is from a pipe ( | ) cat file.txt | sed --null-data 's/\n/ /g' sed --null-data 's/\n/\<br\>\n/g' file.txt > filewithhtmlbreakline.txt sed --null-data 's/\n/\<br\>\n/g' < file.txt > filewithhtmlbreakline.txt sed -z 's/\x0D\x0A/\x0A/g' file.in > file.out # Replace the carriage return linefeed sequence with a linefeed. The -z (--null-data option is obligatory) sed -z 's/\x0D\x0A/\x0A/g' file.in | sed -z 's/\x0D\x0D/\x0A/g' | sed -z 's/\x0A\x0D/\x0A/g' | sed -z 's/\x0D/\x0A/g' > file.out # To make sure you got them all
On the other hand:
sed is not ment to replace a newline characters since it works on lines, not files. See the sed FAQ 5.10. Why can't I match or delete a newline using the \n escape sequence? Why can't I match 2 or more lines using \n?
How can I replace a newline (\n) using sed
Solution
- The x character is a character that is not in the string (the line sed is processing)
- Replace a newline character “ \n ” with four tabs “ \t ” in file filename.txt
cat filename.txt | tr '\n' x | sed 's/x/\t\t\t\t/g'
Replace \n with “\n” in filename.txt
cat filename.txt | tr '\n' @ | sed 's/\@/\"\n\"/g' > filename.quotes.txt
Characters on the end of the line
Work in progress
echo -e "one:123: two\none:456: two\none:789: two" | sed 's/\([0-9]\+\)/(\1,0)/g' echo -e "one:123: two\none:456: two\none:789: two" | sed 's/[0-9]\{1,\}/(&,0)/' echo -e "one:123one:456one:789" | sed 's/[0-9]\{1,\}/&\n/g' sed 's/,[^,]*$/,one/' (replace everything from the first comma to the end of the line with 'one') sed 's/one.*$/two/g' (replace everything from one and all what is following it to the end of the line with 'two') echo one.two.three | sed 's/..$/-/' (replace the last two characters with '-') echo -e "one:123 two:456 three:789" | sed 's/\([0-9]\+\)/\1^/g' | tr -d " " | tr "^" '\n'
Print selected lines
numbers.txt
1 2 3 4 5 6 7 8 9 10 11 12
Print lines 3,4 and 5 then quit
sed -n '3,5p' numbers.txt
Output:
3 4 5
Useful links
Delete all characters between two strings
Removing text between two specific strings
Main subjects on this wiki: Linux, Debian, HTML, Microcontrollers, Privacy
RSS
Disclaimer
Privacy statement
Bugs statement
Cookies
Copyright © : 2014 - 2024 Webevaluation.nl and the authors
Changes reserved.