How to replace anything before a pattern?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to replace anything before a pattern?
# 1  
Old 10-26-2010
How to replace anything before a pattern?

Hi,
I have the following text file:
Code:
facdat=$SPDR_SHARED_SCRIPTS/mdb_facdat.pl -s fnma;
upddte=$AGENCY_SCRIPTS/get_upddte.pl -f $EMBS_DAILY_DIR/$filenm
$SASRUN $AGENCY_SCRIPTS/fnma_daily_factors.sas -sysparm "$facdat $upddte"
$SASRUN $AGENCY_SCRIPTS/fnma_daily_addtn.sas -sysparm "$facdat $upddte"
repret=$SPDR_SHARED_SCRIPTS/mdb_facdat.pl -s $sectyp -r Y

I want to replace anything before '$AGENCY_SCRIPTS' and '$SPDR_SHARED_SCRIPTS' I want my output to look like below
Code:
$SPDR_SHARED_SCRIPTS/mdb_facdat.pl -s fnma;
$AGENCY_SCRIPTS/get_upddte.pl -f $EMBS_DAILY_DIR/$filenm
$AGENCY_SCRIPTS/fnma_daily_factors.sas -sysparm "$facdat $upddte"
$AGENCY_SCRIPTS/fnma_daily_addtn.sas -sysparm "$facdat $upddte"
$SPDR_SHARED_SCRIPTS/mdb_facdat.pl -s $sectyp -r Y

# 2  
Old 10-26-2010
Code:
sed '
  s/.*\($AGENCY_SCRIPTS\)\>/\1/
  t
  s/.*\($SPDR_SHARED_SCRIPTS)\>/\1/
 '

# 3  
Old 10-26-2010
Code:
sed -e 's/.*\($AGENCY_SCRIPTS\)/\1/' -e 's/.*\($SPDR_SHARED_SCRIPTS\)/\1/'

# 4  
Old 10-26-2010
The t saves/prevents looking for B if you find A. While this list is trivial, longer lists can be created with the most likely at the top to save processing, when you do many possible transforms.
This User Gave Thanks to DGPickett For This Post:
# 5  
Old 10-26-2010
Code:
sed 's/.*\($[AS][GP].*\)/\1/'

Code:
[ctsgnb@shell ~]$ cat ramk
facdat=$SPDR_SHARED_SCRIPTS/mdb_facdat.pl -s fnma;
upddte=$AGENCY_SCRIPTS/get_upddte.pl -f $EMBS_DAILY_DIR/$filenm
$SASRUN $AGENCY_SCRIPTS/fnma_daily_factors.sas -sysparm "$facdat $upddte"
$SASRUN $AGENCY_SCRIPTS/fnma_daily_addtn.sas -sysparm "$facdat $upddte"
repret=$SPDR_SHARED_SCRIPTS/mdb_facdat.pl -s $sectyp -r Y
[ctsgnb@shell ~]$ sed 's/.*\($[AS][GP].*\)/\1/' ramk
$SPDR_SHARED_SCRIPTS/mdb_facdat.pl -s fnma;
$AGENCY_SCRIPTS/get_upddte.pl -f $EMBS_DAILY_DIR/$filenm
$AGENCY_SCRIPTS/fnma_daily_factors.sas -sysparm "$facdat $upddte"
$AGENCY_SCRIPTS/fnma_daily_addtn.sas -sysparm "$facdat $upddte"
$SPDR_SHARED_SCRIPTS/mdb_facdat.pl -s $sectyp -r Y
[ctsgnb@shell ~]$
#


Last edited by ctsgnb; 10-26-2010 at 04:09 PM.. Reason: code fix ... Thx to jlliagre for noticing me the bug
# 6  
Old 10-26-2010
Quote:
Originally Posted by ctsgnb
Code:
sed 's|^[^$]*||' infile

