I have a textfile and would like to save all lines that are ending with .m2 or .M2. I tried several ways including this here
D:\filetype\core\sed.exe -n -e "s/^\(.*\)\.M2//" D:\filetype\listfile\test.txt > D:\filetype\listfile\test2.txt
But as result i only get a emtpy textfile, so i guess something is wrong with my code.
The other way was
D:\filetype\core\sed.exe -n -e "^/\(.*)\/.M2\/" D:\filetype\listfile\test.txt > D:\filetype\listfile\test2.txt
But in this case i wasn't able to locate the source of the error
unknown command: `^'
Thanks if someone can see my fault.
You can use below sed
command:
sed -n -e '/\.[mM]2$/p' <file_name>
This will print all the lines which have .m2
or .M2
at the end
Now comming to issues with your commands. Your first command does:
sed -n -e "s/^\(.*\)\.M2//"
which is a search and replace
command indiacated by s
in the command. Syntax for this command is s/search_text/replace_text/
. Now if you are look at your command carefully, you are searching for something and replacing it with nothing
- as indicated by last //
in your command - where replace_text
is missing.
Your second command does
sed -n -e "^/\(.*)\/.M2\/"
which is incorrect syntax. General syntax of a sed command is :
sed -e 'line_range command'
where line range - which is optional - can be line numbers like1
, 5
, 2-5
, or a regular expression like /[gG]/
, /[gG][iIuU]rl/
.
If line_range
is missing, the first letter in sed command should be a command. In your case: line_range
is missing - which is fine syntax wise -, however the first letter is ^
- which is not a sed command - because of which you are getting syntax error.
The command that I suggested is
sed -n -e '/\.[mM]2$/p'
Here, line_range
is the regular expression /\.[mM]2$/
- which says "any line which has .m2 or .M2 at the end"
, and command is p
, which is the letter for print command.