Windows script replace entire line - windows

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

Related

How to extract only version from "go version" command using sed or other bash command in bash shell script running on Debian 10

The command go version currently prints go version go1.13.6 linux/amd64. I installed from the go website rather than Debian packages as the version is old. Therefore traditional ways to extract the version number like dpkg -s cannot be used.
I've explored sed commands to extract only the number (1.13.6) like this other question on this site which is similar I grant you, however after reading various sources online about whats possible with sed and my limited knowledge I've been unable to work out how to tell sed to find the starting point, yet alone make it future proof for new versions which may be slight alterations of this number format. I've tried to explore ways to say "find the 3rd to last number" so that I can then work backwards. Or, "find the 2nd word 'go'".
Current efforts have been purely theoretical, as I can't find where to begin, I've not included any attempts.
Can it be done?
$ v=`go version | { read _ _ v _; echo ${v#go}; }`
$ echo $v
1.13.6
Further reading:
Compound commands.
The read comand.
Parameter expansion.
Command substitution.

Cygwin on Windows 7 (64bit): No such file or directory - but 'which' does give me the correct path

A formely working bash script no longer works after switching computers. I get the following error:
No such file or directory.
Before going on, please excuse any mistakes you may find since english is not my native language.
The script was used in cygwin under Windows XP. I now had to switch to cygwin64 under Windwos 7 (64bit).
The script is used as a checkhandler for the program SMSTools3 to split a file with a specific format into multiple smaller ones, which the program then uses to send SMS to multiple recipients. The script was copied directly from the page of SMSTools3 and uses the package formail.
After looking up the error the most likely problem was that the environmantle path was not set up to look in the right path (/usr/bin). I therefore added it to the path but to no avail.
I then deleted other entries in the enviromental path of windows which contained spaces because that could have been another explanation, but again to no avail.
Following is a minimal example of the code which produces the error.
#!/bin/bash
# Sample script to allow multiple recipients in one message file.
# Define this script as a checkhandler.
echo $PATH
which formail
outgoing="/var/spool/sms/outgoing"
recipients=`formail -zx "To:" < "$1"`
I added the lines the lines echo $Path and which formail to show if the script can find the correct file. Both results look fine, the second command gives me the right output '/usr/bin/formail'
But the line recipients=... throws me the error:
No such file or directory.
I do not have much experience with bash scripting, or cygwin in general. So if someone on this wonderful board could help me solve this problem, I would be really grateful. Thank you all for your help.
EDIT:
First of all thank you all for your comments.
Secondly, I would like to apologize for the late reply. The computer in question is also used for other purposes and my problem is part of a background routine, so I have to wait for "free time" on the pc to test things.
For the things #shellter pruposed: The ls command returned an error: '': No such file or directory.
The which -a formail as well as the echo $(which -a formail) commands that #DougHenderson pruposed returned the 'right' path of /usr/bin/formail. echo \$1 = $1 before the recipent line returned the path to the checkhandler file (/usr/local/bin/smsd_checkhandler.sh), the same command after the recipent line seems to show a empty string ($1 = ). Also, the pruposed change to the recipent line did not change the error.
For the dos2unix conversion that #DennisWilliamson pruposed, I opened the file in notepad++ to use their build in converion, but it showed me that the file is in unix format with Unix style line endings.

gdalinfo - how to pause the outputting data