doesn't match the OP requirements with:
Code:
 $SASRUN $AGENCY_SCRIPTS/fnma_daily_factors.sas -sysparm "$facdat $upddte"

# 7  
Old 10-26-2010
Oooops, you right, i missed that one ...
Code:
sed 's/.*\($[AS][GP].*\)/\1/' input


Quote:
Originally Posted by jlliagre
doesn't match the OP requirements with:
Code:
 $SASRUN $AGENCY_SCRIPTS/fnma_daily_factors.sas -sysparm "$facdat $upddte"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

sed command to replace two character pattern with another pattern

Not able to paste my content. Please see the attachment :-( (2 Replies)
Discussion started by: vivek d r
2 Replies

3. Shell Programming and Scripting

Replace pattern with new one

Hi, I have file as follows. And i want to remove {,} and ' from the line with :4488: to :-}. INPUT FILE :2020:WITCH13288008772 :4488:20131015INR250000,03 :5500:00000060020001752 {Mr. Chintan ' Add1} . { } Add2 :5518:WITC0000822 -} OUTPUT FILE :2020:WITCH13288008772... (10 Replies)
Discussion started by: sandipgawale
10 Replies

4. Shell Programming and Scripting

Linux: replace pattern with pattern

Hi, I am a rookie of Linux. I have a problem on how can I replace a certain pattern in Linux with nothing. Can anyone help me?:( sample.txt: <binding>App189 ABC SampleMachine1 ABC XXX YYY ZZZ </binding> <binding>App190 ABC SampleMachine2 ABC XXX YYY ZZZ </binding> <binding>App191... (3 Replies)
Discussion started by: dirkaulo
3 Replies

5. Shell Programming and Scripting

pattern match and replace another pattern in same line

I have a pattern username:x:32005:32006::/usr/local/user:/bin/bash I need to match the line containing username and replace /bin/bash with /usr/local/my/bin/noshell So it becomes username:x:32005:32006::/usr/local/user:/usr/local/my/bin/noshell (7 Replies)
Discussion started by: anilcliff
7 Replies

6. Shell Programming and Scripting

replace pattern after the first pattern match

I need this. aaa OOOOO bbb ccc OOOOO ddd fff ggg OOOOO iii OOOOO I need all OOOOO replaced with PPPPP, but only change after the pattern ggg. So the first two OOOOO should not be changed. OUTPUT should be :- aaa (2 Replies)
Discussion started by: anilcliff
2 Replies

7. Shell Programming and Scripting

how to replace a pattern

1) I have strings like arun1 arunkumar1 abcd1 I want to replace them to arun2 arunkumar2 abcd2 I tried using sed, it dint work sed 's/a.*1/a.*2/g' I'm using AIX machine. 2) I have another question sample text below (3 Replies)
Discussion started by: arunk2000
3 Replies

8. Shell Programming and Scripting

How to replace pattern

Hello EveryOne, I have a file "test" with following entry. this is test prtestad yes this is test . Above i have colored pr and ad . Actually requirement is to replace all words starts with pr and ends with ad should be replaced by word prahlad . Means after changes... (9 Replies)
Discussion started by: shirsha
9 Replies

9. Shell Programming and Scripting

perl:: search for tow pattern and replace the third pattern

Hi i want to search two pattern on same line and replace onther pattern.. INPut file aaaa bbbbb nnnnnn ttttt cccc bbbbb nnnnnn ppppp dddd ccccc nnnnnn ttttt ffff bbbbb oooooo ttttt now i want replace this matrix like.. i want search for "bbbbb","nnnnnn" and search and replace for... (4 Replies)
Discussion started by: nitindreamz
4 Replies

10. Shell Programming and Scripting

SED Search Pattern and Replace with the Pattern

Hello All, I have a string "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031", and I just want to extract LLSV1, but I dont get the expected result when using the sed command below. # echo "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031" | awk '{print... (4 Replies)
Discussion started by: racbern
4 Replies
Login or Register to Ask a Question