Change the Particualy string value without string comparison in a file using Command line Ubuntu - shell

I want to create a shell script to change the string values which is after '=' in my file Using command line.
File is like:
String name = "Max";
String age = "24";
String address = "Noida";
Or
String name=Max
String age=24
String address=Noida
But here, I don't wanna string comparison, Like this:
$ sed -i 's/Max/Aman/gI' String.txt
$ sed -i 's/24/25/gI' String.txt
$ sed -i 's/Noida/Delhi/gI' String.txt
Please suggest how to change the string values without string comparison in a file using command line.

You may use this single sed that doesn't check previous value while replacing with new ones:
sed '/name = /s/"[^"]*"/"AMAN"/; /age = /s/"[^"]*"/"25"/; /address = /s/"[^"]*"/"Delhi"/;' String.txt
String name = "AMAN";
String age = "25";
String address = "Delhi";

Related

How to find string from txt file and store in variable in BASH

Requirement is to find a string from txt file and store it to variable.
file look like this(rsa.txt)
Encrypting String
... Input string : Test_123
... Encrypted string : $ENC(JEVOQyhZbVpkQmM0L3ArT2c4M05TZks5TmxRPT1+KQ==)
Required output (variable name : encstring):
encstring = $ENC(JEVOQyhZbVpkQmM0L3ArT2c4M05TZks5TmxRPT1+KQ==)
I tried below code but showing no result
encstring=$(grep -oE '$ENC[^()]*==)' <<< rsa.txt)
With awk, could you please try following. Simply, search for string /Encrypted string along with a condition to check if last field of that line has $ENC in it then last field for that line by using $NF.
encstring=$(awk '/Encrypted string/ && $NF~/\$ENC/{print $NF}'
You can use
encstring=$(sed -n 's/.*\(\$ENC(.*)\).*/\1/p' rsa.txt)
# OR
encstring=$(grep -oP '\$ENC\(.*?\)' rsa.txt)
See an online demo:
s='Encrypting String
... Input string : Test_123
... Encrypted string : $ENC(JEVOQyhZbVpkQmM0L3ArT2c4M05TZks5TmxRPT1+KQ==)'
encstring=$(sed -n 's/.*\(\$ENC(.*)\).*/\1/p' <<< "$s")
echo "$encstring"
# => $ENC(JEVOQyhZbVpkQmM0L3ArT2c4M05TZks5TmxRPT1+KQ==)
The sed -n 's/.*\(\$ENC(.*)\).*/\1/p' command does the following:
-n suppresses the default line output
s/.*\(\$ENC(.*)\).*/\1/ - finds any text, then captures $ENC(...) into Group 1 and then matches the rest of the string, and replaces the match with the Group 1 value
p - prints the result of the substitution.
The grep -oP '\$ENC\(.*?\)' command extracts all $ENC(...) matches, with any chars, as few as possible, between ( and ).
You are searching for ENC which is followed by 0 or more occurances of something which is not an open or closed parenthesis. However, in your input file, there is an open parenthese after ENC. Therefore [^()]* matches the null string. After this you expect the string ==). This would match only for the input ENC==)`.
You need to escape $ as \$ as it means "end of string" with -E

How to remove a string from file in mac terminal

I have a file with million records and each line ends with SYSTEM;\N.
I want to delete all occurrences of ;\N from file. How can I approach this?
You can use the sed command to replace all the occurrences of the ';\N' from the file and replace it with ''.
sed -i 's/original/new/g' file.txt
Explanation:
sed = Stream EDitor
-i = in-place (i.e. save back to the original file)
The command string:
s = the substitute command
original = a regular expression describing the word to replace (or just the word itself)
new = the text to replace it with
g = global (i.e. replace all and not just the first occurrence)
file.txt = the file name
This worked finally sed -i '' 's/;\\N//g' test112.csv

sed command replacing string that contains special character is not working

i want to replace a special string inside a file.txt. my strings are like this :
-> Old String
tech=/lsf/dfg/a.v,/ldf/fgh/b.v
-> New String
tech=$var
i have tried following
sed -i 's/tech=/lsf/dfg/a.v,/ldf/fgh/b.v/tech=$var/g' file.txt
it doesnt work.
sed -i 's#tech=/lsf/dfg/a.v,/ldf/fgh/b.v#tech=$var#g' file.txt
Just replace the delimiters '/' for the sed expression with '#' (or another character that is not in the string you are trying to match and replace).

how to use bash to find text string like--> "PROVISIONING_PROFILE[sdk=iphoneos*]" <--this string has many special characters

As the title says, i'd like to find a string in a text, but the string is special.
The text may like this:
{
PREBINDING = NO;
"PROVISIONING_PROFILE[sdk=iphoneos*]" = "22B333C5-6D7A-4A76-81F2-12A045DAE44C";
SDKROOT = iphoneos;
TARGETED_DEVICE_FAMILY = 2;
}
( Additionally, if you are a ios developer, you may know that the text example is from XXX.xcodeproj/project.pbxproj )
I need to find and delete this line of text:
"PROVISIONING_PROFILE[sdk=iphoneos*]" = "22B333C5-6D7A-4A76-81F2-12A045DAE44C";
In this line, it has quotes("), has brackets([ ]), has star(*), also has space( ). When i try to find this line using code below:
provisionStr="22B333C5-6D7A-4A76-81F2-12A045DAE44C"
fileDir=project.pbxproj
totalLineStr=`grep $provisionStr $fileDir`
sed -i "" "s#$provisionStr##" $fileDir
This bash code does not work. Please help me, i'm just beginning to learn to use bash.
sed substitute with nothing:
s(for substitute)/string_to_match/new_string_or_empty_to_remove/g(for global/all document)
sed 's/\"PROVISIONING_PROFILE\[sdk=iphoneos\*\]" = \"22B333C5-6D7A-4A76-81F2-12A045DAE44C\";//g'
escape double quotes, asterix and brackets using \
It sounds like all you want to do is:
grep -v '"22B333C5-6D7A-4A76-81F2-12A045DAE44C"' file
If you ever did want to search for a string in a file instead of search for matches to an RE in a file, then you'd just do this, for example:
$ awk -v str='"PROVISIONING_PROFILE[sdk=iphoneos*]"' 'index($0,str)' file
"PROVISIONING_PROFILE[sdk=iphoneos*]" = "22B333C5-6D7A-4A76-81F2-12A045DAE44C";
$ awk -v str='"PROVISIONING_PROFILE[sdk=iphoneos*]"' '!index($0,str)' file
{
PREBINDING = NO;
SDKROOT = iphoneos;
TARGETED_DEVICE_FAMILY = 2;
}
Do not try escaping all the RE metacharacters and then do an RE comparison as that makes no sense. If you want to look for an RE then do an RE comparison but if you want to look for a string do a string comparison, it's that simple.

Trying to get sed to replace a filepath in a file with another filepath

This is my current code. I am trying to replace a filename string in the file with another filename. But I am currently getting the error
"sed: 1: "s/directory = "[A-Za-z0 ...": bad flag in substitute command: 'U'"
What is wrong with this code?
function restart_existing ()
{
old="directory = \"[A-Za-z0-9\/]\""
new="directory = \"$1\""
sed -i '' "s/$old/$new/" "$HOME/angelpretendconfig"
}
restart_existing "$HOME/blahblahblah/shoot/blah"
EDIT:
Thanks! I've adopted your advice, and adapted the code.
function restart_existing ()
{
old="directory = \"*\""
printf -v new 'directory = "%s"' "$1"
sed -i '' "s;$old;$new;" "$HOME/angelpretendconfig"
}
restart_existing "Query"
But now the line in question goes from
directory = "/home/jamie/bump/server"
directory = "Query"/home/jamie/bump/server"
Why does this occur?
Don't use forward slashes in sed when what you're replacing contains forward slashes:
$ sed 's;foo/bar;baz/wuz;' <<< "where is the foo/bar?"
where is the baz/wuz?
Also, sometimes it's more readable to avoid manually escaping quotes:
function restart_existing ()
{
old='directory = "[A-Za-z0-9\/]"'
printf -v new 'directory = "%s"' "$1"
sed -i '' "s;$old;$new;" "$HOME/angelpretendconfig"
}
restart_existing "$HOME/blahblahblah/shoot/blah"
$1 contains something, which is parsed as a special command to sed, in this case probably a / which marks the end of the replacement string, followed by some other characters.
You have to escape the replacement string first: Escape a string for a sed replace pattern.

Resources