Linux sed in windows

Windows Subsystem Linux sed commando not working through bat file

I have the linux subsystem installed and when I run the ubuntu app or login to the subsystem console via wsl or bash I can run the following command without any problems:

sed -i 's/.*loginscreen.username.*/loginscreen.username=>/' /mnt/c/Users/>/.runelite/profiles/>/settings.properties 
@ECHO OFF bash -c sed -i 's/.*loginscreen.username.*/loginscreen.username=>/' /mnt/c/Users/>/.runelite/profiles/>/settings.properties PAUSE 
Usage: sed [OPTION]. [input-file]. -n, --quiet, --silent suppress automatic printing of pattern space --debug annotate program execution -e script, --expression=script add the script to the commands to be executed -f script-file, --file=script-file add the contents of script-file to the commands to be executed --follow-symlinks follow symlinks when processing in place -i[SUFFIX], --in-place[=SUFFIX] edit files in place (makes backup if SUFFIX supplied) -l N, --line-length=N specify the desired line-wrap length for the `l' command --posix disable all GNU extensions. -E, -r, --regexp-extended use extended regular expressions in the script (for portability use POSIX -E). -s, --separate consider files as separate rather than as a single, continuous long stream. --sandbox operate in sandbox mode (disable e/r/w commands). -u, --unbuffered load minimal amounts of data from the input files and flush the output buffers more often -z, --null-data separate lines by NUL characters --help display this help and exit --version output version information and exit If no -e, --expression, -f, or --file option is given, then the first non-option argument is taken as the sed script to interpret. All remaining arguments are names of input files; if no input files are specified, then the standard input is read. GNU sed home page: . General help using GNU software: . 

Basically I want to have a bat file which changes text in a file and after that execute an executable.
I tried to use sed to change the text but that didn’t work.
How do I have to change my .bat file to make the script change the text using sed or any other method? sed version: 4.7 Ubuntu version: Ubuntu 20.04.1 LTS

Читайте также:  Linux window operating system

Источник

Is There any sed Like Utility for cmd.exe?

sed” is Linux/Unix-based command line utility also known as stream editor. It is primarily used to filter text from massive files, but it is also referred to as a Text processor and Text editor. It filters and modifies the text line by line. Sometimes, we must replace or filter some text from a massive file. In such a situation, it becomes difficult to replace specific words from the whole text file and line by line manually. Therefore, you can utilize sed commands in Linux and Unix operating systems.

So, what to do and how to use the sed command in the Windows operating system? In this blog, we will demonstrate sed like utilities for Windows.

Method 1: Windows Equivalent of sed Command

As we have discussed, sed is Unix and Linux-based command line utility and cannot be directly usable on Windows. But, thanks to Windows PowerShell as it provides the “Get-Content” command. The Get-Content command supports many parameters, and thus it can be used to perform the functionalities of the “sed” command in windows.

Look at the syntax of the “Get-Content” cmdlet:

Get-Content” is used to access the file content, the “-replace” parameter is used to replace the specified string. Here “string” is an existing value that needs to be replaced with a “replace” string.

Let’s practice the Get-Content command to perform the functionality of the “sed” command in windows.

Example 1: Replace a String in a File

One of the primary uses of the sed command is to find and replace the matched string in a text file. Let’s see how we can do it in Windows.

First, check the actual content of the targeted file using the type command:

Now, we will replace the string “Hello World” with the “My File” string. For instance, the command stated below can be used to match and replace the string in a file:

Example 2: Remove a String in a File

The Linux and Unix sed command can also be used to remove text or string from the file. For this purpose, take a look at the below-provided command:

> Set-Content -Path «File1.txt» -Value ( get-content -Path «File1.txt» | Select-String -Pattern ‘Hello World!’ -NotMatch )

Here, “Set-Content” is used to set the content that is selected with the help of the “Get-Content” command.

In our case, we have removed the “Hello World!” and set the remaining content in File1.txt:

Читайте также:  Права linux запуск файла

Let’s verify whether the specified string or line was removed or not by viewing the file using the “type” command:

You can see that we have successfully removed the “Hello World!” string from the File1.txt file:

We have offered different commands that can be versions of Unix/Linux sed commands.

Alternate Method: Use sed Command on Command Prompt

Although you cannot use sed commands on Windows, there is a third-party tool named “GnuWin32” that provides sed command line utility that can be usable on Windows Command Prompt. Therefore, to use the “sed” command, we need to install it first on Windows.

How to Install sed Command on Windows?

GnuWin32 is an open-source project that provides an executable program for the system and open-source tools, and the sed command is one of them.

To install the sed command line utility on Windows, follow up on the listed steps.

Step 1: Download sed Installer

Click the below link and download sed installer:

Step 2: Execute sed Installer

Execute the sed installer from the “Downloads” directory:

Step 3: Install sed

The sed setup wizard will display on the screen. To start sed installation, click on the “Next” button:

Mark the “I accept the agreement” radio button and hit the “Next” button:

Continue with the default selected installation location and hit the “Next” button:

Select the sed components you want to install and click the “Next” button:

There is no requirement to create the Start menu folder for sed. Therefore, mark the “Don’t create a Start Menu folder” and press the “Next” button:

Click the “Next” button to move forward:

Lastly, hit the “Install” button to begin the sed command installation:

After completing the sed command installation, hit the “Finish” button:

Step 4: Copy sed Command Installation Path

Open the location where you have installed the sed command and copy its path. However, by default, sed is stored in the “C:\ProgramFiles(x86)\GnuWin32\bin” path:

Step 5: Add sed to Window Path

Open the Windows Advanced Settings by searching “Advanced Settings” in the Start menu:

Next, hit the “Environment Variables” button:

After that, from the “System variables” settings panel, select the “Path” and hit “Edit” button:

In the next step, click the new button, paste the sed installation path there, and press the “OK” button:

Step 6: Verify sed Installation

Check the sed version in Command Prompt to verify sed command installation on Windows:

Читайте также:  Linux check ssl cert

The above-given output states that we have successfully installed sed version “4.2.1” on our system.

How to Use sed Command on Windows?

The sed commands help users to perform frequently required operations, such as finding, replacing, and removing text or strings from files. To check the use of sed commands, go through the below-provided examples.

Firstly, view the actual file content to understand the effect and use of the sed command on files. To view file content, use the “type” command:

Example 1: Replace Content of One File to Another File Also Add Line Number to Each String

To add line number to each file and save it in another file, utilize the provided command:

> SED = «C:\Users\anuma\Downloads \F older \F ile1.txt» > «C:\Users\anuma\Downloads \F older \F ile2.txt»

The above command “SED” is utilized to copy the content of the target file and place it in another output file. It will also add a line number with each line:

To display the output file, execute the “type” command:

You can see that the content of File1.txt is placed in File2.txt along with the line number. We can also make a change in the targeted file by using the source file as an output file:

Example 2: Remove spaces and Align Text or String

Let’s remove the extra spaces and lines and make the content align using provided command:

> SED «N;s/ \n / \t /» «C:\Users\anuma\Downloads \F older \F ile1.txt» > «C:\Users\anuma\Downloads \F older \N ewFile.txt»

Here, “/n” is used suppress pattern space in above command:

Let’s verify by viewing the resultant output file “NewFile.txt

Here you can see that we have successfully suppressed spaces make content align:

We have offered the Windows version of sed command and also explain how we can use sed command in Windows Command Prompt.

Conclusion

sed is the command line utility of Unix and Linux operating systems which cannot be directly accessible in Windows operating systems; however, Windows PowerShell provides commands that are Windows versions of sed command, such as the “Get-Content” command with the “-replace” parameter to replace string or text. You can also install sed commands using GnuWin32 third-party projects. In this study, we have learned the Windows equivalent of the sed command and how we can install the sed command on Windows.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.

Источник

Оцените статью
Adblock
detector