Sponsored Content
Top Forums Shell Programming and Scripting Replace every second instance of delimeter Post 302967882 by Don Cragun on Monday 29th of February 2016 05:23:13 PM
Old 02-29-2016
Here are two other ways to do what you seem to want; one with sed and one just using shell built-ins. (Note that in this suggested code the string assigned to var starts with a <tab> followed by a <space> and ends with a <space> followed by a <tab> and all of the spaces and tabs are preserved in the output):
Code:
#!/bin/ksh
var='	 Name 1,Value 1,"Name 2",Value 2,Name 3 , Value 3 	'
printf '%s\n' "$var"|sed 's/\([^,]*,[^,]*\),/\1|/g'

(	IFS=","
	set -- $var
	while [ $# -gt 2 ]
	do	printf '%s,%s|' "$1" "$2"
		shift 2
	done
	[ $# -eq 0 ] && echo ''
	[ $# -eq 1 ] && printf '%s\n' "$1"
	[ $# -eq 2 ] && printf '%s,%s\n' "$1" "$2"
)

The above script produces the output:
Code:
	 Name 1,Value 1|"Name 2",Value 2|Name 3 , Value 3 	
	 Name 1,Value 1|"Name 2",Value 2|Name 3 , Value 3

(unfortunately, the CODE tags seem to be stripping off the trailing <space> and <tab> characters on the last line). So too verify that the spaces and tabs were preserved, when that script's output is piped through od -bc, the output is:
Code:
0000000   011 040 116 141 155 145 040 061 054 126 141 154 165 145 040 061
          \t       N   a   m   e       1   ,   V   a   l   u   e       1
0000020   174 042 116 141 155 145 040 062 042 054 126 141 154 165 145 040
           |   "   N   a   m   e       2   "   ,   V   a   l   u   e    
0000040   062 174 116 141 155 145 040 063 040 054 040 126 141 154 165 145
           2   |   N   a   m   e       3       ,       V   a   l   u   e
0000060   040 063 040 011 012 011 040 116 141 155 145 040 061 054 126 141
               3      \t  \n  \t       N   a   m   e       1   ,   V   a
0000100   154 165 145 040 061 174 042 116 141 155 145 040 062 042 054 126
           l   u   e       1   |   "   N   a   m   e       2   "   ,   V
0000120   141 154 165 145 040 062 174 116 141 155 145 040 063 040 054 040
           a   l   u   e       2   |   N   a   m   e       3       ,    
0000140   126 141 154 165 145 040 063 040 011 012                        
           V   a   l   u   e       3      \t  \n                        
0000152

This was tested using a Korn shell, but should work with any POSIX-conforming shell.

Note that using printf '%s\n' "$var" instead of echo $var protects you in cases where the expansion of $var contains any <tab> characters; adjacent <space> characters; and depending on what operating system and shell you're using, any backslash characters (\) that could yield unexpected transformations.
This User Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

replace first instance(not first instance in line)

Alright, I think I know what I am doing with sed(which probably means I don't). But I cant figure out how to replace just the first occurance of a string. I have tried sed, ed, and grep but can't seem to figure it out. If you have any suggestions I am open to anything! (3 Replies)
Discussion started by: IronHorse7
3 Replies

2. Shell Programming and Scripting

sed replace 2nd instance

Hello, I want to replace 2nd instance of "foo" in a file use sed. Any suggestions? (2 Replies)
Discussion started by: katrvu
2 Replies

3. Shell Programming and Scripting

replace nth instance of string

Hi all, I have file with following content ........................... ..........TEST.......... ..........TEST.......... ..................... .....TEST.......... ..................... ..................... .....TEST.......... I want to replace nth "TEST" with "OK" using... (4 Replies)
Discussion started by: uttamhoode
4 Replies

4. Shell Programming and Scripting

Count the delimeter from a file and delete the row if delimeter count doesnt match.

I have a file containing about 5 million rows, in the file there are some records which has extra delimiter at random position. (we dont know the positions), now we have to Count the delimeter from each row and if the count of delimeter is not matching then I want to delete those rows from the... (5 Replies)
Discussion started by: Akumar1
5 Replies

5. Shell Programming and Scripting

Replace all but skip first instance in a line

I have a record like the one given below. 010000306551~IN ~N~ |WINDWARD PK|Alpharetta| If ~ is present more than instance in a line,then I need to delete those instances. Any ideas? I am working in Solaris (7 Replies)
Discussion started by: prasperl
7 Replies

6. Shell Programming and Scripting

Complex find and replace only 1st instance string with dynamic combination

test.txt is the dynamic file but some of combination are fix like below are the lines ;wonder_off = ;wonder_off = disabled wonder_off = wonder_off = disabled the test.txt can content them in any order #cat test.xt ;wonder_off = ;wonder_off = disabled wonder_off = wonder_off =... (5 Replies)
Discussion started by: SilvesterJ
5 Replies

7. Shell Programming and Scripting

Script to replace last instance of . between two consecutive = sign by ,

Suppose you have a line like this: cn=user.blr.ou=blr.india.o=company The line should be converted like this: cn=user.blr,ou=blr.india,o=comapny Was wondering how to do that using shell script. Please use tags where appropriate, thank you (4 Replies)
Discussion started by: saurabhkoar
4 Replies

8. Shell Programming and Scripting

Replacing the delimeter with other delimeter

Hi Friends, I have a file1.txt as below 29123973Ç2012-0529Ç35310124Ç000000000004762Ç00010Ç20Ç390ÇÇÇÇF 29123974Ç20120529Ç35310125Ç0000000000046770Ç00010Ç20Ç390ÇÇÇÇF 29123975Ç20120529Ç35310126Ç0000000000046804Ç00010Ç20Ç390ÇÇÇÇF 29123976Ç20120529Ç35310127Ç0000000000044820Ç00010Ç20Ç390ÇÇÇÇF i have a file2.txt... (4 Replies)
Discussion started by: i150371485
4 Replies

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

10. Shell Programming and Scripting

Replace delimeter between quotes with \| using sed or awk

I've a pipe delimited data in a file of size 3GB. if the text data conatins pipe delimiter that will be enclose with double quotes. I have to replace delimiter which exists between double quotes with #%@#%@#%@ using awk or sed. can some one provide a better and efficient solution to me. The below... (4 Replies)
Discussion started by: BrahmaNaiduA
4 Replies
All times are GMT -4. The time now is 07:23 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy