I want to create .bat application that read into text file, find / and remove it.
FROM PPD/HQ
ALLEN/BRAD/MR
change to:-
FROM PPD HQ
ALLEN BRAD MR
Eliminate the / and save in other text file will be just fine.
I'm a newbie and help me to enter DOS world.
Thnx!
Does this really have to be pure MS-DOS? As in, no Windows, no extra tools? If you mean a "DOS Box" -- really, a Windows console -- and are able to install a little software to help you, this one-liner will do it:
$ tr / ' ' < original-file.txt > new-file.txt
That's Bash shell syntax calling the POSIX tr program, rather than an MS-DOS style batch program. The Unixy command line offered by Cygwin is vastly more powerful than the legacy MS-DOS stuff, even with the minor enhancements Microsoft has added over the years.
Plus, it's a gateway into the Linux/BSD/OS X/Unix world, another fun place to play.
Related
On a windows script i need to replace entire line number of a file (eg: line number 15) with the contents of a variable.
I don´t have a string to search for it will depend of the file but the line number is always the same.
The file in question is of type xml, if necessary i can install any tool that could help me doing this using the windows scripting.
I am out of options, once i see many options of achieving this on linux but not on windows.
Already tried find and replace but since my files are different i don´t have a search pattern
As you seem in dire straits, there are standalone versions the unix/linux sed utility for windows you can install without requiring cygwin or MSYS2 etc.
Sorry, I can't provide a link. It will be better for you to search as you're aware of your local OS version and any other details that might impact which version you select.
But
echo "1
> 2
> 3
> 4" | sed '3s/.*/Something/'
output
1
2
Something
4
Shows one approach.
IHTH
I am using Windows 10 and Strawberry Perl
It is well know that the line terminator in Linux is \n, and in Windows is \r\n.
I found that, on my computer, files of Linux type will automatically transform to windows type \r\n after a replacement operation like
perl -i.bak -pe "s/aaa/bbb/g" test.txt
But this is not what I want, and it seems unreasonable. I would like to know if this is a Strawberry Perl issue, or another factor?
How can I leave the line terminator unaffected on Windows?
This is standard behavior of Perl on Windows (to convert \n to \r\n).
You can get around it by using binmode, which prevents Perl from doing the automatic line-ending conversion.
Your command would then be changed to look like this. It tells binmode to write to STDOUT and then that output has to be redirected to another file. The following command should do what you want (though not in place):
perl -pe "BEGIN{ binmode(STDOUT) } s/aaa/bbb/g" test.txt > newtest.txt
"Actually I set unix format as notepad++ default which is my main editor" I think you should make the effort the keep files with the correct line endings for the appropriate system. You won't make any friends if you keep Linux files everywhere, as it will make it very hard for others to work with your non-standard methodology
It isn't very hard to work with both systems properly, as all you have to do is to make the change automatically when copying from one system to another. You can use dos2unix and unix2dos when making the copy, but it would be a simple job to write a Perl program to update all of your systems with the relevant version of the text files
However, if you insist on this plan, this should help you to achieve it
By default, when running on Windows, perl will use the IO layers :unix and :crlf, which means it works the same as on a Linux system but will translate CRLF to LF on input, and LF to CRLF on output
You can make individual open calls behave differently by adding an explicit pseudo-layer :raw, which removes the :crlf layer. But if you want to modify the special file handlesSTDIN, STDOUT and ARGV then you need a different tactic, because those handles are opened for you by perl
You can use the open pragma at the top of your program, like this
use open IO => ':raw';
which will implicitly apply the :raw layer to every input or output file handle, including the special handles. You can set this from the command line by using
perl -Mopen=IO,raw program.pl
Or you can set the PERLIO environment variable
set PERLIO=raw
which will affect every program run henceforth from the same cmd window
I have a perl script to start a script file with default program.
system("Start C:\\Temp\\test.jsx");
It works file with English user names but when I change user name to ai𥹖Ц中 it doesn't work.
Also no error message appears to I'm not able to debug.
perl on Windows uses so called ANSI functions to interface with the outside world. That means, if you use interesting characters (for example, certain Turkish letters on a US-English Windows install), perl cannot see them. As I wrote on my blog:
You can't pass characters that are outside of the Windows code page to perl on the command line. It doesn't matter whether you have set the code page to 65001 and use the -CA command line argument to perl: Because perl uses main instead of wmain as the entry point, it never sees anything other than characters in the ANSI code page.
For example:
$ chcp 65001
$ perl -CAS -E "say for #ARGV" şey
sey
That's because ş does not appear in CP 437 which is what my laptop is using. By the time it reaches the internals of perl, it has already become s.
So, there is not much you can do with a stock perl. I was working on a set of patches, but things intervened. I may still get around to it this summer.
Now, in your case, you are passing "interesting characters" to an external program via system. The same problem applies. Because perl is using the ANSI versions of functions used to spawn processes etc, the program spawned will not see a Unicode environment. So, if you are trying to use Korean or Japanese programs with a system code page that does not include them, I am not sure what will happen.
There is not much you can do once perl is running. The environment, command line arguments, everything lives in the ANSI world from that point on. There may be funky work-arounds, but for that one would need to know exactly how 'ai𥹖Ц中' gets from your perl program to the external program.
When i wrote a code for Shell Script in windows platform
#!/bin/bash
a=20
b=10
sum=`expr $a + $b`
echo $sum
but tried to execute it on UNIX platform it gave me error '20\r': command not found because Windows file ends with /r/n while UNIX file ends with /n.
Any Idea how to remove and process such error ?
Use dos2unix - "[a] program that converts plain text files in DOS/MAC format to UNIX format" - or a similar command.
Repositories like Git or Hg they have support for 'line ending normalization'. Using a version control system is great for other reasons too!
If transferring via [S]FTP/SCP (eg. WinSCP) specify "TEXT mode" for the transfer the conversion will be done automatically - the newline transcoding is done as part of the copy/transfer process itself.
A number of "advanced" text editors (eg. [x]emacs, [g]vim, sublime, notepad++, and even Visual Studio) can also repair/normalize such files. These editors can also be used to write the file from the start :)
I am looking for a Windows tool (exe) or Python script which can be used on the command line to search and replace strings in text files recursively in a source code tree. I am trying to avoid the custom syntax of sed/awd like linux tools. It needs to be automated, therefore it needs to be command line.
Any suggestions?
I think WinGrep may meet your needs. I believe it can be run from the command line. Link: www.wingrep.com
A little pricey, but PowerGrep has a command line interface or so the manual says.
There is fnr.exe, which is a lightweight utility that supports command line. It doesn't have as many features as grep, but it makes it easy to generate command line script by providing all the parameters in windows form. It is available from here:
http://findandreplace.codeplex.com
NotePad++ worked great. Free add-in.
Search - Find in files...
sed -i%TEMP%\* "s/regexp/replacement/g" *.txt
Backup files will be in temp directory.
Sed can be easily downloaded with chocolatey:
https://chocolatey.org/packages/sed