Replace whole text in a text file using command prompt? - text

Is there a way to replace whole text from a text file using command prompt? As far as I have come across is about replacing part of a text through find and replace. Is there a way to replace the complete texts from text file?
powershell -Command "(gc Pro.txt) -replace 'One', 'Two' | Out-File -encoding ASCII Pro.txt"

Found it, the way to replace whole text is:
#echo myText > Pro.txt

Related

Data hidden in jpg

I am currently looking for hidden data in a jpg file but I have no clue on how to operate.
There is a jpg file containing text in a format I have never seen before :
-ne \xff\xd8\xff\xe0\x00\x10\x4a\x46\x49\x46\x00\x01\x01\x01\x00\x60\x00\x60\x00\x00\xff\xdb\x00\x43\x00\x06\x04\x04\x05\x04\x04\x06\x05\x05\x05\x06\x06\x06\x07\x09\x0e\x09\x09\x08\x08\x09\x12\x0d\x0d\x0a\x0e\x15\x12\x16\x16\x15\x12\x14\x14\x17\x1a\x21\x1c\x17\x18\x1f\x19\x14\x14\x1d\x27\x1d\x1f\x22\x23\x25\x25\x25\x16\x1c\x29\x2c\x28\x24\x2b\x21\x24\x25\x24\xff\xdb\x00\x43\x01\x06\x06\x06\x09\x08\x09\x11\x09\x09\x11\x24\x18\x14\x18\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\x24\xff\xc0\x00\x11\x08\x01\x8e\x03\x4e\x03\x01\x22\x00\x02\x11\x01\x03\x11\x01\xff\xc4\x00\x1f\x00\x00\x01\x05\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\xff\xc4\x00\xb5\x10\x00\x02\x01\x03\x03\x02\x04\x03\x05\x05\x04\x04\x00\x00\x01\x7d\x01\x02\x03\x00\x04\x11\x05\x12\x21\x31\x41\x06\x13\x51\x61\x07\x22\x71\x14\x32\x81\x91\xa1\x08\x23
-ne \x42\xb1\xc1\x15\x52\xd1\xf0\x24\x33\x62\x72\x82\x09\x0a\x16\x17\x18\x19\x1a\x25\x26\x27\x28\x29\x2a\x34\x35\x36\x37\x38\x39\x3a\x43\x44\x45\x46\x47\x48\x49\x4a\x53\x54\x55\x56\x57\x58\x59\x5a\x63\x64\x65\x66\x67\x68\x69\x6a\x73\x74\x75\x76\x77\x78\x79\x7a\x83\x84\x85\x86\x87\x88\x89\x8a\x92\x93\x94\x95\x96\x97\x98\x99\x9a\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xff\xc4\x00\x1f\x01\x00\x03\x01\x01\x01\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\xff\xc4\x00\xb5\x11\x00\x02\x01\x02\x04\x04\x03\x04\x07\x05\x04\x04\x00\x01\x02\x77\x00\x01\x02\x03\x11\x04\x05\x21\x31\x06\x12\x41\x51\x07\x61\x71\x13\x22\x32\x81\x08\x14\x42\x91\xa1\xb1\xc1\x09\x23\x33\x52\xf0\x15\x62\x72\xd1\x0a\x16\x24\x34\xe1\x25\xf1\x17\x18\x19\x1a\x26\x27\x28\x29\x2a\x35\x36\x37\x38\x39\x3a\x43\x44\x45\x46\x47\x48\x49
This is just the beggining of the file as there is at least a hundred lines.
The file type given by the command file : file.jpg: ASCII text, with very long lines
I tried some of the common tools to identify any patterns or hidden data like exiftools, strings, xxd but I found nothing.
If you have any idea on what to do it would be very much appreciated.
If it's a challenge of CTF, there are some common way to find out flag.
First try to find flag in file metadata, like description of file field
you can also try tool: stegsolve.jar.
In more advance sence, stego info hidden with some math calulation, give this tool a try: zsteg
Perhaps I'm misunderstanding the problem here, but if your file actually starts with a backslash character followed by the characters x, f, f, \, x, d, 8 and so on, then what you're looking at is the binary content of a JPG file that has been converted into ASCII text.
If so, you need to convert this back into binary data. For example, in Linux or MacOS, you could do this by entering the following on the command line:
echo -ne '\xff\xd8\xff\xe0\x00\x10\x4a\x46\x49\x46\x00\x01...etc...' > img.jpg
echo -ne '\x42\xb1\xc1\x15\x52\xd1\xf0\x24\x33\x62\x72\x82...etc...' >> img.jpg
(Note: > sends the results to a new file, and >> appends to the end of the file)
Or alternatively in Python:
with open("img.jpg","wb") as f:
f.write(b'\xff\xd8\xff\xe0\x00\x10\x4a\x46\x49\x46\x00\x01...etc...')
f.write(b'\x42\xb1\xc1\x15\x52\xd1\xf0\x24\x33\x62\x72\x82...etc...')
# and so on for all the other lines
Either way, you should end up with a file called img.jpg containing the image you're after.

