0

How would you remove a string that repeats in most lines of a list with bash?

E.G.

My list looks like this:

Rex Rocket Steam Game
Magdalena Steam Game
FLASHOUT 2 Steam Game
Falcon
Girls Like Robots Steam Game
The Land Of Lamia Steam Game
Aeon Command

And I want to remove all the "Steam Game" string from all the lines that end that way.

I'm super rusty, this looks so easy but I can't figure it out.

Мона_Сах
  • 322
  • 3
  • 12
te0k
  • 21

1 Answers1

4

There's many options, sed is probably the simplest.

$ sed 's/Steam Game$//' foo.txt        
Rex Rocket 
Magdalena 
FLASHOUT 2 
Falcon
Girls Like Robots 
The Land Of Lamia 
Aeon Command

You asked for "Steam Game" to be removed not " Steam Game". If you want the space also removed, add a space to the regex: 's/ Steam Game$//'. Or use 's/ *Steam Game$//' if there are more than one space.

rrauenza
  • 6,285
  • 4
  • 32
  • 57
  • This leaves a space at the end of every line it was removed from. – Benjamin W. Jun 20 '16 at 04:21
  • Thanks! That works great. Now the problem is when I redirect that command to a file it removes all newlines from every row. Any ideas? – te0k Jun 20 '16 at 04:33
  • That's really odd. How exactly are you running it? You're not using backticks are you? – rrauenza Jun 20 '16 at 04:36
  • Linux? MacOS? Is this a windows file? – rrauenza Jun 20 '16 at 04:47
  • I'm using cygwin in Win10. Didn't know this could affect the outcome. – te0k Jun 20 '16 at 04:52
  • http://stackoverflow.com/questions/4652652/preserve-line-endings - add `--binary` to the command line. I think what you're seeing is the `\r\n` converted to `\n`. If you're using notepad to check, notepad ignores `\n`. Try [Programmers Notepad](http://www.pnotepad.org/). – rrauenza Jun 20 '16 at 04:58
  • For some reason, adding --binary makes the command not to work. However, you're right. Opened the same file with WordPad and the newlines are there. No idea why Notepad doesn't respect them. Thank you very much for your help!!! – te0k Jun 20 '16 at 06:46
  • @te0k : `'m using cygwin in Win10.` That information usually goes in the question. – Мона_Сах Jun 20 '16 at 08:04