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
Related
I have to go through 2 files stored as variables and delete the lines which contain a string stored in another variable:
file1="./file1"
file2="./file2"
text="searched text"
for i in $file1,$file2; do
sed -i.txt '/$text/d' $i
done
The files to exist in the same folder as the script.
I get "No such file or directory". I have been stuck for the past 3 hours on this and honestly I'm pretty much about to quit Linux.
You have a several issues in your script. The right way to do is:
file1="./file1"
file2="./file2"
text="searched text"
for i in "$file1" "$file2"; do
sed -i.txt "/$text/d" "$i"
done
Issues:
for expects a space delimited list of arguments, not comma separated
it is important to enclose your variable expansions in double quotes to prevent word splitting
you need double quotes to enclose the sed expression since single quotes won't expand the variable inside
You could catch these issues through shellcheck and debug mode (bash -x script) as suggested by Charles.
Sorry to say that your shell script is not nicely design. In a shell scripts multi files should not be stored in multiple variables. Suppose you need to do the same operation on 100 different files what will you do? So follow the below style of code. Put all your file names in a file for example filelist.dat now see:-
First put all the file names in filelist.dat and save it
text="searched text"
while read file; do
sed -i.txt '/$text/d' $i
done < filelist.dat
Also not sure whether sed command will work like that. If not working make it like below:-
sed -i.txt 's|'"$text"'|d' $i
Writing a small script in bash (MacOS in fact) and I want to use find, with multiple sources. Not normally a problem, but the list of source directories to search is held as a string in a variable. Again, not normally a problem, but some of them contain spaces in their name.
I can construct the full command string and if entered directly at the command prompt (copy and paste in fact) it works as required and expected. But when I try and run it within the script, it flunks out on the spaces in the name and I have been unable to get around this.
I cannot quote the entire source string as that is then just seen as one single item which of course does not exist. I escape each space with a backslash within the string held in the variable and it is simply lost. If I use double backslash, they both remain in place and again it fails. Any method of quoting I have tried is basically ignored, the quotes are seen as normal characters and splitting is done at each space.
I have so far only been able to use eval on the whole command string to get it to work but I felt there ought to be a better solution than this.
Ironically, if I use AppleScript I CAN create a suitable command string and run it perfectly with doShellScript (ok, that's using JXA, but it's the same with actual AppleScript). However, I have so far been unable to find the correct escape mechanism just in a bash script, without resorting to eval.
Anyone suggest a solution to this?
If possible, don't store all paths in one string. An array is safer and more convenient:
paths=("first path" "second path" "and so on")
find "${paths[#]}"
The find command will expand to
find "first path" "second path" "and so on"
If you have to use the string and don't want to use eval, split the string into an array:
string="first\ path second\ path and\ so\ on"
read -a paths <<< "$string"
find "${paths[#]}"
Paths inside string should use \ to escape spaces; wraping paths inside"" or '' will not work. eval might be the better option here.
I know there is Alt + Enter combination to extract single String to string resource. But I am wondering is there anything that I can extract all string of my project into string resource?
Also Android studio not making same String to string resource if I make one of them.
String s = "Hello World";
String s2 = "Hello World";
For example. I make "Hello World" to string resource still another remain Hardcoded in same file as well in the project too.
String s = getString(R.string.helloworld);
String s2 = "Hello World";
If anyone know something like that.
As your requirements and as I know there is no such feature in android studio you were really searching for, But here are some alternative ways that can help you.
Go to "Analyze > Run Inspection ..", and type "Hardcoded strings". Run that in your whole project, and you will get an inspection results panel that will show all the hardcoded text of projects. Then hit Alt + Enter and you'll get an option to automatically extract that Strings.
Another approach is to Find and Replace But It's not better because of time consumption. To simplify the approach you can have a look at here for flexibility.
For hardcoded strings in XML layout files:
Click on Analyze -> Run inspection by name -> enter Hardcoded strings -> select the whole project -> OK.
Here, you will get all the hardcoded strings from your xml layout files only that u can extract to strings.xml by:
Click on the hardcoded text -> Alt + Enter -> extract from resource -> enter your corresponding resource name for that string -> OK
For hardcoded strings in Java class code:
Click on Analyze -> Run inspection by name -> enter Hardcoded texts -> select the whole project -> OK.
Here, you will get all the hardcoded strings from your Java class files only that u can extract to strings.xml by:
Click on the hardcoded text -> Alt + Enter -> extract from resource -> enter your corresponding resource name for that string -> OK -> add getString for each string resource created in your java code.
Hence, Hardcoded strings -> Java class code whereas
Hardcoded texts -> Xml layout files in Android Studio.
In Android 3.2.1 you can go to Edit->Find->Find In Path... and then search for android:text=" . This will give you a list of all the hardcoded strings in xml files.
Search for Toast.makeText to find all the toast items from the java files.
Search for setText(" to search for text sets in java files and so forth.
You can do searches like this for items you would like to find throughout the project and replace.
I wrote the following script to extract all hardcoded strings from an activity.xml file and add them to strings.xml. (Bash/Linux):
Usage: extractAll.sh activity_main.xml strings.xml
Original files are backed up before changes are made.
#!/bin/bash
################################################################################
# Extract All Hardcoded Strings From an Activity XML File and Save New
# Versions of the activity.xml and strings.xml
################################################################################
#check the number of arguments supplied, print usage if not 2
if [ "$#" -ne 2 ]; then
echo "extract all hardcoded strings from activity.xml and update .xml files"
echo "original files are saved with .bak extension"
echo "usage: $0 activity.xml strings.xml"
exit
fi
#backup input files
#TODO: save these backups in another folder so they don't cause build error
cp $1 $1.bak
cp $2 $2.bak
#grep for hardcoded strings, for each one sed out special characters, change
#all to lower case, replace space characters with underscores, truncate string
#variable names to 30 characters. The result will be the name of the new string
#variable entered in strings.xml
grep -Po "(?<=android:text=\")[^\"]+(?=\")" $1 | while read -r HARDSTRING ; do
STRINGVARNAME=`echo $HARDSTRING | sed 's/\([\d0-\d31]\|[\d33-\d35]\|\$\|[\d37-\d47]\|[\d58-\d64]\|[\d91-\d96]\|[\d123-\d126]\)//g'|sed -e 's/\(.*\)/\L\1/'|sed 's/ /_/g'|head -c 30`
#substitute each hardcoded string with the string variable name
sed -i "s/$HARDSTRING/#string\/$STRINGVARNAME/" $1
#get the number of lines in strings.xml file
NUMLINES=`wc -l < $2`
#insert string definition at second-to-last line of strings.xml
let "NUMLINES++" #I had to increment mine to get the desired result
#add an entry to the strings.xml defining the newly extracted string
sed -i "$NUMLINES""i\\ <string name=\""$STRINGVARNAME"\">$HARDSTRING</string>\\" $2
done
Final note: wc -l returned one less than expected in my test case using a strings.xml file created by Android Studio (I suspect because the last line had no newline). This caused sed -i to insert the new line at the third-to-last position instead of the desired second-to-last. My fix was to increment the result of wc -l using let.
References:
What are invalid characters in XML
sed one-liner to convert all uppercase to lowercase?
How to process each output line in a loop?
How can I truncate a line of text longer than a given length?
Linux Shell Script - String Comparison with wildcards
How to insert a string into second to last line of a file
Check number of arguments passed to a Bash script
https://askubuntu.com/questions/385528/how-to-increment-a-variable-in-bash
https://docs.oracle.com/cd/E82085_01/150/funtional_artifacts_guide/or-fasg-standards.htm
https://tldp.org/LDP/abs/html/refcards.html#AEN22828
I believe the shortcut you are looking for is (on a Mac) Alt + Command + C
Right now I'm working on creating a script in linux bash shell that adds the word "-BACKUP" to a file name between certain points. For example, if I had a file/string called file1.txt I would want to add the "-BACKUP" between "file1" and ".txt" to make "file1-BACKUP.txt". How would I go about doing that? Would I use the basename command anywhere? In this situation, the extension and stem could be anything, not just what I gave as an example. All help is appreciated!
Use the substring processing parameter expansion operator % to remove the suffix from the string, then append the new text. The variable must be enclosed in braces for substring processing parameter expansion to work.
var="file1.txt"
echo "${var%.txt}.BACKUP.txt"
I have a powershell script that will run a query and export the results to Excel. I want to hook this into SQL Studio Management Studio (2008)'s external tools. SSMS will not allow powershell scripts so I am using a batch file to get it started.
In the external tools section you can specify some predefined arguments. The argument that I want is called "current text". This argument passes whatever is highlighted to the tool (batch file). My batch file then passes that argument on to a powershell script.
The problem is that if the user has a query that spans multiple lines highlighted then the powershell script fails because of the linebreaks. It seems it would be relatively easy to strip them out, or better yet, replace them with a space?
Here is my batch file:
echo %~1
call powershell ^& 'c:\temp\ExportSQL.ps1' -query "%~1"
My question is how can I replace newlines and carriage returns from %1 with a space before passing it to the powershell script? TIA.
First, I can't believe that you have really linefeeds in your parameter %1, as it is possible, but a bit complex to achieve this.
To control this you could use
echo on
rem # %1 #
To handle CR and linefeed characters you have to use the delayed expansion, as percent expansion of such a content is not possible.
With percent expansion CR's are always removed, linefeeds removes the rest of the line or in a block context they append a new command line.
So it's not possible to handle content with linefeeds in a parameter like %1 ..%9.
You need it stored in a variable.
Assuming you have content with newlines in a variable, you could replace it with spaces.
The empty lines in this example are important for the result.
#echo off
set text=This contains a ^
newline
setlocal EnableDelayedExpansion
set ^"result=!text:^
= !"
echo Content of text is "!text!"
echo ----
echo Content of result is "!result!"
--- Output ---
Content of text is "This contains a
newline"
----
Content of result is "This contains a newline"