Replace all but skip first instance in a line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace all but skip first instance in a line
# 1  
Old 06-21-2011
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
# 2  
Old 06-21-2011
It would help if you provided a few sample lines and how they should appear after processing.

Regards,
Alister
# 3  
Old 06-21-2011
The above given text should look like
010000306551~IN N |WINDWARD PK|Alpharetta|
# 4  
Old 06-21-2011
Hmmm.

Splitting the spring apart with IFS="~" would delete all the ~'s and remember where they belonged. IFS="" ; echo "$*" will put the string back together with no ~'s at all. So you save and shift away the first parameter, then mash the rest together with $*, and put them together with ~ in the middle.

Code:
#!/bin/ksh

while read LINE
do
        IFS="~"
        # Break into $1, $2, ... $N based on IFS
        set -- $LINE

        # More than 2 chunks means it found more than one ~
        if [ "$#" -gt 2 ]
        then
                VAR="${1}"
                #toss $1, $2 becomes $1, $3 becomes $2, etc.
                shift
                # IFS affects how $* expands.
                IFS=""
                echo "${VAR}~${*}"
        else
                # Print the unmodified line.  We don't need to delete anything.
                echo "$LINE"
        fi        
done

Code:
$ ./ifsplit.sh <<EOF
010000306551~IN ~N~ |WINDWARD PK|Alpharetta|
a~b~c~d~e
a
b
c~d

EOF
010000306551~IN N |WINDWARD PK|Alpharetta|
a~bcde
a
b
c~d
$

You'd use it like
Code:
./ifsplit.sh < input > temp ; cat temp > input ; rm temp

# 5  
Old 06-21-2011
Thanks Corona688.

This actually breaks the line into multiple chunks.

Ideally I just want to replace >1 occurences of ~
# 6  
Old 06-21-2011
Quote:
Originally Posted by prasperl
Thanks Corona688.

This actually breaks the line into multiple chunks.
It does exactly what you asked for. Smilie It comes in as a single line and leaves as a single line with every ~ beyond the first one removed. The logic's just slightly different than you expected.

String operations like "find the first ~" aren't so easy to do in the shell. In C you could call strchr() but in ksh that could mean 'cut' once for each individual character in a string. So I redefined 'find' a little and let it find the ~'s by splitting, then put the split string back together the way you wanted it.

Last edited by Corona688; 06-21-2011 at 07:39 PM..
# 7  
Old 06-21-2011
Thank you so much.It works.

My bad I was executing in a wring shell where I had already switched the IFS
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Skip to next line if the variable is string

Hi I have the follwoing requirement I have a file as follows: # cat priy yyy.poweroff_cmd = /sbin/poweroff hhh.powersave-nap = 1 When this file is provided as input, I first used "awk" command and saved variables present after "=" replace=$line replace1=`echo $line | awk -F "="... (3 Replies)
Discussion started by: Priya Amaresh
3 Replies

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

3. Shell Programming and Scripting

Skip first and last line

Hi All I have a sample file like below: 012312112 1372422843 1236712 1372422843 1275127 3109301010 from which I wan't to: 1.)delete... (10 Replies)
Discussion started by: swasid
10 Replies

4. Shell Programming and Scripting

sed command to skip the first line during find and replace operation

Hi Gurus, I did an exhaustive search for finding the script using "sed" to exclude the first line of file during find and replace. The first line in my file is the header names. Thanks for your help.. (4 Replies)
Discussion started by: ks_reddy
4 Replies

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

6. Shell Programming and Scripting

replace but skip data between certain commas

OK, I am one needy dude. However, how can I make the program NOT change any of the values BETWEEN the first and second "," ? I dont want any of the numbers changed that are preceded by "AT". I want ALL other numeric values > 300 changed to 300. cat qin.csv |head... (4 Replies)
Discussion started by: herot
4 Replies

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

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

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

10. Shell Programming and Scripting

Skip new line

Hi, how can I skip the new line of echo? In SH!!!! echo "the date is :" date and result I want is the date is : Tue Oct 11 22:24:37 WEST 2005 I've already tried including the \c inside the echo, but it didn't work. Thanks! (2 Replies)
Discussion started by: pmpx
2 Replies
Login or Register to Ask a Question