Replace blanks with | (pipe)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace blanks with | (pipe)
# 1  
Old 10-05-2010
Replace blanks with | (pipe)

Hello,

I'm trying to replace the first x number of spaces in a line with a |. Right now I'm using a very inefficient syntax to accomplish the task and was looking to simplify it.

I have several cases were the pipes need to replace just the first space on the line, which I did a simple replace. I am doing this while reading each line of a file that I'm cat'ing.

Code:
v_sec=`echo $line | sed 's/ /|/'`

The ugly code comes in when I need to replace the first 11 spaces with pipes and ended up doing this:

Code:
		v_bar1=`echo $line | sed 's/ /|/'`
			v_bar2=`echo $v_bar1 | sed 's/ /|/'`
			v_bar3=`echo $v_bar2 | sed 's/ /|/'`
			v_bar4=`echo $v_bar3 | sed 's/ /|/'`
			v_bar5=`echo $v_bar4 | sed 's/ /|/'`
			v_bar6=`echo $v_bar5 | sed 's/ /|/'`
			v_bar7=`echo $v_bar6 | sed 's/ /|/'`
			v_bar8=`echo $v_bar7 | sed 's/ /|/'`
			v_bar9=`echo $v_bar8 | sed 's/ /|/'`
			v_bar10=`echo $v_bar9 | sed 's/ /|/'`
			v_bar_final=`echo $v_bar10 | sed 's/ /|/'`

Any suggestions to simplify would be appreciated. Smilie
# 2  
Old 10-05-2010
hi,
Do you mean 11 spaces in the beginning?

Code:
sed -s 's/^    */|/' input

Here im finding for 4 or more spaces to substitute.you can increase it if your file has 4 spaces or more use {n} option.

Code:
sed -s 's/^ \{11\}/|/' input

Input:
Code:
           
test           make
           check           this

output:
Code:
|
test           make
|check           this

# 3  
Old 10-05-2010
Try:
Code:
sed ':a;s/^\(|*\) /\1|/;ta'

or
Code:
sed -e :a -e 's/^\(|*\) /\1|/;ta'

Code:
$ echo "           xxx xx xxxxx  yyyy" | sed ':a;s/^\(|*\) /\1|/;ta'
|||||||||||xxx xx xxxxx  yyyy


Last edited by Scrutinizer; 10-05-2010 at 11:23 AM..
# 4  
Old 10-05-2010
My apologizes, I should have been more specific. The data looks like this:

Dataset1 Dataset2 Dataset3 etc etc etc A B C D E F G blah blah blah


Required output would then be:

Dataset1|Dataset2|Dataset3|etc|etc|etc|A|B|C|D|E|F G blah blah blah
# 5  
Old 10-05-2010
Code:
$ ruby -ane 'print "#{$F[0..11].join("|")} #{$F[12..-1].join(" ")}\n"' file
Dataset1|Dataset2|Dataset3|etc|etc|etc|A|B|C|D|E|F G blah blah blah

# 6  
Old 10-05-2010
I'm not comfortable using ruby, as this is the first time I have heard of it.
# 7  
Old 10-05-2010
Code:
awk '{for(i=1;i<=NF;i++){s=(i<12)?"|":FS;if(i==NF)s=RS;printf $i s}}' file


Last edited by Scrutinizer; 10-05-2010 at 03:53 PM..
This User Gave Thanks to Scrutinizer 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

How to replace the first and last character which is pipe symbol in all files within a folder?

with in my files i have the data like this, starting with a pipe and ending the line with a pipe. all i want is to replace the first and last pipe , remove those trying to use following sed command, but it is only showing on the screen the entire data of the file as if it removed, but when i... (4 Replies)
Discussion started by: cplusplus1
4 Replies

2. Shell Programming and Scripting

Replace CRLF between pipe (|) delimiter with LF

Hi Folks! Need a solution for the following :- Source data ------------- 123|123|<CRLF><CRLF><CRLF>|321<CRLF> Required output ------------------ 123|123|<LF><LF><LF>|321<CRLF> <CRLF> represents carriage return <LF> represents line feed Being hunting high and low for a... (10 Replies)
Discussion started by: hishamzz
10 Replies

3. How to Post in the The UNIX and Linux Forums

How to replace value of password tag in xml with blanks when special characters are there?

Hi All, I am trying to replace the values inside <password> tag in an xml file but it doesn't replace certain passwords: For eg: Server/home/sperinc>cat TextXML.txt <appIds> <entry name="AccountXref"> <type id="ldap"> <realm>nam</realm> ... (7 Replies)
Discussion started by: saroopkris85
7 Replies

4. Shell Programming and Scripting

Replace space and tab to pipe delimeter

I have file like below abc 12 34 45 code abcdef 451 1 4 code ghtyggg 4 56 3 code I need to change this to 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... (7 Replies)
Discussion started by: greenworld123
7 Replies

5. Programming

Psql replace blanks with character

Well as the title describes, its a pretty straight forward problem. I have a series of psql tables where there are lots of blanks. However there is at least one column, called name, that will never be blank. I want to write a select statement to get all of the contents of the table and then turn... (3 Replies)
Discussion started by: wxornot
3 Replies

6. Shell Programming and Scripting

Replace trailing whitespaces with pipe symbol using perl

I want to replace the whitespace with Pipe symbol to do a multiple pattern matching for the whole text "mysqld failed start" and same as for other text messages Below are the messages stored in a file seperate by whitespace mysqld failed start nfsd mount failed rpcbind failed to start for... (6 Replies)
Discussion started by: kar_333
6 Replies

7. Shell Programming and Scripting

Replace pipe <|> with comma <,> in a column

Hi All Gurus, I need to replace a pipe <|> with a comma <,> in a few columns with pipe delimited file. The column name are fixed for the replacement of comma <,>. For below example, Col3, Col6 and Col8 are columns need to replace with comma <,> if any pipe encountered. example:... (14 Replies)
Discussion started by: agathaeleanor
14 Replies

8. Shell Programming and Scripting

Replace pipe with Broken Pipe

Hi All , Is there any way to replace the pipe ( | ) with the broken pipe (0xA6) in unix (1 Reply)
Discussion started by: saj
1 Replies

9. Shell Programming and Scripting

Replace multiple blanks within double quotes

I have various column names within double quotes, separated by commas. Example: "column one", "column number two", "this is column number three", anothercolumn, yetanothercolumn I need to eliminate the double quotes and replace the blanks within the double quotes by underscores, giving: ... (5 Replies)
Discussion started by: jgrogan
5 Replies

10. 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
Login or Register to Ask a Question