• Post Reply Bookmark Topic Watch Topic
  • New Topic

SED regular expressions for simple replace. ...  RSS feed

 
jay vas
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys :

I want to replace all tabs with new lines in a file ...

I tried

sed s/\t/\n oldTabDelimitedFile.txt > newLineDelimittedFile.txt

and got an error : unterminated substitute in regular expression .

What is this error ? By my estimation, \t and \n are valid regular expressions ?

Any feedback from one of you Unixy types would be appreciated !!!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error means that what you should have tried is something like

sed s/\t/\n/g oldTabDelimitedFile.txt > newLineDelimittedFile.txt

You forgot the last separator character.

Now, that will stop the error message, but it won't work. You can't use \t with sed -- it's not part of regular expressions in general -- and although you can use \n in the pattern part of a sed replace command, you can't use it in the replacement part! Here's what you can do, though:

sed 's/ /\
/g' < oldTabDelimitedFile.txt > newLineDelimittedFile.txt


That is an actual, literal tab in the pattern (you can type this at a UNIX prompt as the two keystrokes Control-V, Control-I) and an actual literal newline in the replacement, preceded by a backslash. Believe it or not, this will work!


 
Tim Holloway
Saloon Keeper
Posts: 18792
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This kind of stuff in sed is a major pain, and all the more so since the command shell escaping and interpreting mechanism further complicates the issue.

In your case, you might find the "tr" utility to be simpler, although I'm not sure if tr will convert to/from newline characters.

The octal code for tab in shell-speak, is \010 (hex 08), incidentally.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

should do it, but

(Gnu-sed 4.2.1) works for me too.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!