I have a filename in the format dir1/dir2/filename.txt.org and I like to rename this to dir1/dir2/filename.txt . how can this be done. I tried 'cut' with '.' separator but it also removes .txt
You can try korn shell variable expansion formats, instead of using a subprocess (e.g. cut) . This can be much faster.
example:
var1=dir1/dir2/filename.txt.org
var2=${var1%.*}
If you now print $var2 its value will be dir1/dir2/filename.txt
The % tells it to delete the smallest matching rightmost match for .* (which means anything following the rightmost period character).
${variable%pattern} - return the value of variable without the smallest ending portion that matches pattern.
Other variable expansion formats are available, it is worthwhile to study the docs.
I want to make sure user input has:
Two letters at the start
And the support for any number of optional space characters following these two letters.
Additionally, if at least one space character is provided, optionally allow letters, digits or . characters after it.
Here's the expression I currently have:
[a-zA-Z][a-zA-Z] (?\\s+ (?a-zA-Z0-9.))
And here's my thinking:
[a-zA-Z][a-zA-Z] makes sure the input begins with at least two letters
(?\\s+ begins an optional statement. This optional statement must start with at least one space (I'm on windows which is why I have two slashes).
(?a-zA-Z0-9.)) finishes the optional statement. So, if at least one space is provided, at least one optional character, number or . can also be added.
For instance, ab, ab , ab .s, and ab .asd2 should all be valid inputs.
How do I solve this problem?
The problem with your attempt is that both (?\ and (?a are syntax errors. If you want to create an optional group, you need to write (...)?, not (?...).
(The other issue is that a-zA-Z0-9 in your regex matches literally because it's not part of a character class.)
Besides, \s (to match whitespace) does not exist in POSIX regex.
My suggestion:
^[a-zA-Z]{2}( +[a-zA-Z0-9.]*)?$
That is:
^ # beginning of string
[a-zA-Z]{2} # exactly two letters
(
\ + # one or more spaces
[a-zA-Z0-9.]* # zero or more of: letters, digits, or dot
)? # ... this group is optional
$ # end of string
I am new to Unix systems and trying to learn some thing with help of terminal. I have following question in my mind. If we can write filepath without single quotes in terminal (for ex : mv path1 path2) then why we sometime use single quotes to specify paths. What is the difference between these two?
This is not a question of the operating system, but of the shell you use. You can actually chose what shell you want to use on a unixoid system if multiple are installed (which usually is the case).
In general the shell has to interpret the input you make. It has to decide how to handle the tokens of the input. What to consider as the "command" you want to execute, what as arguments. For the arguments it has to decide if the string is meant as a single argument or multiple arguments.
Without quotes (single or double quotes), whitespace characters are considered separators between words, words are typically considered separate arguments. So you can specify multiple arguments for a single command. If that is not desired then you can use quote characters to group multiple words separated by whitespace characters into a single argument, for example a folder name containing a space character. This works because now the shell knows that you want everything following the quote character to be considered as a single argument up to the next matching quote character (actually except escaped ones...).
It's used to escape spaces in file names, otherwise, a backslash is needed. For instance:
$ rm spaces\ in\ file\ name
$ rm 'spaces in file name'
If your file path does not have spaces, it's probably safe to omit the quotes.
This is my lexical analyzer code when I enter as an input the following :
/*This is an example */
program
var a,b:integer;
begin
a =2;
b =a+5;
write(a);
if b==1 then write(a);
end
the output must be like this :
<res,program>
<res,var> <id,a>,<id,b>:<res,integer>;
<res,begin>
<id,a> <assign,=><num,2>;
<id,b> <assign,=><id,a><addop,+><num,5>;
<res,write>(<id,a>);
<res,if> <id,b><relop,==><num,1> <res,then> <res,write>(<id,a>);
<res,end>
but I my output is :
Lexical Error~/hedor1>exampler < input\ .txt
<res,program><res,var><id,a>,<id,b>:<res,integer>;<res,begin><id,a><assign,=><num,2>;<id,b><assign,=><id,a><addop,+><num,5>;<res,write>(<id,a>);<res,if><id,b><relop,==><num,1><res,then><res,write>(<id,a>);<res,end>
I don't know why it just avoids the newline and doesnot print it to the output although I have defined that in my patterns section \n printf("\n");
what is the problem?
Nowhere in your input do you have a single newline by itself. All you have are sequences of one or more whitespace characters (spaces, tabs and newlines). Since you have a rule that matches that, Flex uses the longest match.
Flex generates a greedy parser, which tries to match as much of the input as possible. For example, if it sees the input reality, it doesn't stop after matching real and then go on and match ity as a separate token. Instead, it matches all of reality.
In the same way, in your input after the starting comment you have not one but two newlines (since there is an empty line there), and this will be matched by your {whitespace}+ rule, instead of twice by the \n rule.
I want to write simple program in C equivalent to the regular expression:
/<rr>(.*?)<\/rr>/<test>$1<\/test>/gi.
Does anyone have examples?
It helps if you understand what the regex is supposed to do.
The pattern
The parentheses (...) indicate the beginning and end of a group. They also create a backreference to be used later.
The . is a metacharacter that matches any character.
The * repetition specifier can be used to match "zero-or-more times" of the preceding pattern.
The ? is used here to make the preceding quantifier "lazy" instead of "greedy."
The $1 is likely (depends on the language) a reference to the first capture group. In this case it would be everything matched by (.*?)
The /g modifier at the end is used to perform a global match (find all matches rather than stopping after the first match).
The /i modifier is used to make case-insensitive matches
References
regular-expressions.info, Grouping, Dot, Repetition: *+?{ā¦}