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
# 15  
Old 12-31-2013
This seems to do what you want for any number of files you want to process:
Code:
#!/bin/ksh
IAm=${0##*/}
if [ $# -lt 3 ] || [ $(($# % 3)) -ne 0 ]
then    printf "Usage: %s file1 IP1 Port1 [filen IPn Portn]...\n" "$IAm" >&2
        exit 1
fi
while [ $# -gt 1 ]
do      printf "Processing file: %s, New IP: %s, New Port: %s\n" "$1" "$2" "$3"
        ed -s "$1" <<-EOF
                /^leprechaun[.]host[.]uri[.]list=raft:\/\//s,//[[:alnum:].:/]*,//$2:$3,
                w
                q
EOF
        shift 3
done

It will work whether or not you have carriage returns in your file (if they are in your input file, they will not be removed; if they are not in your input file, they will not be added).

The earlier scripts weren't working for several reasons, possibly including, but not necessarily limited to:
  1. Your sample file had two lines that match your pattern; your real file only has one.
  2. The second field in the lines after the 1st matched line had numeric strings between the 1st and 2nd periods on the line; your real file does not.
  3. Your sample input and output showed that you wanted us to add lines in increasing numeric order if numbers were missing in the sequence between the 1st and 2nd matches in your input file; your real input file has neither numeric strings nor two matching lines.
# 16  
Old 01-01-2014
Hi Don,

Thanks your valauable code.

In my Sample text file i have same searching string but the full line is not same as the one which i am going to change.So if i use sed it will also change the below line:

Code:
#example: leprechaun.host.uri.list=raft://10.1.120.154:20127,raft://10.1.122.2:21327^M


Can you let us know how this code will work for my sample file.I am passing my inputs as shown in the below code and the output i am receving:

Code:
#!/bin/ksh
file1=HostSocket.properties
IP1=rt900test.info53.com
Port1=23054
IAm=${0##*/}
if [ $# -lt 3 ] || [ $(($# % 3)) -ne 0 ]
then    printf "Usage: %s $file1 $IP1 $Port1 [filen IPn Portn]...\n" "$IAm" >&2
        exit 1
fi
while [ $# -gt 1 ]
do      printf "Processing file1: %s, New IP1: %s, New Port1: %s\n" "$1" "$2" "$3"
        ed -s "$1" <<-EOF
                /^leprechaun[.]host[.]uri[.]list=raft:\/\//s,//[[:alnum:].:/]*,//$2:$3,
                w
                q
EOF
        shift 3
done

output:

Code:
Usage: test.sh HostSocket.properties rt900test.info53.com 23054 [filen IPn Portn]...

# 17  
Old 01-02-2014
Quote:
Originally Posted by bhas85
Hi Don,

Thanks your valauable code.

In my Sample text file i have same searching string but the full line is not same as the one which i am going to change.So if i use sed it will also change the below line:

Code:
#example: leprechaun.host.uri.list=raft://10.1.120.154:20127,raft://10.1.122.2:21327^M


Can you let us know how this code will work for my sample file.I am passing my inputs as shown in the below code and the output i am receving:

Code:
#!/bin/ksh
file1=HostSocket.properties
IP1=rt900test.info53.com
Port1=23054
IAm=${0##*/}
if [ $# -lt 3 ] || [ $(($# % 3)) -ne 0 ]
then    printf "Usage: %s $file1 $IP1 $Port1 [filen IPn Portn]...\n" "$IAm" >&2
        exit 1
fi
while [ $# -gt 1 ]
do      printf "Processing file1: %s, New IP1: %s, New Port1: %s\n" "$1" "$2" "$3"
        ed -s "$1" <<-EOF
                /^leprechaun[.]host[.]uri[.]list=raft:\/\//s,//[[:alnum:].:/]*,//$2:$3,
                w
                q
EOF
        shift 3
done

output:

Code:
Usage: test.sh HostSocket.properties rt900test.info53.com 23054 [filen IPn Portn]...

Maybe I got this thread confused with another thread. I thought you had lots of files that needed to be modified in similar ways but with different IP and Port values for different files. The idea of editing your script to make it work for each different file defeats the purpose of writing a script in the first place. Adding three variables to the start of my script and changing the usage message won't make it work. Try taking the script I gave you in message #15 in this thread and invoke it like this:
Code:
test.sh HostSocket.properties rt900test.info53.com 23054

and if you have two files to be changed, you could invoke it like this:
Code:
test.sh HostSocket.properties rt900test.info53.com 23054 2ndHostSocket.properties rt900test.info53.number2.com 22222

The script is set up to process sets of the arguments where the 1st argument in each set is the name of the file to be updated, the 2nd argument in each set is the new IP to be used for that file, and the 3rd argument in each set is the Port to be used for that file.

In an earlier post I asked:
Quote:
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:
and you replied:
Code:
leprechaun.host.uri.list=raft://

The script I gave you will not change the line:
Code:
#example: leprechaun.host.uri.list=raft://10.1.120.154:20127,raft://10.1.122.2:21327^M

because #example is at the start of the line; not leprechaun.... There is only one line in any of your sample input files where
Code:
leprechaun.host.uri.list=raft://

appears at the start of a line.

And, I repeat. If you have more than one line that matches a pattern, you want to change the 1st matching line, and keep all of the other lines unchanged; sed is not the tool you want to use for this project.
# 18  
Old 01-31-2014
Tha

I am very sorry for my late response.Thanks Don for your support.It works fine now.
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