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

try using sed, with which you can replace strings easily.

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

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 "[1]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


  1. \t ↩︎

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

Input file:

--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:

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

Output:

--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 :slight_smile:
Oh, I've removed all the comments at the end of the lines but it should work also with comments.

Thanks a lot ajcannon , I ll try it ..

But Is there any way to change the file permanently without any inbetween files.
If I m not wrong I think in the above sed command I ve to redirect to file then move to the original file name like

sed "/[1]TAS2[ ^I]([ ^I]*8'/s/TAS2/TAS/" file1 > file2
mv file2 file1

Like in perl if we give the command like below it changes the input file

grep "[2]TAS2[ ^I]([ ^I]*8' "| perl -p -i -e 's/TAS/TAS2/' $ttt

Is there any same way to do it in sed commmand

Thanks Robotonics again for your kind help
Regards,
user_prady


  1. ^I ↩︎

  2. ^I ↩︎

If you're using gnu sed, there's the "-i" option which edits the input file in place:

sed -i "..." input_file.txt

Try the man page for more details. If you don't have gnu sed you have to use an intermediate file like you said.

Thanks a LOOOOOOOT To Allllllllllllllllllllllllll..

Hello Experts,

Please help me to cope with the following problem

I ve patterens like

Input
Noptx(5) // remain the same
-*Nop(3);
Nop(9);
--Nop(8); // remain the same d3
**---Nop(7); //remain the same d3
**---Nop(7);
*--Nop(6);
--**Nop(5);
-Nop(4);
Nop(3);

  • represents a space
  • represents a tab space
    Desired OUPUT

Noptx(5) // remain the same
-*Nop(6);
Nop(18);
--Nop(8); // remain the same d3
**---Nop(7); //remain the same d3
**---Nop(14);
*--Nop(12);
--**Nop(10);
-Nop(8);
Nop(6);

I want to change the no to two times but which matches the below patteren

CASE-1: Zero or more spaces/tabs then Nop AND
CASE-2: The line should not end with d3 .

Basically if both patteren matches then double the no inbetween ()
Previously I ve a suggestion but that wont work for the first statement in my input file

awk -F '\\([-@ ]|\\)' 'NF==3{sub($2,$25)};1' file1

Now my code only does the CASE-1 but how to overcome both I ve no idea .

gawk '
/^[ \t]*Nop\(/{
split($0, a, /\(|\)/);
print a[1] "(" a[2]*2 ")" a[3] "// d3" >> "/tmp/my_tmp";
next
}
{ print $0 > "/tmp/my_tmp" } ' file

pls help.......
Regards
User_Prady