1

I have been trying to manipulate a file using tr and have a task on removing all punctuation in one file and moving the contents into another.

cat file1 | tr --delete [:punct:] | cp file1 file2

When this command is used I'm returned with a preview of the file with no punctuation but when I look at file2, all the punctuation is still there.

Is there something I'm doing wrong?

Joaquin
  • 2,013
  • 3
  • 14
  • 26
  • 1
    It's not clear what that script is supposed to do. Please provide concise, testable sample input and expected output to complete the required [mcve]. See [ask] if that's not clear. – Ed Morton Aug 12 '18 at 15:56
  • You didn't understand what a pipe (`|`) does. – Cyrus Aug 12 '18 at 16:11
  • Possible duplicate of [Redirect all output to file](https://stackoverflow.com/q/6674327/608639), [How can I redirect and append both stdout and stderr to a file with Bash?](https://stackoverflow.com/q/876239/608639), etc. – jww Aug 12 '18 at 21:35
  • I'm voting to close this question as off-topic because it is offtopic here but ontopic on the Unix se. – peterh Aug 13 '18 at 11:16

2 Answers2

4

You aren't changing file1, so the last cp just copies the old file. cp doesn't take input from stdin either, so you're not saving that translated output.

You can resolve this by piping your tr output directly into file2 with a > redirect.

tr --delete '[:punct:]' < file1 > file2

Note that I've also removed the useless use of cat.

Neil Locketz
  • 4,228
  • 1
  • 23
  • 34
  • uuoc is contentious and of no relevance to the question. Performance wise the extra process is unlikely to cause a problem in this case and certainly isn't what original question was about. – Iain Aug 13 '18 at 01:35
  • @lain, Nowhere did I say it was better. I also linked to the post so they could read about it themselves. I prefer it without cat, so I make the change. It was also an afterthought in the post; chill out – Neil Locketz Aug 13 '18 at 01:39
  • Yeah, ok. I prefer the pure pipeline, reading left to right. It's easier to visualize and easier to build on. – Iain Aug 13 '18 at 01:55
-2

Looks like you need a redirect rather than a pipe. That last command is not reading stdio, just copying the original file1 to file2. Try:

cat file1 | tr --delete [:punct:] > file2
Iain
  • 300
  • 2
  • 13