LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-23-2017, 07:26 AM   #1
Entropy1024
Member
 
Registered: Dec 2012
Location: UK
Distribution: Ubuntu 16 & 17
Posts: 130

Rep: Reputation: Disabled
Script to remove characters in text file


Hello all. I need to write a script that will open a text file and find a list of characters strings such as &37, &55 and remove them.

So if there was a line as follows:
This is &37 a &55 quick example

I want it to rewrite the file so that it now reads:
This is a quick example.

If it was just the two characters I needed to remove then I would just use a find/replace on Nano or similar, however there are about 60 of these special characters and a script would be much faster.

Many thanks for any help.

Regards
Tim
 
Old 01-23-2017, 07:58 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 & 7
Posts: 3,208

Rep: Reputation: 869Reputation: 869Reputation: 869Reputation: 869Reputation: 869Reputation: 869Reputation: 869
http://www.grymoire.com/Unix/Sed.html
 
1 members found this post helpful.
Old 01-23-2017, 08:08 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,719

Rep: Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034
Vim could easily do the same, but sed would the tool of choice at the command line.
 
1 members found this post helpful.
Old 01-23-2017, 08:13 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,066

Rep: Reputation: 4131Reputation: 4131Reputation: 4131Reputation: 4131Reputation: 4131Reputation: 4131Reputation: 4131Reputation: 4131Reputation: 4131Reputation: 4131Reputation: 4131
additionally sed and vim understand the same command.
 
1 members found this post helpful.
Old 01-23-2017, 11:57 AM   #5
Entropy1024
Member
 
Registered: Dec 2012
Location: UK
Distribution: Ubuntu 16 & 17
Posts: 130

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by smallpond View Post
Many thanks for all the replies. I will check out Sed & Vim.

Cheers
Tim
 
Old 01-23-2017, 12:06 PM   #6
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,237

Rep: Reputation: 1651Reputation: 1651Reputation: 1651Reputation: 1651Reputation: 1651Reputation: 1651Reputation: 1651Reputation: 1651Reputation: 1651Reputation: 1651Reputation: 1651
If they are truly 'special characters' like \n and \r or some HEX or OCTAL or UNICODE characters, then you might save time using 'tr' to transform and replace them.

http://www.linfo.org/tr.html
https://linux.die.net/man/1/tr
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Shell script to remove selected line number from text file. ChocaMocha Programming 4 04-25-2013 03:11 PM
Substituting some characters in a text file (shell script). stf92 Programming 20 07-04-2012 01:30 AM
script to remove text from file brixtoncalling Programming 19 10-04-2009 01:04 AM
How to remove string in the text file ? Bash script dlugasx Linux - Server 9 06-05-2009 11:40 AM
remove text from file with script paul_mat Linux - Software 3 11-17-2005 12:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration