Searching for a particular string and modifying text within block of data


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching for a particular string and modifying text within block of data
# 1  
Old 11-18-2010
Searching for a particular string and modifying text within block of data

Hi Forum.

Is there a quick way to do the following search/replace within a block of data? I tried to google the solution but didn't really know what to look for.

I have the following text file (I want to search for a particular string "s_m_f_acct_txn_daily_a1" and replace the "$$startdate=to_char(sysdate-10,'j')" to "$$startdate=to_char(sysdate-10000,'j')" within that data block only (Appending 3 zeroes at end).

Everything else should remain as is.

Process should ask for a particular string to search for.

Code:
Original Text:
[s_m_f_acct_txn]
$PMSessionLogFile=$PMRootDir/Logs/AUTO.S_M_F_ACCT_TXN.LOG
$DBConnection_EDW=SAS2EDW
$$startdate=to_char(sysdate-10,'j')
$$enddate=to_char(sysdate, 'j')
 
 
[s_m_f_acct_txn_FULL_LOAD]
$PMSessionLogFile=$PMRootDir/Logs/AUTO.S_M_F_ACCT_TXN_FULL_LOA.LOG
$DBConnection_EDW=SAS2EDW
 
[s_m_f_acct_txn_daily_a1]
$PMSessionLogFile=$PMRootDir/Logs/SAS_FACT1/WF_FACT1_F.LOG
$DBConnection_EDW=SAS2EDW
$$startdate=to_char(sysdate-10,'j')
$$enddate=to_char(sysdate, 'j')
 
[s_m_f_asp_setup]
$PMSessionLogFile=$PMRootDir/Logs/SAS_FACT1/WF_FACT1_F.S_M_F_A.LOG
$DBConnection_EDW=SAS2EDW
$$startdate=to_char(sysdate-10,'j')

Modified Text:
[s_m_f_acct_txn]
$PMSessionLogFile=$PMRootDir/Logs/AUTO.S_M_F_ACCT_TXN.LOG
$DBConnection_EDW=SAS2EDW
$$startdate=to_char(sysdate-10,'j')
$$enddate=to_char(sysdate, 'j')
 
 
[s_m_f_acct_txn_FULL_LOAD]
$PMSessionLogFile=$PMRootDir/Logs/AUTO.S_M_F_ACCT_TXN_FULL_LOA.LOG
$DBConnection_EDW=SAS2EDW
 
[s_m_f_acct_txn_daily_a1]
$PMSessionLogFile=$PMRootDir/Logs/SAS_FACT1/WF_FACT1_F.LOG
$DBConnection_EDW=SAS2EDW
$$startdate=to_char(sysdate-10000,'j')
$$enddate=to_char(sysdate, 'j')
 
[s_m_f_asp_setup]
$PMSessionLogFile=$PMRootDir/Logs/SAS_FACT1/WF_FACT1_F.S_M_F_A.LOG
$DBConnection_EDW=SAS2EDW
$$startdate=to_char(sysdate-10,'j')

Thank you.
# 2  
Old 11-18-2010
Is sysdate always on third line from search string? Then try this
Code:
sed "/s_m_f_acct_txn_daily_a1/{n;n;n;s/sysdate-10,/sysdate-10000,/;}" file

# 3  
Old 11-18-2010
how about this,
Code:
 awk '{if(/s_m_f_acct_txn_daily_a1/){print;x=1}else if (x==1 && $0 ~/sysdate-/){split($0,a,",");print a[1]"000"a[2];x=0} else {print}}' inputfile

# 4  
Old 11-18-2010
Code:
awk -vRS="\\[" -vFS="\n" '($0~/daily_a1/){i=NR}(NR==i){sub("0,","0000,",$0)}{printf((NR!=1)?RS:"")$0}' inputfile


Last edited by ctsgnb; 11-18-2010 at 05:33 PM..
# 5  
Old 11-18-2010
Code:
sed '/s_m_f_acct_txn_daily_a1/,/^[ \t]*$/{/startdate/s/\(sysdate-10\),/\1000,/;}' infile

Code:
awk '/s_m_f_acct_txn_daily_a1/{s="startdate=to_char{sysdate-10";sub(s",",s"000")}1' RS= infile

# 6  
Old 11-18-2010
Thanks guys for all your quick responses.

Sorry I did not include all possible data elements in my first example.

Another possible case scenario of data can be:

Code:
[s_m_f_mtg_res_key_meas_monthly]
$PMSessionLogFile=$PMRootDir/Logs/SAS_FACT1/WF_SAS_FACT1.LOG
$DBConnection_EDW=SAS2EDW
$DBConnection_Lkp=EDW_STG
$$startdate=to_char(TRUNC(LAST_DAY(ADD_MONTHS(SYSDATE,-3)))+1,'j')
$$enddate=to_char(sysdate, 'j')

As you can see, the text that I need to replace is not always on the 3rd line and not always "sysdate-" but still want the entry to be modified to "...ADD_MONTHS(SYSDATE,-3000))..." in this example.

First search the string and within that block of data, replace -# (Where # represents 1 or more numbers) with -#000 (Add 3 more zeroes to existing text).

Also, can the code be modified so that you are prompt for the string to search?

Thank you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Modifying text file records, find data in one place in the record and print it elsewhere

Hello, I have some text data that is in the form of multi-line records. Each record ends with the string $$$$ and the next record starts on the next line. RDKit 2D 15 14 0 0 0 0 0 0 0 0999 V2000 5.4596 2.1267 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 ... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

2. Shell Programming and Scripting

How to read all data after a specific string from a text file ?

Hi, I have a file(input.txt) and trying to format as output.txt. See the attached file format. Note: This is a windows file (DOS format) and the commands are also going to execute on windows. Basically I am trying to capture all the data in between Local Group Memberships and Global Group... (10 Replies)
Discussion started by: Monoj2014
10 Replies

3. Shell Programming and Scripting

Need comand or script for append text after searching for the desired string

Hi all, i have a generated report in unix in the following command like input.txt 47.85,10 0124,42.35,8 0125,3.5,2 the input file format is fixed I need the my output file with append text as below output.txt 0124 amount:42.35 0125 amount:3.5 0124 count : 8 0125... (34 Replies)
Discussion started by: hemanthsaikumar
34 Replies

4. Shell Programming and Scripting

find string(s) in text file and nearby data, export to list help

Hi, So I'm kinda new to shell scripts and the like. I've picked up quite a bit of use from browsing the forums here but ran into a new one that I can't seem to find an answer for. I'm looking to parse/find a string AND the next 15 or so charachters that follow the string within a text file... (1 Reply)
Discussion started by: kar23me
1 Replies

5. Shell Programming and Scripting

searching a text string for n'th :

hello, i'm a novice on bsh scripting so thanks for any help here basically i have a shell var $x that looks like this > echo $x nabc1234:!:73394:17155:Gary Mason:/home/garym:/bin/ksh and i'm trying to keep the first 8 characters and the text from the 4th : to the 5th : i've been trying... (9 Replies)
Discussion started by: sasglm
9 Replies

6. Shell Programming and Scripting

String searching and output to a file in a formatted text

Hi, I'm very new to UNIX scripting and find quite difficult to understand simple UNIX syntax. Really appreciat if somebody could help me to give simple codes for my below problems:- 1) I need to search for a string "TTOH 8031950001" in a files which filename will be "*host*'. For example, the... (3 Replies)
Discussion started by: cuji
3 Replies

7. Shell Programming and Scripting

Text data string conversion to Integer

Folks Appreciate your help in understanding issue in relation to below. I need to pul uvalue from a file (tmpfile) and compare it with a number to make decision. Using #!/bin/sh contents of tmpfile : Slot uvalue : 0.16 How I am pulling it: unifval=`awk '/uvalue/ {print $4}' tmpfile` ... (1 Reply)
Discussion started by: wndgs
1 Replies

8. Shell Programming and Scripting

Searching a particular string with spaces in a data file

Hi, I'm new to shell scripting and require your help in achieving the requirement. I have a data file which stores organization name as one of the column data in a csv data file. Organization name stored in data file is: Canadian OU CAD Sample Data file: 1,5,4,5,, ... (9 Replies)
Discussion started by: andy4013
9 Replies

9. Shell Programming and Scripting

PERL: Searching for a string in a text file problem

Looking for a bit of help. I need to search for a string of words, but unfortunately these words are located on separate lines. for example the text output is: United Chanmpions Ronaldo Liverpool Losers Torres and my script code is print("DEBUG - checking file message"); while... (15 Replies)
Discussion started by: meevagh
15 Replies

10. UNIX for Dummies Questions & Answers

Hi! Searching for a text string in UNIX

Hi! I'm new here and glad to meet everyone! I've been wrestling with a problem lately however! How do I recursively (recursive means to keep going through the subdirectories until no more are there) search a bunch of textfiles in a long directory structure for a specific string.. but only... (1 Reply)
Discussion started by: skwadim
1 Replies
Login or Register to Ask a Question

Featured Tech Videos