I am using GDAL. in command prompt, i am doing
$ gdalinfo (my file location)
It works but because it is a huge file the command gives a lot of information. I am only interested in seeing what's near the beginning. The command prompt only allows scrolling up to the last 1000 or so lines of info (it must give about 100,000 lines or so). How can I do this?
This will depend on the OS and utilities it provides. I am assuming you are using a POSIX OS which support pipes and provides utilities such as less/more. The command in this case would be:
$ gdalinfo file.tif | less
If less is unavailable, you may have the more command installed. You can also save the output from gdalinfo into a file and look at the file later.
$gdalinfo tile.tif > output.txt
n
[Appended]
On Windows, I get a truncated response like this:
C:\Users\elijah>gdalinfo "C:\xData\GreeneCountyMo\ortho_1-1_1n_s_mo077_2010_1.sid" | more
(Use ENTER/RETURN to advance to the next line, and CTRL+C to "escape" when you're finished.)
Or I can do the outfile as well:
C:\Users\elijah>gdalinfo "C:\xData\ortho_1-1_1n_s_mo077_2010_1.sid" > "C:\xData\gdalinfo.txt"
If you are on a windows machine... What type of file are you using? Perhaps it contains a lot of ground control points, which you can skip using the -nogcp flag, or skip the metadata using the -nomd flag (see http://www.gdal.org/gdalinfo.html). Also, see --help-general; you might have the --debug flag set to on?

Suggestions for a command line search-and-replace tool on Windows

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

Do you know a similar program for wc (unix word count command) on Windows? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
A quick search gives me tawbaware wc, but it does not accept stdout as input stream, meaning I can not use pipe within a DOS session.
Note:
I can not install cygwin or use powershell (which would have allowed a '|foreach-object {(get-content $_).count}')
unxutils and and gnuwin32 Packages might have this feature...
Even easier, find /c.
ex:
netstat -an | find /c "ESTABLISHED"
find /c: Displays only the count of lines containing the string.
You can use the original "wc", built for windows: it is part of the coreutils package. Get the most recent coreutils exe.
For unix tools on windows your options are:
msys - similair to unixtools, originally just a few build tools needed to go with mingw (native version of gcc), now has almost all of the cygwin tools
cygwin - just about everythign for unix, complex install and requires a dll to provide unix api. Can be problems mixing tools built with different versions of cygwin.dll
Unixtools - not all the tools provided by cygwin but compiled natively
ch - pretty much all the unix tools, compiled natively. And a shell which includes a 'c' interpreter. The standard version is free (beer) but not open source.
uwin - free from ATT, includes the korn shell if you like that sort of thing.
mks a Commercial port of unix tools. Rather expensive given the free versions available.
Try:
find /c /v "~any string that will never occur~"
This command gives a count of all lines that DO NOT contain the search string. Testing it, I see a problem that it doesn't seem to count blank lines at the end of a file.
Well, I'm sorry to disagree, but unxutils do have a wc.exe
Give it a try!
Cheers,
My unxutils pack has word count:
C:\Java\vssWorkspace\java\portlets_core>wc
-l C:\Users\malp\AppData\Local\Temp__portlets41366.html
79717
C:\Users\malp\AppData\Local\Temp__portlets41366.html
Besides, the unxutils page indicates wc.exe is available. Are you looking for something that wc.exe does not handle?
Here are two other (pure Windows CMD) ways to count lines in a git log:
set n=0
for /f %a in ('git log --oneline') do set /a n=n+1
Or:
git log --online | find /v /c ""
The advantage of the first one is that you end up with the value in an environment variable you can do stuff with. But it might perform slow with huge files.
There is also WinXs 4.2, it's shareware, so you could see if it'll do what you need it to.
Could you install a script language for this? It might be overkill, but if it gets the job done with a minimum of fuss...
I found this thread and was charmed by the innovative solutions for emulating wc using just the tools that Windows has built in. This stimulated an answer to my need for a character count so that I might prevail in my battle with a web form field's max character warning.
If you want wc -c which gives a byte count you can use DEBUG, a DOS utility (which isn't listed by the HELP command) in Windows. Character count should equal byte count minus the line count times the size of a newline, which is one newline character for Unix ('\n') or two characters, carriage return + linefeed ('\cr' and '\lf' or '\0Dh' '0Ah' for a DOS plain text file.
Char Count = Byte Count - (Line Count * sizeof("\n"))
To do this open a command line window (Start->Run->Open: "cmd"), run debug on the plain text file and examine the CX register which contains the length of the file loaded:
Debug [pathname]
-rcx
CX [filelength in hex]
:
-q
Then run find on the file:
find /v /c "notlikelystring"
---------- [pathname]: [line count]
And apply the formula.
getgnuwin32 facilitates downloading and installing of gnuwin32 (which certainly has wc utility).

Resources