PowerShell unable to remove space between text and String

In PowerShell I am trying to find a way to remove the text from an output of text and a String.
Write-Host 'File located at C:\'$Fileline.FilePath -
I get an output of
c:\ program files\path
The space between c:\ and "Program files" is what I want to remove. Do I have to convert the text to a string, and then output it as two strings and then remove the spaces?
This is happening because you are passing multiple strings to Write-Host, which it is then joining with spaces. This behaviour is somewhat unique to Write-Host.
You can meet your need by sending a single double quoted string to Write-Host, which you can then put your variable inside and it will be expanded. However because you are accessing a property of your variable, you need to wrap it in a sub-expression: $():
Write-Host "file located at C:\$($Fileline.FilePath) -"
Try using the PowerShell -f formatting operator:
Write-Host ("File located at C:\{0} -" -f $FileLine.FilePath)
There's good info on -f at SS64 and at TechNet

PowerShell - Remove certain string from multiple filenames

I have multiple .mp3 files in the catalogue and want to remove artist's name from their filenames.
All the files' names contain string
"Adhesive Wombat - "
followed by the song's title.
I want to remove (not replace) every occurence of that specific string in every filename in current folder.
Not to mention, I've searched for answer on this site and failed to find an elegant and satisfying solution.
This is my first encounter with PowerShell.
As you know what you have is really close.
Dir | ren -NewName { $_.Name -replace "Adhesive Wombat - ", "" }
You added a stipulation that "Adhesive Wombat" may or may not contain a space. -replace uses regular expressions so lets harness that and make that space optional using a ?. I am also going to show you the full cmdlet name as supposed to aliases so you are aware of what is happening behind the scenes. It's hard to see but I tried to highlight the ? in bold for it to "pop".
Get-ChildItem | Rename-Item -NewName {$_.Name -replace "Adhesive ?Wombat - ", "" }
So that will match both
"other text Adhesive Wombat - other text "
"other text AdhesiveWombat - other text "
Also, depending on how much white space there is you could use \s+ where you have space which will match all consecutive white space.
From the above answers, the quick working example for simply copy&paste, that worked on win 10 Powershell:
change directory to your target directory
define files with e.g. *.txt or set all files with *.*
insert your Strings for replacement
cd C:\....
Get-ChildItem *.* | Rename-Item -NewName {$_.Name -replace "StringtoReplace", "newString"}

Searching for specific string in a file

I need to find this pattern, "LogEntry=", across multiple lines in the following file:
C:\test.conf
And change the line to read: "LogEntry=&MAC=" instead.
The script works fine so far to do that, but if someone runs the script twice it will re-add the same pattern, doubling it up.
I need to find a way to put a check in place to know if the file already has that pattern in it. Could someone please give me a hand with this one ?
Use negative lookahead in your regex e.g.:
'LogEntry=(?!&MAC=)'
That regex will not match the already modified line. Read more about look ahead/behind zero length assertions.
BTW if you have the PowerShell Community Extensions, you can do this edit operatio with a single command:
Edit-File C:\test.conf -Pattern 'LogEntry=(?!&MAC=)' -Replace 'LogEntry=&MAC='
And if you can't use PSCX's Edit-File, here is roughly the equivalent:
$content = Get-Content C:\test.temp
$content | Foreach {$_ -replace 'LogEntry=(?!&MAC=)','LogEntry=&MAC='} | Out-File -Encoding ASCII
I don't know what the file encoding is for your file. You need to know that and use the appropriate encoding on the Out-File command. If you don't specify the encoding, it defaults to Unicode.

How to split comma-separated words?

How can i split my words in new line (i have a lot of them) currently separated with comma,
Example of my file contains words in a single line:
Viktor, Vajt, Adios, Test, Line, Word1, Word2, etc...
The the output file should look like:
Viktor
Vajt
Adios
Test
...
If you are using NotePad++, this can easily be done. See image below
If you – for some reason – want to stick with the doc Format (to keep formatting, etc.) you could use LibreOffice (http://de.libreoffice.org/) to do the following replacement:
I agree installing LibreOffice just for this replacement would be overkill though.
Not sure what language you are in but you could use an explode/split function which would create an array of values split at ','. Then you could loop through the array and append the new line special character "\n". You would wind up with something like:
$fileContentsAsString; //read file into string variable
$valuesArray = explode(',' $fileContentsAsString);
$outputString;
foreach($valuesArray as $item){
$outputString .= $item . "\n";
}
For a quick text edititng i'm using online tool (http://regexptool.org/). Also you can do it step by step (screen).

Resources