sed: string between single qoutes - sed

I want to put an capital E infront of every string.
It is important that I use sed.
Such that
(260,'\"$40 a Day\"',2002,'Color','USA','','2000100002',131,6.1,'2002-04-24')
becomes
(260,E'\"$40 a Day\"',2002,E'Color',E'USA',E'',E'2000100002',131,6.1,E'2002-04-24')
I have tried
sed "s/'.*'/E&/g"
but it only puts an E infront of the first string!
Regards Kim

Another sed,
sed "s/,'/,E'/g"

The greedy matching of * is matching from the first single quote all the way to the very last one. Try this instead:
sed "s/'[^']*'/E&/g"
As John1024 warns above, this will not work if escaped single-quotes are allowed.

Related

Append specific caracter at the end of each line

I have a file and I want to append a specific text, \0A, to the end of each of its lines.
I used this command,
sed -i s/$/\0A/ file.txt
but that didn't work with backslash \0A.
In its default operations, sed cyclically appends a line from input, less it's terminating <newline>-character, into the pattern space of sed.
The OP wants to use sed to append the character \0A at the end of a line. This is the hexadecimal representation of the <newline>-character (cfr. http://www.asciitable.com/). So from this perspective, the OP attempts to double space a files. This can be easilly done using:
sed G file
The G command, appends a newline followed by the content of the hold space to the pattern space. Since the hold space is always empty, it just appends a newline character to the pattern space. The default action of sed is to print the line. So this just double-spaces a file.
Your command should be fixed by simply enclosing s/$/\0A/ in single quotes (') and escaping the backslash (with another backslash):
sed -i 's/$/\\0A/' file.txt
Notice that the surrounding 's protect that string from being processed by the shell, but the bashslash still needed escape in order to protect it from SED itself.
Obviously, it's still possible to avoid the single quotes if you escape enough:
sed -i s/$/\\\\0A/ file.txt
In this case there are no single quotes to protect the string, so we need write \\ in the shell to get SED fed with \, but we need two of those \\, i.e. \\\\, so that SED is fed with \\, which is an escaped \.
Move obviously, I'd never ever suggest the second alternative.

How to cut prefix of string with 'sed'?

Consider the following lines:
prefix1.value[TAB]someString
prefix2.anotherVal[TAB]anotherString
val[TAB]String
pref.stuff[TAB]stuff
dontTouch[TAB]stuff
I would like to have the result
value[TAB]someString
anotherVal[TAB]anotherString
val[TAB]String
stuff[TAB]stuff
dontTouch[TAB]stuff
So I want to cut the prefix. if there is one. Regular expressions work in the way that the first match is the longest so I was not able to create a working program. Is it possible to do this task with a single sed program?
My solution that is not working as it should:
sed 's/^[^\t\.]*\.\?\([^\t\.]\+\)\t\(.*\)/\1\t\2/'
This matches the prefix alone, and replaces it by an empty string.
sed 's/^[^\t\.]*\.//'
Try this if there is only one dot possibe:
sed -e 's/^.*\.//' file
if until first dot
sed 's/^[^.]*\.//' YourFile
if until last dot
se 's/.*\.//' YourFile
up to you to define your prefixe type

Sed replace text

I need to replace text with single quotes with sed and I can't get it to work. Here is my code; can you help me?
I have a text file with this format:
#sometext
$configuration_TEstbk2_bk2_environment12 = 'lalala'
$configuration_TEstbk2_bk2_envoronment12 = 'lalala1'
$configuration_TEstbk2_bk2_staging12 = 'BACKUP 2'
$configuration_waq4faw4f_q4fq4qg4f = 'r234rq43rq4rqr'
$configuration_alice_StagingTEstBk_bk = 'testebk'
$configuration_deployment_overlays_alice_TEStStngDir = 'some'
$configuration_arefgqrqgrq_341q34tq34t = '134t135'
And I need to do something like:
sed s/$configuration_arefgqrqgrq_341q34tq34t ='134t135'/$configuration_arefgqrqgrq_341q34tq34t = 'NEWVALUE'/g
I have tried with many combinations with sed but I can't find one that works.
Would this work for you?
sed "/\$config_deployment_overlays_alice_arefgqrqgrq_341q34tq34t_staging/s/'134t135'/'NEWVALUE'/" file
I'd probably use:
sed '/$configuration_arefgqrqgrq_341q34tq34t *=/'"s/'134t135'/'NEWVALUE'/"
This uses a mix of single quotes and double quotes at the shell level to get the correct information to sed. The single quotes enclose the search condition for the lines containing $configuration_arefgqrqgrq_341q34tq34t followed by some blanks and an equals sign. This avoids the shell expanding what might be a shell variable name (but probably isn't, so the empty string would be substituted). I then switch to double quotes at the shell level, so that sed gets to see the single quotes: it substitutes the value in single quotes with the replacement value, but only on those lines that contain the given configuration parameter name.
I hope users never have to type those configuration parameter names.
I suppose your problem is with the quoting. You could use complex quoting to make sure everything is in single quotes, except the single quotes which need to be in double quotes:
sed 's/$configuration_arefgqrqgrq_341q34tq34t *= *'"'134t135'"'/$configuration_arefgqrqgrq_341q34tq34t = '"'NEWVALUE'/g"
... or you could use some temporary variables to make the whole thing more readable, and suitable for inclusion in a single pair of double quotes:
directive='$configuration_arefgqrqgrq_341q34tq34t'
oldvalue="'134t135'"
newvalue="'NEWVALUE'"
sed "s/$directive *= *$oldvalue/$directive = $newvalue/g"
(If you only expect one match, the /g flag is superfluous.)
You can also totally avoid matching quotes by capturing them:
sed '/$configuration_arefgqrqgrq_341q34tq34t/{
s/\(= *.\).*\(.\) *$/\1NEWVALUE\2/
}' input
This might work for you (GNU sed):
sed -r 's/^(\$\S+\s=\s'\'').*('\'')/\1NEWVALUE\2/' file

how to use sed/awk to remove words with multiple pattern count

I have a file of string records where one of the fields - delimited by "," - can contain one or more "-" inside it.
The goal is to delete the field value if it contains more than two "-".
i am trying to recoup my past knowledge of sed/awk but can't make much headway
==========
info,whitepaper,Data-Centers,yes-the-6-top-problems-in-your-data-center-lane
info,whitepaper,Data-Centers,the-evolution-center
info,whitepaper,Data-Centers,the-evolution-of-lan-technology-lanner
==========
expected outcome:
info,whitepaper,Data-Centers
info,whitepaper,Data-Centers,the-evolution-center
info,whitepaper,Data-Centers
thanks
Try
sed -r 's/(^|,)([^,-]+-){3,}[^,]+(,|$)/\3/g'
or if you're into slashes
sed 's/\(^\|,\)\([^,-]\+-\)\{3,\}[^,]\+\(,\|$\)/\3/g'
Explanation:
I'm using the most basic sed command: substitution. The syntax is: s/pattern/replacement/flags.
Here pattern is (^|,)([^,-]+-){3,}[^,]+(,|$), replacement is \3, flags is g.
The g flag means global replacement (all matching parts are replaced, not only the first in line).
In pattern:
brackets () create a group. Somewhat like in math. They also allow to refer to a group with a number later.
^ and $ mean beginning and end of the string.
| means "or", so (^|,) means "comma or beginning of the string".
square brackets [] mean a character class, ^ inside means negation. So [^,-] means "anything but comma or hyphen". Not that usually the hyphen has a special meaning in character classes: [a-z] means all lowercase letters. But here it's just a hyphen because it's not in the middle.
+ after an expression means "match it 1 or more times" (like * means match it 0 or more times).
{N} means "match it exactly N times. {N,M} is "from N to M times". {3,} means "three times or more". + is equivalent to {1,}.
So this is it. The replacement is just \3. This refers to the third group in (), in this case (,|$). This will be the only thing left after the substitution.
P.S. the -r option just changes what characters need to be escaped: without it all of ()-{}| are treated as regular chars unless you escape them with \. Conversely, to match literal ( with -r option you'll need to escape it.
P.P.S. Here's a reference for sed. man sed is your friend as well.
Let me know if you have further questions.
You could try perl instead of sed or awk:
perl -F, -lane 'print join ",", grep { !/-.*-.*-/ } #F' < file.txt
This might work for you:
sed 's/,\{,1\}[^,-]*\(-[^,]*\)\{3,\}//g file
sed 's/\(^\|,\)\([^,]*-\)\{3\}[^,]*\(,\|$\)//g'
This should work in more cases:
sed 's/,$/\n/g;s/\(^\|,\|\n\)\([^,\n]*-\)\{3\}[^,\n]*\(,\|\n\|$\)/\3/g;s/,$//;s/\n/,/g'

Basic SED question for Linux

Temp file has only the number 22.5 in it.
I use
sed 's/.//' Temp
and I expect 225 but get 2.5
Why?
The dot is a special character meaning "match any character".
$ sed s/\\.// temp
225
You would think that you could do sed s/\.// temp, but your shell will escape that single backslash and pass s/.// to sed.. So, you need to put two backslashes to pass a literal backslash to sed, which will properly treat \. as a literal dot. Or, you could quote the command to retain the literal backslash:
$ sed "s/\.//" temp
225
The reason you get 2.5 when you do s/.// is that the dot matches the first character in the file and removes it.
Because '.' is a regular expression that matches any character. You want 's/\.//'
. is a wildcard character for any character, so the first character is replaced by nothing, then sed is done.
You want sed 's/\.//' Temp. The backslash is used to escape special characters so that they regain their face value.
'.' is special: it matches any single character. So in your case, the sed expression matches the first character on the line. Try escaping it like this:
s/\.//
you can also use awk
awk '{sub(".","")}1' temp