Replace blanks with | (pipe)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace blanks with | (pipe)
# 8  
Old 10-05-2010
It works exactly as I need. Thanks!
# 9  
Old 10-05-2010
Yes, 11 is a big number for sed!

You might have done this:

Code:
sed '
  s/ /|/
  s/ /|/
  s/ /|/
  s/ /|/
  s/ /|/
  s/ /|/
  s/ /|/
  s/ /|/
  s/ /|/
  s/ /|/
  s/ /|/
 '

and there is this, but it falls short at 9 (Is there a newer sed with more \(\) tags? They changed regex, why not this and the silly 2 digit \{\} limit?):

Code:
sed '
  s/^\([^ ]*\) \([^ ]*\) \([^ ]*\) \([^ ]*\) \([^ ]*\) \([^ ]*\) \([^ ]*\) \([^ ]*\) \([^ ]*\) /\1|\2|\3|\4|\5|\6|\7|\8|\9|/
  s/^\([^ ]*\) \([^ ]*\) /\1|\2|/
 '

Sadly, even ksh seems less limited:

Code:
#!/usr/bin/ksh

fix_line(){

 i = 0

 while (( $(( i  += 1 )) <= 11 ))
 do
   echo "$1|"
   shift
 done

 echo "$*"
}

while read ln
do
 fix_ln $ln
done

# 10  
Old 10-05-2010
GNU sed:
Code:
sed 's/ /|/g;s/|/ /12g' infile

This User Gave Thanks to Scrutinizer For This Post:
# 11  
Old 10-06-2010
Quote:
Originally Posted by Scrutinizer
GNU sed:
Code:
sed 's/ /|/g;s/|/ /12g' infile


If 12g, why not g11 ? Did the GNU miss symmetry? Yes! We should tune it up!

Code:
 
$ echo '1234567890' |local/bin/sed 's/[0-9]/x/3g'
12xxxxxxxx
$ echo '1234567890' |local/bin/sed 's/[0-9]/x/g3'
12xxxxxxxx
$

# 12  
Old 10-06-2010
using Perl:-

Code:
perl -wlane 'print join("|",@F[0..11])," ",join(" ",@F[12..$#F])'  infile.txt

SmilieSmilieSmilie
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