Sponsored Content
Top Forums Shell Programming and Scripting Replace all but skip first instance in a line Post 302532681 by Corona688 on Tuesday 21st of June 2011 06:23:05 PM
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

 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
All times are GMT -4. The time now is 12:22 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy