[Solved] Last occurrence of a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Last occurrence of a string
# 1  
Old 03-07-2014
[Solved] Last occurrence of a string

I apologize if it was asked before but I couldn't find something related.
I want to replace 2 strings in a file

e.g
pwddb=Lar1wod (need to replace string after =)
pwdapp=Wde2xe (need to replace string after =)

AND in same file want to find last occurrence of a string (SR2-134561), add "file was changed by $appid" to newline.

Thanks in advance.
# 2  
Old 03-07-2014
A simple way to do things like this is with ed or ex. Your requirement to
Quote:
add "file was changed by $appid" to newline
could be interpreted several ways. Assuming that what you meant was to add a new line containing that text after the last line in the file containing SR2-13451, the following does that (and also adds a timestamp saying when the file was updated):
Code:
#!/bin/ksh
file=${1:-file}
appid='J_ang_script'
ed -s "$file" <<EOF > /dev/null
        w $file.backup
        /pwddb=/s/\(pwddb=\)[^ ]*/\1PWDDB_Replacement/
        /pwdapp=/s/\(pwdapp=\)[^ ]*/\1PWDAPP_Replacement/
        1
        ?SR2-134561?a
file was change by $appid $(date)
.
        w
        q
EOF

Note that it saves a backup copy of your file before making any changes.

If the file data.txt contains:
Code:
SR2-134561
pwddb=Lar1wod (need to replace string after =)
SR2-134561
pwdapp=Wde2xe 
SR2-134561
SR2-134561

then running the above script with:
Code:
./script_name data.txt

will save the original contents of data.txt in data.txt.backup and change the contents of data.txtto:
Code:
SR2-134561
pwddb=PWDDB_Replacement (need to replace string after =)
SR2-134561
pwdapp=PWDAPP_Replacement
SR2-134561
SR2-134561
file was change by J_ang_script Fri Mar  7 18:06:06 PST 2014

# 3  
Old 03-10-2014
Thank you very much it worked prefectly.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Append a particular string after occurrence of particular string.

Hi Friends, Good morning. Appended a particular string after occurrence of particular string in a file. my file abc.sql as below create or replace function f1(p_cust_no IN VARCHAR) RETURN number IS DECLARE v_country country.customer_tbl%TYPE; begin begin select... (4 Replies)
Discussion started by: ram0106
4 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Awk: count occurrence of each character for every field

Hi, let's say an input looks like: A|C|C|D A|C|I|E A|B|I|C A|T|I|B as the title of the thread explains, I am trying to get something like: 1|A=4 2|C=2|B=1|T=1 3|I=3|C=1 4|D=1|E=1|C=1|B=1 i.e. a count of every character in each field (first column of output) independently, sorted... (4 Replies)
Discussion started by: beca123456
4 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Replace first occurrence after match

hey guys, i have been trying to work this thing out with sed with no luck :confused: i m looking for a way to replace only the first occurrence after a match for example : Cat Realized what you gotta do Dog Realized what you gotta do Sheep Realized what you gotta do Wolf Realized... (6 Replies)
Discussion started by: boaz733
6 Replies

4. Shell Programming and Scripting

Find last occurrence of a character in a string

Hello how to find last occurence of a string for example in the following I want last occurence of '-' i.e. position 12 str="aa-bbb-cccc-ddd-ee" my pupose is to get the string 'ee' Thanks and Regards Chetanz (5 Replies)
Discussion started by: Chetanz
5 Replies

5. UNIX for Dummies Questions & Answers

counting occurrence of characters in a string

Hello, I have a string like this 0:1:2:0:2:2:4:0:0:0:-200:500...... what i want is to break down how many different characters are there and their count. For example for above string it should display 0 - 5 times 1 - 1 times 2 - 3 times 4 - 1 times . . . I am stuck in writing... (8 Replies)
Discussion started by: exit86
8 Replies

6. Shell Programming and Scripting

[Solved] Sed/awk print between patterns the first occurrence

Guys, I am trying the following: i have a log file of a webbap which logs in the following pattern: 2011-08-14 21:10:04,535 blablabla ERROR blablabla bla bla bla bla 2011-08-14 21:10:04,535 blablabla ERROR blablabla bla bla bla ... (6 Replies)
Discussion started by: ppolianidis
6 Replies

7. Shell Programming and Scripting

remove characters from string based on occurrence of a string

Hello Folks.. I need your help .. here the example of my problem..i know its easy..i don't all the commands in unix to do this especiallly sed...here my string.. dwc2_dfg_ajja_dfhhj_vw_dec2_dfgh_dwq desired output is.. dwc2_dfg_ajja_dfhhj it's a simple task with tail... (5 Replies)
Discussion started by: victor369
5 Replies

8. Shell Programming and Scripting

Displaying only First Occurrence of a String

Hi, My requirement is that I should search for a particular string and display the string only once if there is more occurrence of the same string in a file. e.g In the given below log file,the string AMQ5037 comes twice.I want to search for this string and display it only once.grep will... (5 Replies)
Discussion started by: charudpss
5 Replies

9. UNIX for Dummies Questions & Answers

last occurrence of a string across multiple files

Hello: Thank you for your help. (2 Replies)
Discussion started by: porphyrin
2 Replies

10. UNIX for Dummies Questions & Answers

Search and replace to first occurrence of string

Hi all, I have a very large; delimited file. In vi I would like to replace: CSACT_DY;AVG_UEACT1;uesPerActiveLinkSetSize_1;#;A CSACT_DY;AVG_UEACT2;uesPerActiveLinkSetSize_2;#;A CSACT_DY;AVG_UEACT3;uesPerActiveLinkSetSize_3;#;A with: CSACT_DY;AVG_UEACT1;Average... (7 Replies)
Discussion started by: gilmord
7 Replies
Login or Register to Ask a Question