Replace Line of Text Through Script - linux

I'm playing around with a script that updates a file replacing a line of text with one that is stored in a variable. After trying a whole bunch of things, I'm guessing the issue is with the text inserted, it's probably using characters that sed doesn't like. An example of how I'm using the command, with what the replacement text looks like is below:
sed 's/^define.*$/'define('AUTH_KEY', 'r*v8]Wic;#Y4{|0EQ9Z?~W,-P}k:d{k)ylAFHm-d(tY6v?U,5{hn].e9eH%/Xmdy');'/' change.html
I've read somewhere else on here that you need to escape the characters but was unable to get it working with the answer I found here: Escape a string for a sed replace pattern
Any help, or a pointer in the right direction is greatly appreciated, thank you! :)

Be aware that sed can take any character as the separator, and that / is only conventional. If you can guarantee that your key is free of spaces, you could try:
sed 's ^define.*$ &(yourkey) '

Try something like this -
Assign your replacement text to a variable -
[jaypal:~/Temp] abc="'define('AUTH_KEY', 'r*v8]Wic;#Y4{|0EQ9Z?~W,-P}k:d{k)ylAFHm-d(tY6v?U,5{hn].e9eH%/Xmdy');'"
Use that variable in awk's sub function.
[jaypal:~/Temp] awk -v rep="$abc" '{sub(/^define.*$/,rep,$0); print}' change.html

Related

Using SED to replace capture group with regex pattern

I need some help with a sed command that I thought would help solve an issue I have. I have basically have long text files that look something like this:
>TRINITY_DN112253_co_g1_i2 Len=3873 path=[38000:0-183]
ACTCACGCCCACATAAT
The ACT text blocks continue on, and then there are more blocks of text that follow the same pattern, except the text after the > differs slightly by numbers. I want to replace only this header part (the part followed by the >) to everything up until the very last “_” the sed command I thought seemed logical is the following:
sed -i ‘s/>.*/TRINITY.*_/‘
However, sed is literally changing each header to TRINITY.*_ rather than capturing the block I thought it would. Any help is appreciated!
(Also.. just to make things clear, I thought that my sed command would convert the top header block into this:
>TRINITY_DN112253_co_g1_
ACTCACGCCCACATAAT
This might help:
sed '/^>/s/[^_]*$//' file
Output:
>TRINITY_DN112253_co_g1_
ACTCACGCCCACATAAT
See: The Stack Overflow Regular Expressions FAQ

I am trying to replace a text for example

Example:
"word" -nothing
To
word" - nothing
in gvim.
I tried
:%s/^.*\"/
But what I get is: -nothing
Well I am new to scripting so I would like to know if it can be done in any other way like using gvim or awk or sed.
In vim... Check for \(word + quote + space + hyphen\) as first reference, followed directly by another \(word\) as second reference... replace by first reference + space + second reference... Make sure the find/replace can happen multiple times on a line with g suffix.
:%s/\(\w" -\)\(\w\)/\1 \2/g
Note that I left out the leading quote... I suppose it is possible you might have spaces in the quoted text - and I think this form might be better for you. Now in sed, that is the really cool thing about the relationship between *nix tools - they all use similar (or the same) regular expressions pattern language. So, the same exact pattern as above can be done in sed (using : as delimiter for clarity).
sed 's:\(\w" -\)\(\w\):\1 \2:g'
Awk doesn't do back references; so, not to say it can't be done, but it is not so convenient.
Could you please try following and let me know if this helps you.
awk '{sub(/^"/,"");sub(/-/,"- ")} 1' Input_file
Solution 2nd: With sed.
sed 's/^"//;s/-/- /' Input_file
Since you also tagged grep: GNU grep has the -P switch for PCRE (Perl compatible reg ex) which has \K: Keep the stuff left of the \K, don't include it in $&, so:
$ echo \"word\" | grep -oP "\"\Kword\""
word"
If I understand your question correctly you want to replace first " in each line with empty string. So in sed it is just:
sed 's/"//'
Without g flag it will replace only first occurrence in each line.
EDIT:
The same way it will work in Vim (unless you have 'gdefault' option set), so in Vim you can:
:%s/"//
try this :
:%s/\"(.*)\"/\1\"/gc

Linux Sed command replace after special character

How can I use sed command in Linux to replace key value pair. I want to replace characters that occur after “:”
For example
App.log.level: “xyz”
It sounds like you just want something like sed 's/:.*$/: YOURTEXTHERE/' where the general format is sed 's/REPLACE_THIS/WITH_THIS/g'
The /:.*$/ bit means I want to replace all text from a colon to the end of the line. The : YOURTEXTHERE is what you're replacing with. (I'm putting the colon back in and putting the extra text.) Since I'm only doing one replacement per line, I don't need the g at the end (although it wouldn't hurt anything.)
A real example:
>> echo App.log.level: \"xyz\" | sed 's/:.*$/: YOURTEXTHERE/'
App.log.level: YOURTEXTHERE

Using the awk command in linux terminal to ignore repeats?

So I am working on a program that needs to scan a file with a format that causes trouble when I use the awk function.
Basically, the trouble I am having is that there are repeats that I don't want to have. For example, file looks like this:
abcd
abcde
I do a line by line search for the string "abcd", and I only want it to give me the first line, not both. Is there anything I can add to the awk function so that it searches for just the thing I'm looking for and nothing more?
I apologize if this question is dumb, but I really couldn't figure out a way to search for the problem I'm having online, and from what I've read about awk, I couldn't find a way to fix my problem.
I also cannot edit the file at all, unfortunately.
Thanks for the help!
Just an another way,
awk '/(^| )abcd( |$)/' file
It prints the line which contains the string abcd preceded by a space or a starting pattern and followed by a space or an end pattern.
Explanation:
(^| ) Matches the start of a line OR a space.
abcd Matches the Literal abcd.
( |$) Matches a space or a line end.
| called a logical OR operator.
If a line matches the above mentioned pattern then it will be printed. You don't need to specify '{print}'. AWK would do it automatically.
You can do:
awk '/^abcd$/' file
or you can do
awk '$0=="abcd"' file
or if its the second field that needs to be only abcd not abcde
awk '$2=="abcd"' file
All this will only match abcd and not abcde

Replacing comma on specific lines only

I have a dataset that is comma separated. But I have a little problem with its format. I want everything to be in the form x,x,x
Below is a sample of my dataset:
995970,16779453
995971,16828069
995972,
995973,16828069
995974,16827226
As you can see, most of my dataset is in the proper format but I have those commas on single id#'s also (my data is in form id#, connection#). How would I go about removing the commas on those single id#'s? I can't seem to figure it out just using a text editor. Any suggestions?
Edit: can I use some sort of regex expression to only remove it from those ids that have a specified length?
Edit2: Ok I figured it out using some regex, thanks for all the help!
In vi one would do something like
:%s/,$//
This means
: (enter a line mode command)
% (try the command on every line)
s (substitute)
,$ (match a comma at the end of a line)
(empty replacement text)
Sometimes you need something like /, *$/ do match a comma followed by 0 or more trailing spaces. You can get vi on windows in various different ways; one way is to install Cygwin.
You can select regular expression mode in Notepad++ and do find and replace using the following regex ,$. Leave the replace field blank.
With the sed command:
sed 's/, *//' < FILE
or inplace (requires GNU sed):
sed -ie 's/, *//' FILE

Resources