LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   delete a specific string (http://www.linuxquestions.org/questions/programming-9/delete-a-specific-string-828901/)

cruzdelsur 08-27-2010 05:41 PM

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!!!

crts 08-27-2010 06:05 PM

Hi,

is this what you are looking for?
Code:

sed -e 's/3456//g' input > output

sycamorex 08-27-2010 06:15 PM

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.

cruzdelsur 08-27-2010 06:23 PM

That's just what I wanted and needed!! great, great!!! Thank you very much!!! :):)

ghostdog74 08-27-2010 07:00 PM

Quote:

Originally Posted by sycamorex (Post 4080124)
If you don't want to create another file,

there is actually another file, which one thinks one don't "see".

sycamorex 08-27-2010 07:03 PM

Quote:

Originally Posted by ghostdog74 (Post 4080155)
there is actually another file, which one thinks one don't "see".

huh???

Does it create a temporary file?

ghostdog74 08-27-2010 07:04 PM

Quote:

Originally Posted by cruzdelsur (Post 4080099)
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<&-


crts 08-27-2010 07:25 PM

Quote:

Originally Posted by sycamorex (Post 4080158)
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.


All times are GMT -5. The time now is 06:19 AM.