replace line after a regex


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers replace line after a regex
# 1  
Old 04-23-2009
replace line after a regex

Hi,

I am trying to write a script which will modify a given account's settings by searching for a line in a file and then replacing the line after it. Here is a portion of my input file:

[0002]
type=friend
username=0002
secret=password
host=dynamic
dtmfmode=rfc2833
mailbox=0002
context=sip
insecure=very
canreinvite=no
nat=yes
realm=192.168.1.122
callerid="Phone2id" <0002>

[0003]
type=friend
username=0003
secret=password
host=dynamic
dtmfmode=rfc2833
mailbox=0003
context=sip
insecure=very
canreinvite=no
nat=yes
realm=192.168.1.122
callerid="Phone3id" <0003>

I want to replace "password". I want to match the line before "secret=password" to be sure it is changing the correct line as the username will be a unique identifier.

I have been trying to do this with sed but have not been able to make it work. Also, I am not limited to sed.
# 2  
Old 04-23-2009
if you know the username and the file always has each account separated by a blank line and the secret entry always follows the username entry.
Code:
sed '/username=0003/,/^$/s/secret=.*/secret=<newpassword>/' file > output_file

if you want to change a list of username:secret entries from an input file. the -i switch will update the file inplace so make a backup first
Code:
# content of input file
0002:newpwd002
0003:newpwd003

IFS=":"
while read name pwd;do
  sed -i "/username=$name/,/^$/s/secret=.*$/secret=$pwd/" account_file
done < input

# 3  
Old 04-23-2009
thanks a bunch! the sed line was just what I needed!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Regex Expression Replace

I have a XML file where there is a tag with like <wd:address_line_1>1234 Street</wd:address_line_1> I want to replace the values "1234 Street" with "Test Data". Different people have different address lines and i want to replace with a fixed value to mask the file. I was trying to use sed... (7 Replies)
Discussion started by: dr46014
7 Replies

2. Shell Programming and Scripting

Replace HTML tags using sed regex

I need all the end tags of </font> to be replaced with new line yet enclosing tag to be retained </font>. Please help me in this regard. Input: <font>abc</font>def<font>ghi</font> Output: <font>abc</font> def <font>ghi</font> (3 Replies)
Discussion started by: Badhrish
3 Replies

3. Shell Programming and Scripting

Multi line regex for search and replace

I have text file like below: a.txt Server=abc Run=1 Time=120.123 Tables=10 Sessions=16 Time=380.123 Version=1.1 Jobs=5 Server=abc Run=2 Time=160.123 Tables=15 Sessions=16 Time=400.258 Version=2.0 (1 Reply)
Discussion started by: sol_nov
1 Replies

4. Shell Programming and Scripting

Regex - search and replace

I have file which contains data in the following format all in a single line: BDW_PUBLN_ID DECIMAL(18:0) NOT NULL PRIMARY INDEX ARGO_ACCT_DEP_PI ( OFC_ID ,CSHBX_ID ,TRXN_SEQ_NUM ,PROCG_DT ) PARTITION BY RANGE_N(PROCG_DT BETWEEN DATE '2012-03-01' AND DATE '2014-12-31' EACH INTERVAL '1' MONTH );... (4 Replies)
Discussion started by: ysvsr1
4 Replies

5. Shell Programming and Scripting

Using regex's from file1, print line and line after matches in file2

Good day, I have a list of regular expressions in file1. For each match in file2, print the containing line and the line after. file1: file2: Output: I can match a regex and print the line and line after awk '{lines = $0} /Macrosiphum_rosae/ {print lines ; print lines } ' ... (1 Reply)
Discussion started by: pathunkathunk
1 Replies

6. UNIX for Dummies Questions & Answers

read regex from ID file, print regex and line below from source file

I have a file of protein sequences with headers (my source file). Based on a list of IDs (which are included in some of the headers), I'd like to print out only the specified sequences, with only the ID as header. In other words, I'd like to search source.txt for the terms in IDs.txt, and print... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

7. UNIX for Dummies Questions & Answers

How to specify beginning-of-line/end-of-line characters inside a regex range

How can I specify special meaning characters like ^ or $ inside a regex range. e.g Suppose I want to search for a string that either starts with '|' character or begins with start-of-line character. I tried the following but it does not work: sed 's/\(\)/<do something here>/g' file1 ... (3 Replies)
Discussion started by: jawsnnn
3 Replies

8. Emergency UNIX and Linux Support

search replace regex question

Hi, I need to run a search and replace on a large database, what I need to change is all instances of #### (eg. 1764 or 1964) to (####) (eg. (1764) or (1964)) But there might be other numbers in there such as (1764) and I do not need those changed to ((1764)) How can I... (7 Replies)
Discussion started by: lawstudent
7 Replies

9. Shell Programming and Scripting

sed: how to replace regex with a ' (quote) mark

I've got a text file which has " marks where it there should be ' marks. I tried to do it with sed, but it won't allow me to escape the ' mark. Here's what I tried to do: sed 's/"/\\'/g' file.txt How can this be done? Thanks (3 Replies)
Discussion started by: CraigMoore
3 Replies

10. Shell Programming and Scripting

sed: delete regex line and next line if blank

Hi, I want to write a sed script which from batiato: batiato/giubbe: pip_b.2.txt pip_b.3.txt pip_b.3mmm.txt bennato: bennato/peterpan: 123.txt consoli: pip_a.12.txt daniele: (2 Replies)
Discussion started by: one71
2 Replies
Login or Register to Ask a Question