13

I have about 100 .txt files that contain plain text. Somehow, some of the data has been corrupted and needs to be found/replaced.

I need to search for the characters'--' and replace it with a long dash: '—'.

Is there a way to do this quickly with a command in terminal?

The names of the .txt files in my directory are numbered sequentially: 1.txt, 2.txt, etc.

Thanks!

Lizza
  • 2,571
  • 4
  • 36
  • 68
  • possible [duplicate](http://stackoverflow.com/q/8384809/148680) – chb Aug 10 '12 at 03:52
  • Komodo Edit (http://www.activestate.com/komodo-edit) has a "Find/Replace In Files" feature. Its a free editor for Mac, Windows and Linux. – jsherk Apr 04 '13 at 20:38

2 Answers2

37

GNU sed:

sed -i 's/--/—/g' *.txt

OSX BSD sed:

You need to specify a backup file extension. To create a backup file with the extension: .txt.bak:

sed -i '.bak' 's/--/—/g' *.txt

To completely replace the files, specify an empty extension:

sed -i '' 's/--/—/g' *.txt
Steve
  • 41,445
  • 12
  • 83
  • 96
  • @Casper: Glad I could help :-) – Steve Aug 10 '12 at 04:13
  • What’s the significance of the heading “On OSX”? Does the first example not work on OS X? [Reads @aid’s answer.] Oh. Could you say something about the first example and why it’s different and on which platforms it’s different? Also, is the second example specific to OS X or does it apply to BSD? – Chris Page Aug 11 '12 at 00:34
  • @ChrisPage: When I first wrote this answer, I didn't see that the question was tagged with 'OSX'. The first example will only work using GNU `sed`. Personally, I don't have much experience with other versions of `sed`, but the second example should work using OSX/BSD `sed`. You may find this helpful: http://stackoverflow.com/questions/5694228/sed-in-place-flag-that-works-both-on-mac-and-linux – Steve Aug 11 '12 at 02:04
3

sed -i 's/--/–/g' *.txt ought to work. The -i flag to sed makes it act on the files in-place, the s stands for substitute, and the g makes it replace multiple occurrences of the pattern on the same line. Look up sed's documentation for more information.

EDIT: This works on GNU/Linux; it turns out that the syntax is slightly different on OSX (see comments and accepted answer).

ajd
  • 942
  • 1
  • 6
  • 20
  • So I'm pretty new to the command line...I just put your command into the Terminal in the directory with the files and got this: sed: 1: "base1.txt": undefined label 'ase1.txt'. Any thoughts? Thanks! – Lizza Aug 10 '12 at 03:52
  • I just realized that the files aren't actually named the way I described in the original question. They are named base1.txt, base2.txt, etc. How does that change things? – Lizza Aug 10 '12 at 03:54
  • @Casper: it doesn't. `*.txt` reads every `.txt` file in the present working directory – Steve Aug 10 '12 at 03:58
  • Hmm, still not working: sed: 1: "base1.txt": undefined label 'ase1.txt – Lizza Aug 10 '12 at 04:01
  • @Casper: `sed` on osx works differently to `GNU sed`. You will need to specify an extension for the backup file when using the `-i` flag. – Steve Aug 10 '12 at 04:07