Change only first line of the match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change only first line of the match
# 8  
Old 12-30-2013
Hi All,

I am very sorry for my late reply.Still the it is changing the second line withe below code.

Code:
sed s!${LINE}!leprechaun.host.uri.list=raft://${RAFTIP}:${RAFTPORT}! HostSocket.txt > tmp.properties

When i use AWK command.I am getting the below error.

Code:
$ ./code.sh
awk: 0602-521 There is a regular expression error.
        [] imbalance
 The source line number is 9.
 The error context is
                         >>>  gsub(/[:// <<< ].*:/,"://"RAFTIP,$0)

The code should search for leprechaun.host.uri.list=raft:// and it should only replace this line of first occurance and not touching the second occurance in the file.And i don't know the search is in which line.

Please let me know if i am clear or not.

Last edited by Don Cragun; 12-30-2013 at 12:27 AM.. Reason: Add CODE and ICODE tags.
# 9  
Old 12-30-2013
You are not at all clear!
In your original message in this thread you said you had a file named hostsocket.txt that contains:
Code:
leprechaun.2.uri.list=leprechaun2urilist
leprechaun.3.uri.list=leprechaun3urilist
leprechaun.4.uri.list=leprechaun4urilist
leprechaun.5.uri.list=leprechaun5urilist^
leprechaun.host.uri.list=raft://rt900test.info53.com:20025
leprechaun.1.uri.list=leprechaun1urilist
leprechaun.2.uri.list=leprechaun2urilist
leprechaun.8.uri.list=leprechaun8urilist
leprechaun.9.uri.list=leprechaun9urilist
leprechaun.host.uri.list=raft://11.1.120.154:20527,raft://10.1.182.2:21527
leprechaun.4.uri.list=leprechaun4urilist
leprechaun.5.uri.list=leprechaun5urilist
leprechaun.6.uri.list=leprechaun6urilist

and that after changing one line shown in blue above and below, it should look like:
Code:
leprechaun.2.uri.list=leprechaun2urilist
leprechaun.3.uri.list=leprechaun3urilist
leprechaun.4.uri.list=leprechaun4urilist
leprechaun.5.uri.list=leprechaun5urilist
leprechaun.host.uri.list=raft://rt950test.info53.com:21250
leprechaun.1.uri.list=leprechaun1urilist
leprechaun.2.uri.list=leprechaun2urilist
leprechaun.3.uri.list=leprechaun3urilist
leprechaun.4.uri.list=leprechaun4urilist
leprechaun.5.uri.list=leprechaun5urilist
leprechaun.6.uri.list=leprechaun6urilist
leprechaun.7.uri.list=leprechaun7urilist
leprechaun.8.uri.list=leprechaun8urilist
leprechaun.9.uri.list=leprechaun9urilist
leprechaun.host.uri.list=raft://11.1.120.154:20527,raft://10.1.182.2:21527
leprechaun.4.uri.list=leprechaun4urilist
leprechaun.5.uri.list=leprechaun5urilist
leprechaun.6.uri.list=leprechaun6urilist

But, in addition to the changes marked in blue, the stuff marked in red in the original file was removed and the stuff in red in the updated file was added.

There are lots of fairly simple ways to do what your English description says you want to do. Akshay provided an awk script that comes close to producing the output you showed in your sample from your sample input, if you change:
Code:
                             gsub(/[://].*:/,"://"RAFTIP,$0)
                             gsub(/[:0-9]*$/,":"RAFTPORT,$0)

in his script to:
Code:
                             sub("//.*", "//"RAFTIP":"RAFTPORT

except that it doesn't remove the trailing circumflex from the 4th line of the input file.

The following alternative awk script seems to transform your sample input into what you said your output should be:
Code:
awk -F'[.]' '
/[.^.]$/ {      # Get rid of trailing circumflex...
        $0=substr($0,1,length($0)-1)
}
/^leprechaun[.]host[.]uri[.]list=/ {
        if(found++ == 0) {
                # Change IP address and Port on 1st matching line...
                sub("//.*", "//" RAFTIP ":" RAFTPORT)
                # Set starting sequence number for following lines.
                seq = 1
        }
        print
        next
}
found == 1 && seq <= $2 {
        end = $2
        # Insert missing lines between values between 1st and 2nd matched lines.
        while(seq <= end) {
                gsub(/[0-9]+/, seq++)
                print
        }
        next
}
{       print
}' OFS=. RAFTIP="rt950test.info53.com" RAFTPORT="21250" hostsocket.txt

Due to the considerable discrepancies between your description of what this script should do and the sample input and output provided, Akshay's script and this script make LOTS of assumptions (although his script and my script make different assumptions) that might or might not be what you want.

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.

If you give us a clear description of what you're trying to do and sample input and output that match that description, you stand a much better chance of getting a solution to your problem that doesn't waste out time. (And please use CODE tags.)
# 10  
Old 12-30-2013
Hi Don,

Thanks a lot for your quick reply.And also i know your precious time should not be wasted.This code is working only for the sample input which i given.But i have lot many files like this.So i am passing parameters to change the file.I am not good in awk.So usually use sed to pass the parameters of RAFTIP and RAFTPORT to change hostsocket.txt.If i use awk script for my orginal file.It's going to infinite loop.Please help me with the script with what ever the file it may be.

Code:
sed s!${LINE}!leprechaun.host.uri.list=raft://${RAFTIP}:${RAFTPORT}! HostSocket.txt > tmp.properties

The above code is working fine.But it is replacing in all the matches.
# 11  
Old 12-30-2013
You have already found that sed is not well suited to replacing only one occurrence of a string in a file.

We have shown you how to use awk to make the 3 different sets of changes you showed between your sample input and your sample output. You still have not given us an English specification of all of the changes that should be made to the input to produce the output. So, we have to assume that your real input does not match the assumptions we have made about your actual input based on the sample input you showed us.

Trying to guess at an input file specification from a short sample always leaves us to make assumptions. If our awk script are going into infinite loops, your real input does not match the assumptions we had to make from your sample input.

If you give us:
  1. the OS and release of that OS that you're using,
  2. the shell you're using,
  3. a clear specification of your input file format,
  4. a clear specification of all of the transformations to be made to turn your input into your desired output, and
  5. sample input and output files that match the above specifications
we may be able to help you come up with a script to do it. If you insist on using sed (with or without grep) to make the types of transformations you've shown us, you're out of luck.
# 12  
Old 12-30-2013
Thanks for your reply.I am felxible to anything if it is AWK or sed.

Please find the below details about the environment which i am using.
Os AIX and /usr/bin/ksh

If you need full log it's too big.If you need it i can provide you.The fomat which i sent it right format.No any other changes in the format.

search string:
Code:
leprechaun.host.uri.list=raft://rt920test.info53.com:22025^M

rt920test.info53.com and 22025 are variables which i am passing through command promt.so these values are not fixed and the log file is also not fixed.

Replace string:
Code:
leprechaun.host.uri.list=raft://rt900test.info53.com:21025^M

But the code which was provided is working for the sample input which i given.

Please let me know if you need any more details.
# 13  
Old 12-30-2013
Quote:
Originally Posted by bhas85
Thanks for your reply.I am felxible to anything if it is AWK or sed.

Please find the below details about the environment which i am using.
Os AIX and /usr/bin/ksh

If you need full log it's too big.If you need it i can provide you.The fomat which i sent it right format.No any other changes in the format.

search string:
Code:
leprechaun.host.uri.list=raft://rt920test.info53.com:22025^M

rt920test.info53.com and 22025 are variables which i am passing through command promt.so these values are not fixed and the log file is also not fixed.

Replace string:
Code:
leprechaun.host.uri.list=raft://rt900test.info53.com:21025^M

But the code which was provided is working for the sample input which i given.

Please let me know if you need any more details.
Is the source that is producing hostsocket.txt a windows system? You never said anything before about having <carriage-return><newline> as your line terminator (rather than the standard UNIX <newline> by itself) before. Your sample file didn't have any <carriage-return> characters in it.

Are you copying our suggested code onto your system using an editor that uses <carriage-return><newline> instead of <newline> as line terminators? If so, it is no wonder that none of our code is working for you on AIX.

Does your input file really contain <carriage-return> characters?
Do you really want <carriage-return> characters in the output?
Do you want the script to remove the trailing circumflex characters as shown in your first example?
Do you want the script to add lines as shown in your first example?
To identify the line to be changed, which of the following do we need to match at the start of a line:
Code:
leprechaun.host.uri.list=raft://rt920test.info53.com:22025<carriage-return>
leprechaun.host.uri.list=raft://rt920test.info53.com:22025
leprechaun.host.uri.list=raft://
leprechaun.host.uri.list=raft:
leprechaun.host.uri.list=raft
leprechaun.host.uri.list=
leprechaun.host.uri.list

(where <carriage-return> is a visual representation of the single-byte carriage return character)? If it is one of the first two above, am I correct in assuming that we should accept the match only if the match is to the entire input line? You seem to be supplying your script with only the replacement text rather than the details of the IP and Port that are being changed, and your grep was looking for the next to the last entry above; but you latest message seems to be saying we should look for one of the first two???
# 14  
Old 12-31-2013
Hi Don,

I tested the code without <carriage-return> for the i/p file.but still it is going to infinite loop as below.


Does your input file really contain <carriage-return> characters?
Yes it contains each and every line of the inputfile.

Do you really want <carriage-return> characters in the output?
It can be removed or if it is there also not a problem

Do you want the script to remove the trailing circumflex characters as shown in your first example?
No need

Do you want the script to add lines as shown in your first example?
To identify the line to be changed, which of the following do we need to match at the start of a line:

Code:
leprechaun.host.uri.list=raft://

My original inputfile looks as below:

Code:
##########################################################^M
# Leprechaun Settings^M
##########################################################^M
^M
leprechaun.enabled=TRUE^M
^M
leprechaun.character.encoding=Cd037^M
leprechaun.reconnect.wait.time=5010^M
leprechaun.max.wait.time=10200^M
leprechaun.keep.alive=TRUE^M
leprechaun.version.number=2.1^M
leprechaun.header.encryption.enabled=false^M
leprechaun.pin.encryption.enabled=true^M
leprechaun.captureStatistics=TRUE^M
leprechaun.extended.debug=@leprechaunextendeddebug@^M
leprechaun.max.request.size=16000^M
^M
#----- (New Way) Leprechaun without MQ ---- START ------^M
#^M
# The Following settings are used when leprechaun.pool.enabled^M
#  is set to true & leprechaun.JMS.enabled is set to false^M
#^M
# Most Environments will want to use this because of it no longer^M
#  has a dependency on MQ Series.^M
#^M
leprechaun.pool.enabled=TRUE^M
#example: leprechaun.pool.enabled=true {leprechaun.JMS.enabled=false when leprechaun.pool.enabled=true}^M
leprechaun.host.uri.list=raft://rt900test.info53.com:20025^M
leprechaun.ol1.uri.list=@leprechaunol1urilist@^M
leprechaun.ol2.uri.list=@leprechaunol2urilist@^M
leprechaun.ol3.uri.list=@leprechaunol3urilist@^M
leprechaun.ol4.uri.list=@leprechaunol4urilist@^M
leprechaun.ol5.uri.list=@leprechaunol5urilist@^M
leprechaun.ol6.uri.list=@leprechaunol6urilist@^M
leprechaun.ol7.uri.list=@leprechaunol7urilist@^M
leprechaun.ol8.uri.list=@leprechaunol8urilist@^M
leprechaun.ol9.uri.list=@leprechaunol9urilist@^M
leprechaun.ol10.uri.list=@leprechaunol10urilist@^M
leprechaun.ol14.uri.list=@leprechaunol14urilist@^M
leprechaun.ol15.uri.list=@leprechaunol15urilist@^M
leprechaun.ol20.uri.list=@leprechaunol20urilist@^M
leprechaun.ol18.uri.list=@leprechaunol18urilist@^M
leprechaun.ol19.uri.list=@leprechaunol19urilist@^M
leprechaun.ol23.uri.list=@leprechaunol23urilist@^M
leprechaun.ol25.uri.list=@leprechaunol25urilist@^M
leprechaun.ol30.uri.list=@leprechaunol30urilist@^M
leprechaun.ol35.uri.list=@leprechaunol35urilist@^M
###############################################^M
# The following onlines are added for user credintials ^M
# and will be used by HI_Direct for different environments ^M
# uol represents onlines where user credentials are required^M
###############################################^M
leprechaun.uol1.uri.list=@leprechaunuol1urilist@^M
leprechaun.uol2.uri.list=@leprechaunuol2urilist@^M
leprechaun.uol3.uri.list=@leprechaunuol3urilist@^M
leprechaun.uol4.uri.list=@leprechaunuol4urilist@^M
leprechaun.uol5.uri.list=@leprechaunuol5urilist@^M
leprechaun.uol6.uri.list=@leprechaunuol6urilist@^M
leprechaun.uol7.uri.list=@leprechaunuol7urilist@^M
leprechaun.uol8.uri.list=@leprechaunuol8urilist@^M
leprechaun.uol9.uri.list=@leprechaunuol9urilist@^M
leprechaun.uol10.uri.list=@leprechaunuol10urilist@^M
leprechaun.uol14.uri.list=@leprechaunuol14urilist@^M
leprechaun.uol15.uri.list=@leprechaunuol15urilist@^M
leprechaun.uol18.uri.list=@leprechaunuol18urilist@^M
leprechaun.uol19.uri.list=@leprechaunuol19urilist@^M
leprechaun.uol20.uri.list=@leprechaunuol20urilist@^M
leprechaun.uol23.uri.list=@leprechaunuol23urilist@^M
leprechaun.uol25.uri.list=@leprechaunuol25urilist@^M
#example: leprechaun.host.uri.list=raft://10.1.120.154:20127,raft://10.1.122.2:21327^M

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print next line beside preceding line on column match

Hi, I have some data like below: John 254 Chris 254 Matt 123 Abe 123 Raj 487 Moh 487 How can i print it using awk to have: 254 John,Chris 123 Matt,Abe 487 Raj,Moh Thanks. (4 Replies)
Discussion started by: james2009
4 Replies

2. Shell Programming and Scripting

awk to combine lines from line with pattern match to a line that ends in a pattern

I am trying to combine lines with these conditions: 1. First line starts with text of "libname VALUE db2 datasrc" where VALUE can be any text. 2. If condition1 is met then continue to combine lines through a line that ends with a semicolon. 3. Ignore case when matching patterns and remove any... (5 Replies)
Discussion started by: Wes Kem
5 Replies

3. Shell Programming and Scripting

Rearrange or replace only the second line after pattern match or pattern match

Im using the command below , but thats not the output that i want. it only prints the odd and even numbers. awk '{if(NR%2){print $0 > "1"}else{print $0 > "2"}}' Im hoping for something like this file1: Text hi this is just a test text1 text2 text3 text4 text5 text6 Text hi... (2 Replies)
Discussion started by: invinzin21
2 Replies

4. Shell Programming and Scripting

Match pattern1 in file, match pattern2, substitute value1 in line

not getting anywhere with this an xml file contains multiple clients set up with same tags, different values. I need to parse the file for client foo, and change the value of tag "64bit" from false to true. cat clients.xml <Client type"FIX"> <ClientName>foo</ClientName>... (3 Replies)
Discussion started by: jack.bauer
3 Replies

5. Shell Programming and Scripting

I need to know how to replace a line after a pattern match with an empty line using SED

Hi How Are you? I am doing fine! I need to go now? I will see you tomorrow! Basically I need to replace the entire line containing "doing" with a blank line: I need to the following output: Hi How Are you? I need to go now? I will see you tomorrow! Thanks in advance.... (1 Reply)
Discussion started by: sags007_99
1 Replies

6. Shell Programming and Scripting

awk or sed? change field conditional on key match

Hi. I'd appreciate if I can get some direction in this issue to get me going. Datafile1: -About 4000 records, I have to update field#4 in selected records based on a match in the key field (Field#1). -Field #1 is the key field (servername) . # of Fields may vary # comment server1 bbb ccc... (2 Replies)
Discussion started by: RascalHoudi
2 Replies

7. Shell Programming and Scripting

Delete line with match and previous line quoting/escaping problem

Hi folks, I've list of LDAP records in this format: cat cmmac.export.tmp2 dn: deviceId=0a92746a54tbmd34b05758900131136a506,ou=devices,ou=customer,ou=nl,o=upc cmmac: 00:13:11:36:a5:06 dn: deviceId=0a92746a62pbms4662299650015961cfa23,ou=devices,ou=customer,ou=nl,o=upc cmmac:... (4 Replies)
Discussion started by: tomas.polak
4 Replies

8. Shell Programming and Scripting

Print Line if next line Match a pattern

Hi All, Does anyone know how to print 1H1A....... in peal script print line ^1H1A....... if next line equal 5R0RECEIPT.... Thank for help:D Cat st.txt 1H1A-IN-11-5410-0009420|1010047766|dsds|1|N|IN|IN|000000|1||N|<<<line match 5R0RECEIPT| 5R0RECEIPT|... (2 Replies)
Discussion started by: kittiwas
2 Replies

9. Shell Programming and Scripting

Script to change file contents line by line

Hi, I'm struggling to write a script to do the following, -will go through each line in the file -in a specific character positions, changes the value to a new value -These character positions are fixed througout the file ----------------------- e.g.: file1.sh will have the following 3... (4 Replies)
Discussion started by: vini99
4 Replies

10. Shell Programming and Scripting

sed - Replace Line which contains the Pattern match with a new line

I need to replace the line containing "STAGE_DB" with the line "STAGE_DB $DB # database that contains the table being loaded ($workingDB)" Here $DB is passed during the runtime. How can I do this? Thanks, Kousikan (2 Replies)
Discussion started by: kousikan
2 Replies
Login or Register to Ask a Question