sed repetition-operator operand invalid ***** - sed

I have text files that contain ***** in some locations. I need to replace the ***** with 9.999. This obviously came from some formatting error, but I do not have the program that created the files I now have to work with. I tried using the following command in csh:
sed -i "" 's/*****/9.999/g' *.dat
However, as I expected, I get the following error message:
sed: 1: "s/*****/9.999/g": RE error: repetition-operator operand invalid
I'm assuming this is because ***** is considered a special operator or something like that, but I can't figure out how to exempt them while using the sed command.
Does anyone have a hint that could help?

sed -E 's/\*{5}/9.999/g' file

Related

jq reading filter from a file results in Invalid Syntax Error

filter.jq
".security = {hideVersionStringsWhenNotLogged: true}"
When trying to apply the filter,
jq --from-file filter.jq a.json
encountering the following error
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Unix shell quoting issues?) at , line 1
tried all combinations of single and double quotes to no avail.
Any suggestions?
Thx
If you want the filter to be read from a file, do not enclose it in the quotation marks that are needed if the filter is specified on the command line.
So the contents of your filter.jq file could look like this:
.security = {hideVersionStringsWhenNotLogged: true}

Remove a specific word from a file using shell script

I would request some help with a basic shell script that should do the following job.
File a particular word from a given file (file path is always constant)
Backup the file
Delete the specific word or replace the word with ;
Save the file changes
Example
File Name - abc.cfg
Contains the following lines
network;private;Temp;Windows;System32
I've used the following SED command for the operation
sed -i -e "/Temp;/d" abc.cfg
The output is not as expected. The complete line is removed instead of just the word Temp;
Any help would be appreciated. Thank you
sed matches against lines, and /d is the delete directive, which is why you get a deleted line. Instead, use substitution to replace the offending word with nothing:
sed 's/Temp;//g' abc.cfg
The /g modifier means "globlal", in case the offending word appears more than once. I would hold off on the -i (inline) flag until you are sure of your command, in general, or use -i .backup.
Thank you. I used your suggestion but couldn't get through. I appreciate the input though.
I was able to achieve this using the following SED syntax
sed -e "s/Temp//g" -i.backup abc.cfg
I wanted to take the backup before the change & hence -i was helpful.

Puppet sed and replace

Im learning Puppet and currently trying to install tomcat. While trying to replace the Catalina home on the startup.sh using sed in the exec block, Im facing the below error.
Error: Could not retrieve catalog from remote server: Error 400 on
SERVER: Syntax error at '|export CATALINA_HOME=' at
/etc/puppetlabs/code/environments/production/modules/tomcat/manifests/init.pp:26:58
on node agent
Current value of startup.sh
export CATALINA_HOME="/home/john"
export JAVA_HOME="/usr"
......
.....
Expected output
export CATALINA_HOME="/home/john/apache-tomcat-6.0.44"
export JAVA_HOME="/usr/java/default"
My code snippet
.......
exec { 'modify_file':
command => "sed -i 's|export CATALINA_HOME="/home/john"|export CATALINA_HOME="/home/john/apache-tomcat-6.0.44"|g' /home/john/apache-tomcat-6.0.44/bin/startup.sh"
path => '/bin',
}
Any help is really appreciated, thanks in advance.
Also, I had went thru the puppet documents regarding path atrribute of exec block but I'm not sure why it is being used and what should be my path value in my manifest file.
Your sed expression is likely broken due to quote mismatch.
You could simplify the sed command by using:
sed -i '/CATALINA_HOME=/s,/home/john,&/apache-tomcat-6.0.44,;/JAVA_HOME=/s,/usr,&/java/default,' /home/john/apache-tomcat-6.0.44/bin/startup.sh
The expression contains 2 commands for both CATALINA_HOME and JAVA_HOME. Both commands uses the same syntax to append the required string to the variable declaration.
/<regex>/s will perform the subsitution on line with the <regex>.
The , is the command separator. I usually use / unless the pattern to search is a directory path.
& is printing the pattern space, i.e. the matched pattern.
Had you already tried using the file_line resource type of puppetlabs-stdlib module instead of doing an exec call?
You can see how it works here.
Match parameter receive the old value and will be replaced by the value of line parameter. For example:
file_line { 'catalina':
ensure => present,
path => '/etc/catalina/startup.sh',
line => 'export CATALINA_HOME=\"/home/john/apache-tomcat-6.0.44\"',
match => 'export CATALINA_HOME=\"/home/john\"',
}

Using sed can't replace text in file with contents of variable

I have $subs variable and bbb file.
$subs variable contents:
echo $subs
http://somesite21.something.com:8088/premier-esb-emulator-app/services/es bServiceStub
bbb file contents:
BBBBbbbAAAAA
The command I try to use:
sed "s|bb|${subs}|" bbbb
But it doesn't work, error output:
sed: -e expression #1, char 85: unterminated `s' command
Please advise. Thanks in advance!
The following is my script:
#!/bin/bash
set -x
subs="http://somesite21.something.com:8088/premier-esb-emulator-app/services/es bServiceStub"
sed "s|bb|${subs}|" bbb
This is how bbb file looks like:
BBBBbbbAAAAA
and here is the output:
I know it is a bad answer, but it seems to be working for me.
__UPDATE__
Let's see If I got you right:
Assuming your working environment is unix-like
Jenkins sets a variable, namely subs. I assumed it is an exported var
You need to replace some part of a file, called bbb.
Substitution would be a single line of code.
So, this still works for me:
Am I missing something ?
__UPDATE_II__
I think I found the problem in your case: the way you set the env variable. Please look at my console output below and pay extra attention to the way I set subs var from output.txt file. It works the way I did it so it should work on your side too.
I decided too add commands here for your convenience:
cat output.txt
export subs=`cat output.txt`
echo ${subs}
sed "s|bb|${subs}|" bbb
Let's hope this time it will work for you =]

sed: matching unicode blocks with

I am desperately trying to replace certain unicode characters (graphemes) from a file using sed. However I keep failing for some of them, namely the ones from unicode blocks:
\p{InHigh_Surrogates}: U+D800–U+DB7F
\p{InHigh_Private_Use_Surrogates}: U+DB80–U+DBFF
\p{InLow_Surrogates}: U+DC00–U+DFFF
I tried (in a sed config file loaded via the -f switch):
s/\p{InHigh_Surrogates}/###/ --> no effect at all
s/\\p\{InHigh_Surrogates\}/###_D-NON-UTF8_###/ -> error message 'Invalid content of \{\}'
Anybody got a suggestion? Also, I am not necessarily focused on using the blocks - but I also failed trying to define a character range of the form \xd800-\xdfff.
Thanks,
Thomas
Try using the -r flag for sed:
$ sed -r 's/\\p\{InHigh_Surrogates\}/###/g' file
###: U+D800–U+DB7F
\p{InHigh_Private_Use_Surrogates}: U+DB80–U+DBFF
\p{InLow_Surrogates}: U+DC00–U+DFFF
From man sed:
-r, --regexp-extended
use extended regular expressions in the script.