Unique words in a text file [duplicate] - text-files

This question already has answers here:
Parsing unique words from a text file
(2 answers)
Closed 6 years ago.
I was wondering if there is a way to find (and display) all the unique words (words that appear once) in a text file? Could this be done just using the command line? Or would I have to use something like a python script?

If you don't want to write an application then the easiest way that I can think to accomplish this is to use powershell. See this:
https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.utility/get-unique
The example that Microsoft provides populates a variable with the list of unique words:
$A = $(foreach ($line in Get-Content C:\Test1\File1.txt) {$line.tolower().split(" ")}) | sort | Get-Unique
You may wish you use additional delimiters though to split on punctuation such as this:
$A = $(foreach ($line in Get-Content C:\test.txt) {$line.tolower().split(" .,?!;:")}) | sort | Get-Unique
Place this in a file with the extension .ps1 and you can run it from the command line. In order to get the values out of the variable just a second line with the variable to echo the result to the screen:
$A
To get the count of items in the array you could do this:
$A.count

Related

grep output show result with file name but not full path [duplicate]

This question already has answers here:
How can I use grep to show just filenames on Linux? [closed]
(3 answers)
Closed 5 months ago.
I use grep (egrep) to search text a log. I have multiple txt files to search in the same folder. By default, grep output file name (full path), with math result.
The directory path is long so it looks ugly.
However, if I use -h option, it only give me match result but not file name, which is not good either, because sometimes I need to know which file it is from.
for example, in folder bashsearch I have file1, file2, file3...
The default output is like this:
/mnt/c/Users/chili/Desktop/.../bashsearch/file1:match result 1
/mnt/c/Users/chili/Desktop/.../bashsearch/file2:match result 2
this looks ugly.
If I use -h, the output is like this:
match result 1
match result 2
this is not good either.
What I want is like this:
file1:match result 1
file2:match result 2
How to achieve this please? Any help is highly appreciated.
You can reformat output by using pipe to another command. For example
| grep -o "[^/]*:.*"
will remove the path part.

Replace underscore with hyphen while writing script in Jenkins [duplicate]

