confused with regular expression


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

Quote:
Originally Posted by robotronic
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.
Thanks a lot ajcannon , I ll try it ..
# 9  
Old 09-08-2007
Bug

Quote:
Originally Posted by user_prady
I ve tried the above sed code its working for all the cases.
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 "/^[ ^I]*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 "^[ ^I]*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
# 10  
Old 09-08-2007
If you're using gnu sed, there's the "-i" option which edits the input file in place:

Code:
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.
# 11  
Old 09-10-2007
Quote:
Originally Posted by robotronic
If you're using gnu sed, there's the "-i" option which edits the input file in place:

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

Last edited by user_prady; 09-13-2007 at 11:21 PM..
# 12  
Old 09-13-2007
How to use two regular expression in one

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,$2*5)};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

Last edited by user_prady; 09-13-2007 at 11:26 PM..
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