Insert a hyphen between two delimiters using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert a hyphen between two delimiters using sed
# 1  
Old 06-20-2016
Insert a hyphen between two delimiters using sed

Hey guys,

I have a file that is delimited by | and I am trying to write a sed command to
convert this:

Code:
abc|def||ghi|jkl||||mnop

into this:

Code:
abc|def|-|ghi|jkl|-|-|-|mnop


The output I am getting out of:

Code:
sed -e "s/[|]+/[|-|]/g" /tmp/opt.del > /tmp/opt2.del

is like:

Code:
abc|def|-|ghi|jkl|-|(ignoring this one)|-|mnop




Moderator's Comments:
Mod Comment Please use code tags as required by forum rules!

Last edited by RudiC; 06-20-2016 at 09:14 AM.. Reason: Added code tags.
# 2  
Old 06-20-2016
That's not what I get from your command; must be special with your sed version. For your problem, try
Code:
sed -e ":L; s/||/|-|/g;tL" file
abc|def|-|ghi|jkl|-|-|-|mnop

# 3  
Old 06-20-2016
I am getting something like this with your command

Code:
"The label :L; s/||/|-|/g;tL is greater than eight characters."

Moderator's Comments:
Mod Comment PLEASE use code tags as required by forum rules!


May be our seds are of different versions.

Last edited by RudiC; 06-20-2016 at 09:58 AM.. Reason: Added code tags (again!).
# 4  
Old 06-20-2016
Please, try:
Code:
perl -pe 's/\|(?=\|)/\|-/g' file

This User Gave Thanks to Aia For This Post:
# 5  
Old 06-21-2016
Thanks @Aia that did exactly what I wanted.

Got to do some studying on perl. Can this be achieved using sed or awk?

@RudiC Will take care next time. Thanks for your inputs too.
# 6  
Old 06-21-2016
Many Unix sed have parser bugs with semicolon.
I recommend multi-line
Code:
sed '
:L
s/||/|-|/g
tL
' file

Or at least multi -e (less readable, but a must with t/csh that is not multi-line capable)
Code:
sed -e ':L' -e 's/||/|-|/g;tL' file

This User Gave Thanks to MadeInGermany For This Post:
# 7  
Old 06-21-2016
This should work:
Code:
awk 'BEGIN{FS=OFS="|"}{for(i=1;i<=NF;i++)if($i=="")$i="-"}1' /tmp/opt.del > /tmp/opt2.del

If you are trying this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
This User Gave Thanks to Don Cragun 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

Issues with using sed with word delimiters \< and \>

sed is not applying /d "delete line" option when I also include match word options \< and \> examples... echo cat | sed '/\<cat\>/d' will return cat for some reason echo cat | sed "/\<cat\>/d" will also still return cat. Of course I can just run this... echo cat | sed '/cat/d' and... (1 Reply)
Discussion started by: escooter87
1 Replies

2. Shell Programming and Scripting

sed - replacing a substring containing a hyphen

I'm attempting to replace a substring that contains a hyphen and not having much success, can anyone point out where i'm going wrong or suggest an alternative. # echo /var/lib/libvirt/images/vm888b-clone.qcow | sed -e 's|vm888-clone|qaz|g' /var/lib/libvirt/images/vm888b-clone.qcow (1 Reply)
Discussion started by: squrcles
1 Replies

3. Shell Programming and Scripting

Insert Columns before the last Column based on the Count of Delimiters

Hi, I have a requirement where in I need to insert delimiters before the last column of the total delimiters is less than a specified number. Say if the delimiters is less than 139, I need to insert 2 columns ( with blanks) before the last field awk -F 'Ç' '{ if (NF-1 < 139)} END { "Insert 2... (5 Replies)
Discussion started by: arunkesi
5 Replies

4. Shell Programming and Scripting

Insert data between comma delimiters-large file

Having a huge file in the following format. 2,3,1,,,4 1,2,3,,,,,5, 8,7,3,4,,,, Output needed is: 2,3,1,0.0,0.0,4 1,2,3,0.0,0.0,0.0,0.0,5, 8,7,3,4,0.0,0.0,0.0, I have tried reading the file each line, using AWK to parse to find out ",," and then insert 0.0 . It works but very slow. Need... (8 Replies)
Discussion started by: wincrazy
8 Replies

5. UNIX for Dummies Questions & Answers

Delete string between delimiters with sed

Hi, I need to delete all text between "|" delimiters. The line in text file typically looks like this: 1014182| 13728 -rw-r--r-- 1 imac1 staff 7026127 2 okt 2010 |/Users/imac1/Music/iTunes/iTunes Media/Music/Various Artists/We Are the World_ U.S.A. for Africa/01 We Are the World.mp3... (2 Replies)
Discussion started by: andrejm
2 Replies

6. Programming

sed no match word end with hyphen

Hi, This is my first post.It has two parts first part: I want to match a line that starts with whitespace tab or similar followed by must start with specific word and not match same word start with hyphen like this: grep height file1: height: 150px; line-height: 1.5em; height:... (4 Replies)
Discussion started by: medium_linux
4 Replies

7. Shell Programming and Scripting

Program to insert Delimiters at fixed locations in a file, Can you please Debug it for me??

Can someone please help?I have a file - fixed.txt----------------------------AABBBBCCCCCCDDDEEFFFFGGGGGGHHHIIJJJJKKKKKKLLL----------------------------To insert delimiters at fixed lengths of 2, 4, 6, 3, I created a file text1.txt as-------------------2463----------------------and trying to execute... (10 Replies)
Discussion started by: jd_mca
10 Replies

8. Shell Programming and Scripting

Insert lines between delimiters

I'm working with a file like: somestuff somemorestuff ... someadditionalstuff STARTTAG ENDTAG someotherstuff somecoolstuff ... somefinalstuffI've got some text (either in a file or piped) to put between STARTTAG and ENDTAG. I was thinking something like grepping for the line number of... (2 Replies)
Discussion started by: BMDan
2 Replies

9. Shell Programming and Scripting

Using sed to delete string between delimiters

Hi There! I have the following string which i need to convert to i.e. between each occurence of the delimiter ('|' in this case), i need to delete all characters from the '|' to the ':' so that |10,9:12/xxx| becomes |12/xxx| How can i do this using sed? Thanks in advance! (13 Replies)
Discussion started by: orno
13 Replies

10. UNIX for Dummies Questions & Answers

How do I insert commas/delimiters in File

Hi, Newbie here. Need to convert a txt file to .csv format. There's no character to replace so not sure if I can use sed :confused: . The comma is to be inserted after every certain number of characters in each line... Help! Thanks. (4 Replies)
Discussion started by: mbelen
4 Replies
Login or Register to Ask a Question