I am new to shell script. And right now learning regular expression. I am using BASH as a shell and RedHat Linux. I have couple of doubts regarding the regular expression
1) +
This is matches one or more occurrence of the preceding pattern.
So if the file contains following data file.txt
a
aa
aba
bbb
ccc
should print following
a
aa
aba
But to print above I need to have
2) ?
should match 0 or 1 occurrence of the preceding pattern
But when I use
it pritnts nothing.. And when I use
it pritints all the data in the file. i.e output was as below
a
aa
aba
bbb
ccc
I was expecting the output as below
a
bbb
ccc
Now I have a doubt 0 or 1 means exactly 0 or 1 ?? or is it mean 0 or more then 0 ??? if it means 0 or more then 0 then whats the differnece between * (asterisk ) and ? ( question mark )??
3)
and
prints the same data as below
a
aa
aba
I was expecting this output only when I use
and should print below line when I use
a
Problem is, that grep just understands very limited regular expressions until you add in the switch "-E" or use "egrep" and you have to escape a + of course so it isn't interpreted as "just another character":
For 2):
A single character is a dot, not a question mark.
For 3):
{1} also stands for minimum. It's the same as {1,}. And even {1,4} will print out every line where at least one "a" is in. There is a very good pocke guide on awk and sed from O'Reilly. They have a good overview on regular expressions in the first few pages, very helpful.
For
2): A single character is a dot, not a question mark.
For 3):
{1} also stands for minimum. It's the same as {1,}. And even {1,4} will print out every line where at least one "a" is in. There is a very good pocke guide on awk and sed from O'Reilly. They have a good overview on regular expressions in the first few pages, very helpful.
Hi
Thnx for your reply
.
1) But when I refer the book and some other on line articles they says that "?" will print 0 or 1 occurrence of the preceding character ?? ( I was understanding it as exact 0 or exact 1, my mistake )
But now it seems that "*" and "?" are same. or is there any difference ??
2) And what should I do if I need to match the exact number of occurrence in the string.
i.e. I need to find the string that contains only 2 "a" ( not less then 1 and not more then 2 ).
I understood what you want, but to explain the ?, I read about it and fooled around, which might explain it a bit:
So it is as the description says, none or one occurence. Here it is a group of characters, but they interpret as single characters, not as a complete string.
Hello All,
I have a problem in counting number of process getting run with my current script name..
Here it is
ps -ef | grep $0 | grep -v grep
This display just one line with the PID, PPID and other details when i print it in the script.
But when I want to count the numbers in my... (11 Replies)
I don't even know where to start with this one. There is so much out there about different aspects of this. I am starting with a basic Ubuntu 11.04 install. Do I need to configure a DNS? I am a little confused about that. What do I need to do for a domain name? I have followed various tutorials,... (1 Reply)
Hi friends,
This is a small program built on the concept of shared memory. The producer is a separate program and process, and the consumer is a seperate program and process. Both are executed under the same user account. The producer takes some string from the user and adds that string to the... (1 Reply)
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)
Hi,
I was trying to learn forking in C in UNIX. Somehow i still haven't been able to get the concept well. I mean, i do understand that fork creates an exact replica of the parent (other than the fact that parent gets the process id of the child and child gets 0 when fork is called). This is the... (2 Replies)
Good day, everyone!
Could anybody explain me the following situation.
If I'm running similar script:
Var="anna.kurnikova"
Var2="Anna Kurn"
echo $Var | tr -t "$Var" "$Var2"
Why the output is :
anna KurniKova
instead of Anna Kurnikova?
:confused:
Thank you in advance for any... (2 Replies)
I'm an intern at a company that recently bought out another business. In doing so, they inherited a unix system that contains files which they need to retrieve. No one in the company, including myself, really understands or knows unix so please respond with the true assumption that I'm a unix... (1 Reply)
:confused: some one please tell me where i can possibly find out what is unix 10.2 and the basic system functions of it is. I really need help! (1 Reply)
Hi,
In some machines when i type "clear" it completely clears all the contents on that window but on some it simply scrolls up all the content.
How can i change this? (4 Replies)