I have a file whose contents are something like:
.................
..............
ACTIVATION CODE=<number>
.....................
........................
echo Activation Code: $ACTIVATIONCODE
....................................
............................................
...............................
java -cp $CPATH com.example.GenericApp.EDFGenericApp $ACTIVATIONCODE "$SCANDETAIL1NAME:$SCANDETAIL1VALUE"
I want to replace the "<number>" in the line "ACTIVATION CODE=<number>" using sed. The <number> changes with different files.
sed -re 's/^(ACTIVATION CODE=)[0-9]+$/\1the_new_number/' /path/to/the/file
Thanks
Can You explain the sysntax used.
Absolutely,
Basic:
s/^(ACTIVATION CODE=)[0-9]+$/\1the_new_number/
<----- replace this -----> <- with this -->
Advanced match:
^(ACTIVATION CODE=)[0-9]+$
|\________________/\____/|
| | | +-- End of the line
| | +-- One or more digits (the plus sign require the -r switch)
| +-- Please remember the part between parenthesis (require the -r swith)
+-- Beginning of the line
Warning. Because we specify the begining and end of the line, their can't be anything else than what is mentioned here. I mean, the following line will NOT match the pattern for two reasons: space at the beginning and comment at the end.
ACTIVATION CODE=345 # Example
Advanced replace:
\1the_new_number
\/\____________/
| |
| +-- and write the new number
+-- recall the part between parenthesis
Hope this helps.
if the entry is like:
ACTIVATION CODE=<qW87-hg45q>
i.e <the activation key> can contain alphanumeric characters and a "-" then what should the sed command be?
Kindly help
You mean there is an actual < and > to delimit the code?
And the key can contain lower case, upper case, digits and dashes.
s/^(ACTIVATION CODE=)<[[:alnum:]-]+>$/\1the_new_number/
No there is no < and > to delimit the code.
The line of concern is:
ACTIVATIONCODE=3c8de317-36b2
But the activation code(here 3c8de317-36b2) can be alphanumeric with a single "-"(hyphen) inserted anywhere inside.
---------- Post updated at 03:53 AM ---------- Previous update was at 03:46 AM ----------
A few examples of activation code:
1234-djfkldl
asdDF-45GHrf1
ANJ-56Hjkl
etc
So just remove the <> from my code. If you really want to specify there only one hyphen, then consider the following command:
sed -re 's/^(ACTIVATION CODE=)[[:alnum:]]+-[[:alnum:]]+$/\1the_new_number/' /path/to/the/file
Is the search and replace by sed permanent or that i need to redirect to another file
---------- Post updated at 04:13 AM ---------- Previous update was at 04:08 AM ----------
I have a variable called VAR which contains the new activation code. I want to replace the existing activation code with that of the variable VAR
The following command will permanently change the original file:
sed -ri 's/^(ACTIVATION CODE=)[[:alnum:]]+-[[:alnum:]]+$/\1'"$VAR/" /path/to/the/file
The following command will backup the original file (with extension .bak) and then permanently change it:
sed -ri.bak 's/^(ACTIVATION CODE=)[[:alnum:]]+-[[:alnum:]]+$/\1'"$VAR/" /path/to/the/file
The following command will not modify the original file but rather create a new modified file:
sed -r 's/^(ACTIVATION CODE=)[[:alnum:]]+-[[:alnum:]]+$/\1'"$VAR/" /path/to/the/file > /path/to/the/new/modified/file
---------- Post updated at 14:02 ---------- Previous update was at 12:32 ----------
Hi proactiveaditya,
Instead of creating new posts, it would be polite to at least tell if the given commands worked or not.
Regards
Santiago
Thanks Santiago
It worked
Another approach:
awk -F= -v var=$VAR '/^ACTIVATION CODE/{$2=var}1' file > newfile