This question already has answers here:
How to pass the value of a variable to the standard input of a command?
(9 answers)
Closed 1 year ago.
I'm writing pipeline in Jenkins. My code looks something like below:
void someFun(){
sh '''
VAR='a_b_c_d'
TEMPVAR=$VAR | tr '_' '-'
echo "With hyphens $TEMPVAR-blah-blah"
echo "With underscores $VAR"
'''
}
stage{
someFun()
}
All I want to achieve is a way to replace underscores from 1st variable and use its value in 2nd variable. Also. I'm not intending to mutate VAR. And I want to store the value, not just print it.
When I'm using this above approach, I'm getting TEMPVAR empty.
What I'm trying to possible to achieve is possible? If yes, what is the way to achieve it?
I read multiple posts but couldn’t find any helpful:(
You can do it in many ways, like with:
tr, but in this case you need to use an additional shell:
TEMPVAR="$(echo "$VAR" | tr _ -)"
or even better with string substitution:
TEMPVAR="${VAR//_/-}"

Update part of text in document [duplicate]

This question already has answers here:
How can you find and replace text in a file using the Windows command-line environment?
(30 answers)
Closed 6 years ago.
I have a .xal document which can be opened with notepad just fine. Below is the part of it that is important to me:
...
magV=1
invert=0
text=01000000237843141114052693183714
[Pri1]
enabled=0
...
I would like to create a script, which would auto replace part of text 140526 in line 128 (it is some old date - 26.05.2014) and replace it with the current date.
For example: today it should be 160816, tommorrow there should be 160817, etc.
As said before text is in line 128, characters are from 24 to 30.
If searched around (SED for WIN, VBA, Powershell,...) but i didn't find anything that could work.
Thank you all in advance!
works fine if you do a windows batch file and using sed
Basically, use set and built-in DATE environment variable to extract year, month, day as a 6-digit string (DATE contains yy/mm/dd, slashes need to be removed)
Then apply sed on line 128 only, creating one group, replacing the group + the 6 next chars by the same group + the new date.
The -i flag allows to perform in-place replacement so input file is updated instead of outputing the result in standard output.
set D=%DATE%
set D=%D:~0,2%%D:~3,2%%D:~6,2%
echo %D%
sed -i "128s/\(text=..................\)....../\1%D%/" input.txt
Here a solution using PowerShell:
$content = Get-Content 'your_file_path'
$content[127] = $content[127] -replace '(.{24}).{6}', ('${{1}}{0}' -f (Get-Date).ToString('yyMMdd'))
$content | Set-Content 'your_file_path'

Call script on all file names starting with string in folder bash

I have a set of files I want to perform an action on in a folder that i'm hoping to write a scipt for. Each file starts with mazeFilex where x can vary from any number , is there a quick and easy way to perform an action on each file? e.g. I will be doing
cat mazeFile0.txt | ./maze_ppm 5 | convert - maze0.jpg
how can I select each file knowing the file will always start with mazeFile?
for fname in mazeFile*
do
base=${fname%.txt}
base=${base#mazeFile}
./maze_ppm 5 <"$fname" | convert - "maze${base}.jpg"
done
Notes
for fname in mazeFile*; do
This codes starts the loop. Written this way, it is safe for all filenames, whether they have spaces, tabs or whatever in their names.
base=${fname%.txt}; base=${base#mazeFile}
This removes the mazeFile prefix and .txt suffix to just leave the base name that we will use for the output file.
./maze_ppm 5 <"$fname" | convert - "maze${base}.jpg"
The output filename is constructed using base. Note also that cat was unnecessary and has been removed here.
for i in mazeFile*.txt ; do ./maze_ppm 5 <$i | convert - `basename maze${i:8} .txt`.jpg ; done
You can use a for loop to run through all the filenames.
#!/bin/bash
for fn in mazeFile*; do
echo "the next file is $fn"
# do something with file $fn
done
See answer here as well: Bash foreach loop
I see you want a backreference to the number in the mazeFile. Thus I recommend John1024's answer.
Edit: removes the unnecessary ls command, per #guido 's comment.

Read in two text file lists and write out 1 line from to a text file

I need to create a powershell script that reads in two text files with lists.
The first list is used to identify the remote computers that will have a file appended to it.
The second list is the key phrase that needs to be appended to the file.
List 1:
Computer1
Computer2
Computer3
List 2:
ABC
DEF
GHI
Script would loop through each pointing to a file say C:\temp\help.txt on each of the remote computers and write 1 line from List 2.
So Example: When the script runs it goes out to List 1, finds that computer 1 is first and opens the file \computer1\C$\temp\help.txt, it would then grab the first line from List 2 and write to the file ABC. Close the file and go on to Computer 2. Computer 2 would be \computer2\C$\temp\help.txt and would grab the 2nd item from List 2 and write to it DEF save and move on.
It's been hard to find any help reading in and looping through 2 lists. Or perhaps I am thinking of it wrong. I have gotten to Get-Content to read in the file and foreach($x for text1) can go through 1 of the text files loops but can not figure out how to loop through the 2nd text file.
Processing side-by-side arrays is always a pain, and tends to be error prone. Ideally, as suggested, the computer names and strings would be together in a CSV or something.
But as is, something like this should get you on the right track. You might have to fiddle with newlines at the beginning/end of the strings a bit.
$machines = Get-Content .\MachineList.txt
$strings = Get-Content .\StringsList.txt
if($machines.Count -ne $strings.Count){ throw 'Counts do not match' }
for($i = 0; $i -lt $strings.Count; $i++)
{
$path = "\\$($machines[$i])\C`$\temp\help.txt" # unc path
$strings[$i] | Add-Content $path
}
If you have it in a CSV like
Config.csv
---------
ComputerName,String
machine1,string1
machine2,string2
machine3,string3
Then you could simplify to this:
Import-Csv .\Config.csv |%{
$_.String | Add-Content "\\$($_.ComputerName)\C`$\temp\help.txt"
}

Resources