confused with regular expression


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting confused with regular expression
# 1  
Old 09-07-2007
confused with regular expression

Hi All,

I want to replace all the TAS2(8`h00) ; with TAS(8`h00) ;

But my problem is there are lots of cases like
1. before TAS2 there may be zero,one or more spaces.
2. after TAS there may be zero, one or more spaces.
3. then a open parenthesis.
4. again zero,one or more spaces
5. followed by 8`
6. followed by one or more character A-Z,a-z , 0-9
7. close parenthesis.
8. zero or more spaces
9. last one is the comma in the RE

I ve tried with the following code
for ttt in `find . -name "ad0_signstbin_002.file" -print | grep -v scsim.db.file`
do

grep "^[ \t]*TAS[ \t]*(8'[a-f|A-F|0-9]*)[ \t]*;" | perl -p -i -e 's/TAS/TAS2/' $ttt
done


My file contents are like below

.TAS2 ;
TAS2;
TAS2
lnput TAS2PX ;
output STAS2;
.TAS2();

TAS2(8'h00); // timer TAS.
TAS2(8'h00); // timer TAS.
TAS2 (8'h00); // timer TAS.
TAS2 (8'h00); // timer TAS.
TAS2( 8'b000); // timer TAS.
TAS2 ( 8'b000); // timer TAS.
TAS2(8'h00); // timer TAS.


The all lines with bold letter should be replaced from TAS2 to TAS..

If any other idea please help.

user_pradyu

Last edited by user_prady; 09-07-2007 at 07:23 AM..
# 2  
Old 09-07-2007
try using sed, with which you can replace strings easily.

Code:
sed '1,$s/TAS2/TAS/g' filename

# 3  
Old 09-07-2007
Possible Idea

Not absolutely sure this is what you want but.....

cat filename | sed 's/TAS2/TAS/g'

In my test this results in...

.TAS ;
TAS;
TAS
lnput TASPX ;
output STAS;
.TAS();

TAS(8'h00); // timer TAS.
TAS(8'h00); // timer TAS.
TAS (8'h00); // timer TAS.
TAS (8'h00); // timer TAS.
TAS( 8'b000); // timer TAS.
TAS ( 8'b000); // timer TAS.
TAS(8'h00); // timer TAS.

it preserves the spacing so I am not sure if this is what you want
# 4  
Old 09-07-2007
Quote:
Originally Posted by ajcannon
Not absolutely sure this is what you want but.....

cat filename | sed 's/TAS2/TAS/g'

In my test this results in...

.TAS ;
TAS;
TAS
lnput TASPX ;
output STAS;
.TAS();

TAS(8'h00); // timer TAS.
TAS(8'h00); // timer TAS.
TAS (8'h00); // timer TAS.
TAS (8'h00); // timer TAS.
TAS( 8'b000); // timer TAS.
TAS ( 8'b000); // timer TAS.
TAS(8'h00); // timer TAS.

it preserves the spacing so I am not sure if this is what you want
Sorry I made a mistake in giving my input file . now I corrected in my previouse post .

Thanks for your kind reply and time.. But actually the above will replace all the occurances of TAS2 to TAS. which will drastically change my files which i dont want..


So it should change only when matches my patteren
My patteren is like following

1.starting with zero/one/ more spaces or tabs
2.then TAS2
3.zero/one/ more spaces or tabs
4. then opening parenthesis "("
5.zero/one/ more spaces or tabs
6. 8 followed by " ' "
7.any character "o","b","h" or "d"
8.then followed by any no charater [a-f| A-F| 0-9]
9.zero/one/ more spaces or tabs
10.then closing ")" parenthesis
11.zero/one/ more spaces or tabs
12. finishing with a ";"


an example :- TAS2 ( 8'h00) ;

Where it will find that above expression it should replace TAS2 to TAS but no other changes in that lin.
Earlier I tried with the follwing command but I failed

grep "^[ \t]*TAS2[ \t]*([ \t]*8'[b|o|d|h][0-9|a-f|A-F]*[ \t])[ \t]*;" ad0_signstbin_002.file | sed 's/TAS2/TAS/g'


One more thing I cant able make a ditto copy of my file becoz all tabs and spaces it squezes automatically after posting .

Hope this time my explanation is clear and watting for your reply

User_pradyu

Last edited by user_prady; 09-07-2007 at 07:30 AM..
# 5  
Old 09-07-2007
Getting your need specified

Hi,

your last posting was obviously more detailed than before but I - for one - am still not sure what you want.
May I suggest your repost your original file and use a specific character to represent spaces - say '_' or '*'. That way the posting will not 'corrupt' your messages and we can get a better idea of what you want.
Give the input file *and* what you want as an output.
best regards
# 6  
Old 09-07-2007
Quote:
Originally Posted by ajcannon
Hi,

your last posting was obviously more detailed than before but I - for one - am still not sure what you want.
May I suggest your repost your original file and use a specific character to represent spaces - say '_' or '*'. That way the posting will not 'corrupt' your messages and we can get a better idea of what you want.
Give the input file *and* what you want as an output.
best regards

Input file

--TAS2 ;
t--TAS2;
---tTAS2
TAS2PX
t.TAS2(8);
t.TAS2(9'h0);
-----t.TAS2();
TAS2(8'h00); // timer TAS2. 1
TAS2(10'h00); // timer TAS2. 2
--TAS2(8'h00--); // timer TAS2. 3
t----TAS2-(-8'h00--); // timer TAS2. 4
--t--TAS2--(--8'h00); // timer TAS2. 5
t--TAS2(-8'h00-); // timer TAS2. 6
----tTAS2---(----8'h00--); // timer TAS2. 7
tTAS2----(--8'b00000000); // timer TAS2. 8
t----TAS2(8'h00); // timer TAS2. 9
---tTAS2(8'hff) ; // Just

Note
- represents a space
t represents a tab

output

--TAS2 ;
t--TAS2;
---tTAS2
TAS2PX
t.TAS2(8);
t.TAS2(9'h0);
-----t.TAS2();
TAS(8'h00); // timer TAS2. 1
TAS2(10'h00); // timer TAS2. 2
--TAS(8'h00--); // timer TAS2. 3
t----TAS-(-8'h00--); // timer TAS2. 4
--t--TAS--(--8'h00); // timer TAS2. 5
t--TAS(-8'h00-); // timer TAS2. 6
----tTAS---(----8'h00--); // timer TAS2. 7
tTAS----(--8'b00000000); // timer TAS2. 8
t----TAS(8'h00); // timer TAS2. 9
---tTAS(8'hff) ; // Just

Regards,
Pradyu
# 7  
Old 09-07-2007
Input file:
Code:
--TAS2-;
t--TAS2;
---tTAS2
TAS2PX
t.TAS2(8);
t.TAS2(9'h0);
-----t.TAS2();
TAS2(8'h00);
TAS2(10'h00);
--TAS2(8'h00--);
t----TAS2-(-8'h00--);
--t--TAS2--(--8'h00);
t--TAS2(-8'h00-);
----tTAS2---(----8'h00--);
tTAS2----(--8'b00000000);
t----TAS2(8'h00);
---tTAS2(8'hff)-;

Command:
Code:
sed "/^[-t]*TAS2[-t]*([-t]*8'[obhd][a-fA-F0-9][a-fA-F0-9]*[-t]*)[-t]*;/s/TAS2/TAS/" input_file.txt

Output:
Code:
--TAS2-;
t--TAS2;
---tTAS2
TAS2PX
t.TAS2(8);
t.TAS2(9'h0);
-----t.TAS2();
TAS(8'h00);
TAS2(10'h00);
--TAS(8'h00--);
t----TAS-(-8'h00--);
--t--TAS--(--8'h00);
t--TAS(-8'h00-);
----tTAS---(----8'h00--);
tTAS----(--8'b00000000);
t----TAS(8'h00);
---tTAS(8'hff)-;

If the output is what you want, simply replace all the occurrences of "[-t]" in the sed command with a space and a tab! Hope it works Smilie
Oh, I've removed all the comments at the end of the lines but it should work also with comments.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

sed: -e expression #1, char 0: no previous regular expression

Hello All, I'm trying to extract the lines between two consecutive elements of an array from a file. My array looks like: problem_arr=(PRS111 PRS213 PRS234) j=0 while } ] do k=`expr $j + 1` sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt ---some operation goes... (11 Replies)
Discussion started by: InduInduIndu
11 Replies

2. Shell Programming and Scripting

Regular Expression

Hello, I want to extract text between <td class="di_resultscolumnheader"> and </td>. I wrote the below code to extract text. But I am able to extract the text for the first match only. Can some one help me in this? Thanks in advance. Code: if ($line =~ /<td... (5 Replies)
Discussion started by: koneru_18
5 Replies

3. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

4. Shell Programming and Scripting

Integer expression expected: with regular expression

CA_RELEASE has a value of 6. I need to check if that this is a numeric value. if not error. source $CA_VERSION_DATA if * ] then echo "CA_RELESE $CA_RELEASE is invalid" exit -1 fi + source /etc/ncgl/ca_version_data ++ CA_PRODUCT_ID=samxts ++ CA_RELEASE=6 ++ CA_WEEK_NO=7 ++... (3 Replies)
Discussion started by: ketkee1985
3 Replies

5. Shell Programming and Scripting

Regular Expression Help

Hi there, I have a line in a coded text from which the formtat is DEF/AAA/AAA/AAA/AAA/AAA/AAA/AAA/AAA/AAA where A equals a letter but the fields after the DEF/ are optional. Which means the line could look like DEF/AAA or DEF/AAA/AAA etc etc I am trying to a find regular... (8 Replies)
Discussion started by: sickboy
8 Replies

6. Shell Programming and Scripting

Regular Expression

Hi, I have the following file as shown below: Replace() { sed -e "s+ABCDIR+$DDIR/C+g" \ -e "s+ABCDIR+$DDIR/C+g" \ -e "s + ABCDDIR+$DDIR/C"\ } I need a Regular expression to grep 0nly ABCDIR. if i use grep -i... (3 Replies)
Discussion started by: ravi_rn
3 Replies

7. Shell Programming and Scripting

Regular expression Help

Hi What is the meaning of this in regular expression $k =~ s/^\s*//; Plz explain (3 Replies)
Discussion started by: Harikrishna
3 Replies

8. Shell Programming and Scripting

Regular expression

Hi I have to extract the first field and the last %field of the following out put.. /home (/abc/def/bhd ) : 522328 total allocated Kb 319448 free allocated Kb ... (2 Replies)
Discussion started by: Harikrishna
2 Replies

9. Linux

Regular expression to extract "y" from "abc/x.y.z" .... i need regular expression

Regular expression to extract "y" from "abc/x.y.z" (2 Replies)
Discussion started by: rag84dec
2 Replies

10. Shell Programming and Scripting

Regular Expression + Aritmetical Expression

Is it possible to combine a regular expression with a aritmetical expression? For example, taking a 8-numbers caracter sequece and casting each output of a grep, comparing to a constant. THX! (2 Replies)
Discussion started by: Z0mby
2 Replies
Login or Register to Ask a Question