LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-27-2010, 05:41 PM   #1
cruzdelsur
Member
 
Registered: Aug 2010
Location: Chile
Distribution: Ubuntu 12.04
Posts: 69

Rep: Reputation: 0
delete a specific string


Hello guys!!
These must a be a question you received a lot, but I couldn't find a similar thread! It's a simple question.
I need to delete a specific string from a file. For example the specific numbers "3456" from a file. I've tried with the tr comand, using
Code:
tr -d '3456' file,
but it also deleted whichever ocurrence of 3,4,5,6 from my file. Should I use a regular expression? Can anyone help me?
Thanks!!!
 
Old 08-27-2010, 06:05 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Hi,

is this what you are looking for?
Code:
sed -e 's/3456//g' input > output
 
Old 08-27-2010, 06:15 PM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,572
Blog Entries: 1

Rep: Reputation: 1027Reputation: 1027Reputation: 1027Reputation: 1027Reputation: 1027Reputation: 1027Reputation: 1027Reputation: 1027
If you don't want to create another file, you could use sed's -i flag:
Code:
sed -i 's/3456//g' input_file
This will modify the input file.

or

Code:
sed -i.new 's/3456//g' input_file
This will leave the 'input_file' untouched and create input_file.new with your modifications.
 
Old 08-27-2010, 06:23 PM   #4
cruzdelsur
Member
 
Registered: Aug 2010
Location: Chile
Distribution: Ubuntu 12.04
Posts: 69

Original Poster
Rep: Reputation: 0
That's just what I wanted and needed!! great, great!!! Thank you very much!!!
 
Old 08-27-2010, 07:00 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by sycamorex View Post
If you don't want to create another file,
there is actually another file, which one thinks one don't "see".
 
Old 08-27-2010, 07:03 PM   #6
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,572
Blog Entries: 1

Rep: Reputation: 1027Reputation: 1027Reputation: 1027Reputation: 1027Reputation: 1027Reputation: 1027Reputation: 1027Reputation: 1027
Quote:
Originally Posted by ghostdog74 View Post
there is actually another file, which one thinks one don't "see".
huh???

Does it create a temporary file?
 
Old 08-27-2010, 07:04 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by cruzdelsur View Post
Hello guys!!
These must a be a question you received a lot, but I couldn't find a similar thread! It's a simple question.
I need to delete a specific string from a file. For example the specific numbers "3456" from a file. I've tried with the tr comand, using
Code:
tr -d '3456' file,
but it also deleted whichever ocurrence of 3,4,5,6 from my file. Should I use a regular expression? Can anyone help me?
Thanks!!!
tr is not the tool to delete a number of characters. use tools like sed/awk or any that can read and write files. With that, even you can do it with the shell

Code:
exec 4<"file"
while read -r line <&4
do
  case "$line" in 
   *"3456"*) line="${line//3456/}"
  esac
  echo "$line"
done > tmp
mv tmp file
exec 4<&-
 
Old 08-27-2010, 07:25 PM   #8
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by sycamorex View Post
huh???

Does it create a temporary file?
Yes, the -i option always creates a temporary file. Even if there are no changes and you do just something like
Code:
sed -i -n 'p' file
You can verify this, e.g. by processing a very large file, so that sed will need a few seconds to finish. While sed is running hit Ctrl-C. Then do an 'ls' and see what the temp-file has been named. On my system it is something like sedxxxxxx, with xxxxxx being some random string.

Last edited by crts; 08-27-2010 at 07:28 PM.
 
  


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] C - How to put a specific arbitrary part of a string into it's own string? golmschenk Programming 9 04-19-2010 08:27 PM
Remove files that contain a specific string poymode Linux - General 5 02-17-2010 03:01 AM
read string after specific string from a text file using C++ programing language badwl24 Programming 5 10-08-2009 05:41 AM
Shell Script: Delete lines til string found or until particular string. bhargav_crd Linux - General 3 12-20-2007 11:14 PM
How to find those files having specific string??? saeed Red Hat 4 07-19-2006 11:50 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration