Sed change not known string in one line leaving the rest untouched - linux

I'm facing problem with using sed in my project. I have a file myfile.txt with content inside like this:
{"Argument_date": "2020-04-16", "Argument_post": "true", "Argument_like": "false"}
I need to change the date argument with actual date not touching the rest of the line - but this date can be any not just 2020-04-16 - so this is unknown string
So i did this:
sed 's/\"Argument_date\"\: \".*\"/\"Argument_date\"\: \"'"$(date +%Y-%m-%d)"'\"/g' myfile.txt
But I get output almost correct:
{"Argument_date": "2020-05-05"}
Sed is changing this unknown date correctly to new one from system date, but miss all the rest of the line.
Can someone please explain me what I'm doing wrong to get correct output like this:
{"Argument_date": "2020-05-05", "Argument_post": "true", "Argument_like": "false"}

Use jq to manipulate json:
jq --arg date "$(date +%Y-%m-%d)" '.Argument_date = $date'

Problem is with your regex, it matches the whole string and then replaces it with "Argument_date": "2020-05-05"
Try changing your regex to match only the required part. One solution could be:
sed -E 's/(.*?)"Argument_date":\s+"[0-9]+-[0-9]+-[0-9]+"/\1"Argument_date":"'"$(date +%Y-%m-%d)"'"/g' file1

Related

Using 'sed' to replace a string

I have a file that contains a URL and I wish to replace this URL with a new one.
This URL can be different each time so I do not wish to replace XXX with YYY but to change the value of a variable which contains the URL.
File looks like this:
APP_URL=https://test.hello.co/
I wish to change the value of APP_URL to a different URL but without success.
I am using a bash script in order to make this work.
tried using this inside my script and it didn't work.
oldline='APP_URL=https://test.hello.co'
newline='APP_URL=https://${variable}'
sudo sed -i 's/${oldline}/${newline}/g' .env
I would love to get help here!
Thank you :)
You need to remove the ' in sed and escape all / so that it doesn't think you are ending the part of s (or use other sign)
Both these work (my filename is .test):
variable=aaaaaaaaaaaaaaaaaaaaaaaaa
oldline='APP_URL=\S+'
newline='APP_URL=https:\/\/'$variable
sed -r s/${oldline}/${newline}/g .test
oldline='APP_URL=\S+'
newline='APP_URL=https://'$variable
sed -r s-${oldline}-${newline}-g .test
Single quotes prevent variable interpolation.
You can usually use double quotes instead.
$: cat tst
oldurl=test.hello.co
newurl=foo.bar.com
sed "s,APP_URL=https://${oldurl}/,APP_URL=https://${newurl}/,g" file
$: cat file
APP_URL=https://test.hello.co/
$: ./tst
APP_URL=https://foo.bar.com/
edit
Yes, this can be simplified a lot, such as by passing in the new url.
$: grep . tst file
tst:sed -E "s,APP_URL=https://[^/]+/,APP_URL=https://$1/,g" file
file:APP_URL=https://test.hello.co/
$: ./tst a.b.c
APP_URL=https://a.b.c/

Replace String using Sed?

I am new to Unix and Linux and am trying to replace a certain strings in a file using sed.
This is what I have so far:
Param1=CHANGEME
Param2=Value2
Param3=CHANGEME
Param4=Value4
If I do this command :- sed -i 's/CHANGEME/VALUE/g' input.txt, "CHANGEME" will be replaced by the text "VALUE" which is fine.
What if I want to change "Value" with it's corresponding value number so essentially for Param1=Value1 and for Param3=Value3 ?
How can I achieve that? Thank you
Using GNU sed ...
Your snippet saved to hugger:
cat hugger
Param1=CHANGEME
Param2=value2
Param3=CHANGEME
Param4=value
The following sed command does what you want, I think:
sed -r '/CHANGEME/s/Param([0-9]+)=.*/Param\1=Value\1/' hugger
Param1=Value1
Param2=value2
Param3=Value3
Param4=value4

Sed replace date

