Replace space and tab to pipe delimeter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace space and tab to pipe delimeter
# 1  
Old 04-25-2013
Replace space and tab to pipe delimeter

I have file like below

Code:
abc       12     34  45  code
abcdef   451   1    4    code
ghtyggg 4       56  3    code

I need to change this to
Code:
abc|12|34|45|code|
abcdef|451|1|4|code|
ghtyggg|4|56|3|code|

I tried replace space with | in sed ... but in the middle some row has tab... and it is replace all space with pipe meaning
Code:
abcdef |||451||1|4||code

# 2  
Old 04-25-2013
Here's the correct way:
Code:
$ sed "s/ *$//; s/[[:space:]]\+/|/g; s/$/|/" file
abc|12|34|45|code|
abcdef|451|1|4|code|
ghtyggg|4|56|3|code|

# 3  
Old 04-25-2013
it is not working.. it is just appening | in the end

Code:
aaaaaaaaaaaaaaaaaaaaaar                           1     0       4       alpha
bbbbbbbbbbbbbbbbbbbbbbbbbbbbh                 2     4       2       number
cccccccccccccccccccccr                             3     6       19     alpha
ddddddddddddddde                                      4     27      2       number


after running
Code:
aaaaaaaaaaaaaaaaaaaaaar                           1     0       4       alpha|
bbbbbbbbbbbbbbbbbbbbbbbbbbbbh                 2     4       2       number|
cccccccccccccccccccccr                             3     6       19      alpha|
ddddddddddddddde                                      4     27      2       number|

I think the space r tab is changing while i am pasting here...
# 4  
Old 04-25-2013
Works fine here.
Code:
$ uname 
Linux

Maybe your sed does not understand [[:space:]] syntax. Try this:
Code:
$ tr "\t" " " < file | sed "s/ *$//; s/ \+/|/g; s/$/|/"
aaaaaaaaaaaaaaaaaaaaaar|1|0|4|alpha|
bbbbbbbbbbbbbbbbbbbbbbbbbbbbh|2|4|2|number|
cccccccccccccccccccccr|3|6|19|alpha|
ddddddddddddddde|4|27|2|number|

# 5  
Old 04-25-2013
With awk:
Code:
awk '$1=$1' OFS="|" file

This User Gave Thanks to Franklin52 For This Post:
# 6  
Old 04-25-2013
Code:
$ awk '$1=$1' OFS="|" file | sed 's/$/|/'
aaaaaaaaaaaaaaaaaaaaaar|1|0|4|alpha|
bbbbbbbbbbbbbbbbbbbbbbbbbbbbh|2|4|2|number|
cccccccccccccccccccccr|3|6|19|alpha|
ddddddddddddddde|4|27|2|number|

# 7  
Old 04-25-2013
Quote:
Originally Posted by hanson44
Code:
$ awk '$1=$1' OFS="|" file | sed 's/$/|/'
aaaaaaaaaaaaaaaaaaaaaar|1|0|4|alpha|
bbbbbbbbbbbbbbbbbbbbbbbbbbbbh|2|4|2|number|
cccccccccccccccccccccr|3|6|19|alpha|
ddddddddddddddde|4|27|2|number|

Yes, misread something... in that case:
Code:
awk 'NF=NF+1' OFS="|" file

This User Gave Thanks to Franklin52 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk with tab delimeter

Hi Team below test file contains tab delimeter file and i am excepting the number of files 3. File : test.txt a b c awk -vFPAT='\t' -vOFS="\t" -v a="0" -v b="10" ' NR>a {if (NF != b ) print NR"@"NF }' test.txt current output is 1@2 required output is 1@3 Cloud you please help... (7 Replies)
Discussion started by: bmk123
7 Replies

2. 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

3. Shell Programming and Scripting

Replace every second instance of delimeter

Hi, Need help on replacing every second instance of delimeter. Scenario: var="Name1,Value1,Name2,Value2,Name3,Value3,Name4,Value" I want every second "," to replace with "|" I tried like below echo $var| sed 's/,/|/2' But, it's not working. Expected output: ... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

4. UNIX for Dummies Questions & Answers

[Solved] Replace first two set of space(s) with tab

My data is jumbled up in the following way. I want to replace only the first two occurrences of space sets (may be a single space also) in every line with a single tab, please help. I want to be able to import the tab delimited columns into excel. Input a b c d e f g h i line1 j k ... (1 Reply)
Discussion started by: gina.lizar
1 Replies

5. UNIX for Dummies Questions & Answers

Changing only the first space to a tab in a space delimited text file

Hi, I have a space delimited text file but I only want to change the first space to a tab and keep the rest of the spaces intact. How do I go about doing that? Thanks! (3 Replies)
Discussion started by: evelibertine
3 Replies

6. Shell Programming and Scripting

how to display column value in a row with space as delimeter

Hi I need to write a small script to kill the process id of particular job in one shot , Example > ps PID TTY TIME COMMAND 16280 pts/70 0:00 sh 16278 pts/70 0:00 rlogind 16197 pts/70 0:00 ps 1234 pts/70 0:00 runflow 2341 pts/70 0:00 runflow 12673 pts/70 ... (6 Replies)
Discussion started by: mani_isha
6 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 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

9. UNIX for Dummies Questions & Answers

How do you represent a field delimeter that is a space???

you know like if you want to work on a specified field in the password file. you would specify the field your interested in my telling the script that the fields are separated by a colon. now, my problem is that I want to specify a field that is not separated by a colon but by a space or tab... (1 Reply)
Discussion started by: TRUEST
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