How to strip all of the text out of a text file and leave only the numbers and formatting. And other sed tricks.

Posted: August 5, 2017. At: 10:40 PM. This was 4 months ago. Post ID: 6089
Page permalink.
WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters. These cookies expire two weeks after they are set.

How to strip all numbers out of a text stream

This is how to remove all text from the output of a file and only keep the other formatting.

homer@deep-thought ~/Desktop/b $ fortune -l | sed s/[a-z]/\ /gi;
       :                                        3:15  . .               
                                          -                    .
 
  ,                    !
		--               '84

A possibly useless but interesting use of sed. Here I am removing only the numbers from text output. This is another use of sed. The backspace in the sed regex allows us to have a space character to replace the numbers.

homer@deep-thought ~/Desktop/b $ fortune -l | sed s/[0-9]/\ /gi;
Q:	How many IBM types does it take to change a light bulb?
A:	Fifteen.  One to do it, and fourteen to write document number
	GC       -    , Multitasking Incandescent Source System Facility,
	of which   % of the pages state only "This page intentionally
	left blank", and   % of the definitions are of the form "A:.....
	consists of sequences of non-blank characters separated by blanks".

Here I am replacing all instances of the lowercase ‘a’ with an uppercase ‘A’.

homer@deep-thought ~/Desktop/b $ fortune -l | sed s/[a]/A/gi;
Q:	MinnesotAns Ask, "Why Aren't there more phArmAcists from AlAbAmA?"
A:	EAsy.  It's becAuse they cAn't figure out how to get the little
	bottles into the typewriter.

Here I am removing all of the ‘<' and '>‘ characters from a html file.

cat index.php | head -n 10 | sed s/[\<\>]/\/gi;

And here is the result.

homer@deep-thought ~/Documents $ cat index.php | head -n 10 | sed s/[\<\>]/\/gi;
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"
head
	titleSecuritron GNU/Linux pages./title
	meta name="generator" content="Vim. Debian GNU/Linux." /
	meta content="text-html; charset=UTF-8" http-equiv="Content-Type" /
	meta name="description" content="Securitron GNU/Linux pages. Help and support for Linux and UNIX users." /
	meta name="keywords" content="GNU/Linux, help, tips, code, Ubuntu, UNIX" /
	link href="style.css" rel="stylesheet" type="text/css" /

No comments have been made. Use this form to start the conversation :)

Leave a Reply