SED - Unable to replace with <tab>


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED - Unable to replace with <tab>
# 1  
Old 07-18-2012
Data SED - Unable to replace with <tab>

Hello All,

I have this file with the below contents

Code:
 
1|2|3|4|
this|that|which|what|

when I use,
Code:
 sed 's/|/\t/g' infile

I get,
Code:
 
1t2t3t4t
thistthattwhichtwhatt

Why is this?? Smilie Smilie
# 2  
Old 07-18-2012
what is your OS ?

Code:
$ echo "A|B|C" | sed 's/\|/\t/g'

try with " ( double quotes ) instead of single quote
# 3  
Old 07-18-2012
Use the Tab key instead of the escape sequence.

From man page for sed(POSIX):
Code:
[2addr]s/BRE/replacement/flags
                .
                .
                .
       The replacement string shall be	scanned  from  beginning  to  end.  An
       ampersand ( '&' ) appearing in the replacement shall be replaced by the
       string matching the BRE. The special meaning of '&' in this context can
       be  suppressed  by  preceding  it  by a backslash. The characters "\n",
       where n is a digit, shall be replaced by the text matched by the corre-
       sponding  backreference expression. The special meaning of "\n" where n
       is a digit in this context, can be suppressed  by  preceding  it  by  a
       backslash.  For each other backslash ( '\' ) encountered, the following
       character shall lose its special meaning (if any). The meaning of a '\'
       immediately  followed  by any character other than '&' , '\' , a digit,
       or the delimiter character used for this command, is unspecified.

       A line can be split by substituting a <newline> into it.  The  applica-
       tion shall escape the <newline> in the replacement by preceding it by a
       backslash. A substitution shall be considered to  have  been  performed
       even  if  the  replacement  string  is  identical to the string that it
       replaces. Any backslash used to alter the default meaning of  a	subse-
       quent  character  shall	be  discarded  from the BRE or the replacement
       before evaluating the BRE or using the replacement.


Last edited by elixir_sinari; 07-18-2012 at 10:27 AM..
This User Gave Thanks to elixir_sinari For This Post:
# 4  
Old 07-18-2012
Like @elixir_sinari says, you must enter a "real" tab in the editor when typing the code. It works for both search and replace string.
I was struggling with this some time ago, and when I googled I find out that some had problem with entering a tab in the editor. If you have that problem, or like me think that it's kind of difficult to read the code when there is a <tab> in the text (the length in the editor is "randomly" - depending on the position in the text), here is a way of fixing that. Perhaps very ugly, but I prefer that:
Code:
REAL_TAB=$(echo -e "\t")
# then use it in sed
echo "A|B|C" | sed "s/\|/$REAL_TAB/g"
=> "A       B       C"

You must use double quotes to expand REAL_TAB. And "-e"-flag to make echo expanding "\t". I think there can be differences for the -e flag in different environments/shells?
# 5  
Old 07-18-2012
It may also be convenient to use awk for this. It has two special variables, FS(input separator) and OFS(output separator). Set them appropriately and do any modification to the input string( even something pointless like $1=$1), and it converts them all as appropriate.

Code:
awk '{$1=$1} 1' FS="|" OFS="\t" inputfile  > outputfile

You could even use tr to do this.

Code:
tr '|' '\t' < inputfile > outputfile

Both tr and awk should always accept \t. Old crusty versions of sed sometimes don't.
# 6  
Old 07-18-2012
Funny, I was just posting this https://www.unix.com/shell-programmin...#post302673979 about having to use $1=$1, it was when I first was thinking of answer "use awk with FS and OFS with "|" and "\t" in this thread, like your post, but when I first tested it I found out this problem. Smilie

So this is a normal behavior for awk, that you have to touch one field to enable OFS for the whole line?
# 7  
Old 07-20-2012
Yes, very normal. It's not that OFS is "disabled" -- it's that awk's still got the original line in memory, which it will print raw, not having been told to do otherwise. To recalculate the line, you must do so explicitly by doing an operation on one of the tokens.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace space by tAB

My file looks like 3 33 210.01.10.0 2.1 1211 560 26 45 1298 98763451112 15412323499 INPUT OK 3 233 40.01.10.0 2.1 1451 780 54 99 1876 78787878784 15423210199 CANCEL OK Aim is to replace the spaces in each line by tab Used: sed -e 's/ */\t/g' But I get output like this... (3 Replies)
Discussion started by: sa@@
3 Replies

2. Shell Programming and Scripting

How to replace blank tab with zero in a file?

hi, i need to replace a blank tab output in a file to zero. input file: 2015/08/04 00:00:00 171 730579 27088 <blank> 3823 30273 1621778 ... (6 Replies)
Discussion started by: amyt1234
6 Replies

3. UNIX for Dummies Questions & Answers

select and replace only those string which is followed by \tab

Hi I have a number of sequences and a string occurs a number of times in that sequence. How can I select and replace only those strings which are followed by \tab. for eg : my sequence looks like : string0 positive cd parent=string0 id =121 string0 string0 negative ef parent=... (2 Replies)
Discussion started by: sonia102
2 Replies

4. Shell Programming and Scripting

replace row separated by tab

Dear users, I have this problem, this is the example: 123 (tab) A (tab) B (tab) C (tab) 456 where the (tab) is actually the \t delimiter. I need to replace the A B and C for D E and F, this is: 123 (tab) D (tab) E (tab) F (tab) 456 The thing is that my file is quite long and this... (2 Replies)
Discussion started by: Gery
2 Replies

5. UNIX for Dummies Questions & Answers

Replace tab or any spaces with "

my content: samaccountname employeeid useraccountcontrol description i want it to look like this: "samaccountname","employeeid","useraccountcontrol","description" (2 Replies)
Discussion started by: tjmannonline
2 Replies

6. Shell Programming and Scripting

Replace with tab

I have text file, i want to replace all the charecters with tab. suppose: cat abc.txt dfjkdf dfdfd fd fd the output should have 4 lines and each line will have 1 tab. Thanks :confused: (2 Replies)
Discussion started by: javeed7
2 Replies

7. Shell Programming and Scripting

how to replace new line ( \n ) with space or Tab in Sed

Hi, how to replace new line with tab in sed ... i used sed -e 's/\n/\t/g' <filename > but its not working (1 Reply)
Discussion started by: mail2sant
1 Replies

8. Shell Programming and Scripting

replace comma(,) with Tab

hi all, i have a file with commas(,). i want to replace all the commas with tab(\t). Plz help...its urgent... (3 Replies)
Discussion started by: vikas_kesarwani
3 Replies

9. Shell Programming and Scripting

Replace a Pipe with tab

i have a file which contains text as shown below.... aaa|bbb|ccc|ddd| cccc|ddddd|eeeee|ffffff want to convert pipe symbol to tab like aaaa bbbb cccc ddddd ccccc ddddd eeeee ffffffffff i tried with sed sed 's/|/\t/g' file_name ...but i could not... (1 Reply)
Discussion started by: srikanthus2002
1 Replies

10. UNIX Desktop Questions & Answers

replace tab with space

How do I replace a tab with a space in scripts using sed/awk ? (1 Reply)
Discussion started by: avnerht
1 Replies
Login or Register to Ask a Question