So, I have looked over other questions and nothing specific seems like it can help me. I have a file that has a date variable set like mm/dd/yyyy that I would like to replace the date with.
For example:
version.js
//Application Version Information
app_date="7/07/2015";
...
And I would like to use sed to replace it. I actually have a shell script that replaces many things in this file, and all work except for the date. Currently what I'm trying is:
sed -i -e 's:app_date="[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]":app_date="$(date %m/%d/%Y)":g'
But this isn't getting me the results I want. I've also tried:
sed -ibak 's/app_date=\"[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9][0-9][0-9]\"/app_date=\$(date +%m/%d/$Y)\"/g'
Neither seem to work.
Any ideas?
Edited to add:
The solution I've successfully employed is Sean Bright's with double quotes:
sed -i -e "s:app_date=".*";:app_date="$(date +%m/%d/%Y)";:g" version.js
This works perfectly for my needs.
I bet your script would work if the date you were matching was October 10th...
sed -i -e 's:app_date="[0-9][0-9]*/[0-9][0-9]*/[0-9][0-9][0-9][0-9]":app_date="$(date %m/%d/%Y)":g'
Add the *s after your day and month expressions as above. [0-9][0-9] will not match 7.
Now that I think about it, because you don't care about the date that is already in the file, most of this is unnecessary, you can simply do:
sed -i -e 's:app_date=".*";:app_date="'$(date +%m/%d/%Y)'";:g' version.js
You're using the wrong shell quotes. the command substitution $() will not be expanded inside single quotes.
sed -i "/^app_date=/capp_date=\"$(date +%m/%d/%Y)\";" version.js # GNU only
Another approach, using a file editor instead of a stream editor:
ed -s version.js << EOF
/^app_date=/c
app_date="$(date +%m/%d/%Y)";
.
w
EOF
For replacing date with anything in Linux
n=`date +%d`
cal>temp
If [$n -lt 10]
//10 is just a predction for condition
then
Sed s/"$n"/*/g temp
else
Sed s/"$n"/**/g temp
fi
// s is text substituting option
//* is anything that replaces the date
// g is global replacement

sed does not replace the string correctly when using a variable

Currently I have a file that has a unique line with the pattern
alphanumeric_ChangeMe_moreAlphaNumeric
Actual looks like this:
127.0.0.1 local.com localhost HostType_test_HostNumber2
I'm trying to replace the string test with a variable determined by another command, run as another user using the following code.
site=$(su admin -c get_local_site | less | sed 's/Local Site Name: //')
sed -i -e "s/RecoverPoint_[[:alnum:]]*_RPA/RecoverPoint_$site_RPA/" fakehostfile
I've tested the individual codes and they echo the correct values, but when I try to use the $site variable in the second it fails to replace the section.
I can't seem to find the correct syntax to replace just what's between the underscores with a string (containing only alphanumerics) that's stored in a variable
I've already been looking on here, and found some similar problems, however the solutions don't seem to work, since part of the replacement string is a variable. I've tried to concatenate the string as three separate variables, but it replaces things strange (Maybe due to the underscores?)
What am I missing here??
Questions with similar problems that didn't work:
sed variable replacement does not seem to work
Sed replacement not working when using variables
As others recommended, no less command required, and if you need show the line of Local Site Name only, use -n option in Sed.
Second, put varies in braces, it should fix your problem.
site=$(su admin -c get_local_site | sed -n 's/Local Site Name: //p')
sed -i "s/RecoverPoint_[[:alnum:]]*_RPA/RecoverPoint_${site}_RPA/" fakehostfile

how to extract the data using sed command

I have the data this
&mac=1E-30-6C-A2-47-5F&ip=172.16.1.127&msk=255.255.255.0&gw=172.16.1.1&pdns=0.0.0.0&sdns=0.0.0.0&Speed=0&PortNo=10001&PerMatFram=0&ComPort=0
I want to extract the data string and store it in a variable using sed commond like
ip=172.16.1.127
mac=xyz
How to use sed with the above string?
I have tried using like this
IP=`echo "$QUERY_STRING" | sed -n '/&ip=/,/&)/g'
but it is not giving any data.
Probably easier to do it in two steps, first trimming the left side, and then the right side.
sed 's/.*mac=//' | sed 's/\&.*//'
This will:
Step 1:
Replace anything up until (and including) "mac=" with nothing
Step 2:
Replace anything after (and including) the first ampersand (&) it encounters with nothing.
If you data do not contain any special characters, you can use the following:
eval $( echo "$QUERY_STRING" | sed 's/&/ /g' )
That would create variables directly from the query string as mac=1E-30-6C-A2-47-5F etc. Be careful, though, as an attacker might request a page with the following query string:
&IFS=.&PWD=/&UID=0
See also How to parse $QUERY_STRING from a bash CGI script.

Resources