Using sed on a special character [duplicate] - regex

This question already has answers here:
How to use variables in a command in sed?
(4 answers)
Closed 8 years ago.
currently I have a file
that contains the text
Your path: ../test/tester [hello] whatever
But I want to change it to: Your path: ../../tested/text [hello] whatever
I've tried sed but I believe ../ is a special character so I'm not too sure how to approach this problem.
This won't work for me.
sed s/tester/..//g;s/tester/tested/text/

Does this solve your problem?
➜ scripts cat example
Your path: ../test/tester [hello] whatever
➜ scripts sed "s|../test/tester|../../tested/text|" example > example2
➜ scripts cat example2
Your path: ../../tested/text [hello] whatever

Related

BCP command in shell script fails when called from C [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 days ago.
Improve this question
I have a simple bcp command as mentioned below.It works when I execute it from a shell script or as a command on a RHEL server.
/opt/mssql-tools/bin/bcp *schema_name.table_name* format nul -n -q -f */tmp/format_file* -U *username *-P *password *-D -S *Synp*
This command is to produce a format file for a SQL table existing on Azure synapse.
When I execute directly on terminal it works fine.
when i put in bash script and execute bash on terminal works fine(sample below).
#!/bin/bash
/opt/mssql-tools/bin/bcp *schema_name.table_name* format nul -n -q -f */tmp/format_file* -U *username *-P *password *-D -S *Synp*
But when the shell script is called/executed from a C++ program using an exec command it gives the below error-
The table name specified exceeds the maximum allowed length.
Its called remotely.
It looks like it has trouble recognising the parameters but I really can't understand the reason why.
To make matters worse when I tried executing a bcp out or in command it works just fine.It looks like it is only bcp format that has this issue regarding parameter interpretation.
Any help is apprceiated
I tried checkin environment variables but couldnt find anything.

Using makefile in macOS terminal [duplicate]

This question already has answers here:
makefile:4: *** missing separator. Stop
(17 answers)
Closed 1 year ago.
I'm new to macOS terminal commands and have recently got introduced to makefiles while learning to program in C++.
The makefile that I was using had the following content:
fact:factorial2.o facth.o
c++ factorial2.o facth.o -o fact
facth.o:facth.cpp
c++ -c facth.cpp
factorial2.o:factorial2.cpp
c++ -c factorial2.cpp
All the files, factorial2.cpp and facth.cpp are present in the same folder as makefile.
On entering 'make' command in the terminal, I received the following:
makefile:2: *** missing separator. Stop.
Kindly help me out with this and tell me where exactly I'm making the mistake as I'm not able to figure out the same. For my friends using Ubuntu, the above works perfectly.
Makefiles use tab delimeters rather than 4 spaces. Replace the spaces with tabs on lines 2, 4, and 6.

sed doesn't work in bash script even though individual RegEx expressions seem to be correct [duplicate]

This question already has an answer here:
Sed doesn't work in command line however regular expression in online test regex101 works
(1 answer)
Closed 4 years ago.
What I'm trying to do
Bash script to replace the uncommented php_version: "7.2" with the a user-entered version:
#!/bin/bash
# Ask desired PHP version
read -p "What version of PHP should be used? (5.6, 7.0, 7.1, or 7.2): " phpVersion
# Replace default php version with desired php version
sed -i "s/^php_version: \"[0-9]+\.[0-9]+\"/php_version: \"${phpVersion}\"/g" fileToSearchReplace.txt
Contents of fileToSearchReplace.txt which vim reports is a unix file:
APIVersion: v1.0.0
name: alpha-local
type: typo3
docroot: public
# php_version: "7.1" # PHP version to use, "5.6", "7.0", "7.1", "7.2"
php_version: "7.2"
router_http_port: "80"
router_https_port: "443"
xdebug_enabled: false
additional_hostnames: []
additional_fqdns: []
provider: default
Diagnostics
This worked: I checked the RegEx here using ^php_version: "[0-9]+\.[0-9]+" for search and php_version: "7.0" for replace (you can find replace and paste in the text a tab below. Notice I could only get the search to work using the /gm at the end. I have no idea how to get multiline to work in sed???
Environment
Windows 10 Pro host
Using MINGW64 git bash terminal
These files are part of a Docker container
I tried running this sed both in my MINGW64 git bash terminal and also inside the Ubuntu container
You cannot use + with sed by default. Either do your own + like that:
sed -i "s/^php_version: \"[0-9][0-9]*\.[0-9][0-9]*\"/php_version: \"${phpVersion}\"/g" fileToSearchReplace.txt
Or use the extended regex option -r:
sed -r -i "s/^php_version: \"[0-9]+\.[0-9]+\"/php_version: \"${phpVersion}\"/g" fileToSearchReplace.txt
Example: https://repl.it/repls/PowderblueFoolishEquipment
I don't think its a good idea to try out a regex from a site which doesn't say which class of regex it is supporting and re-using the same in your command.
You have a classic case of mix-up in which the regex pattern from the site, is of the Extended Regular Expressions and the one in your sed uses Basic Regular expression construct which does not support your [0-9]+ expression. Simply enable the ERE support with the -E switch in your sed
sed -Ei "s/^php_version: \"[0-9]+\.[0-9]+\"/php_version: \"${phpVersion}\"/g" fileToSearchReplace.txt
See the [ POSIX | GNU ] variants of the same solution, with the POSIX version using the BRE and the GNU version using ERE. Do read through this useful article on POSIX Regular Expressions on how the two variants of the regular expressions work.
You may use this script with select that lets user select a version from a menu list. You can then pass selected option to a simplified sed command:
select ver in "5.6" "7.0" "7.1" "7.2"; do
sed -i "s/^\(php_version: \).\+/\1\"$ver\"/" fileToSearchReplace.txt
break
done

why is "./" sign used to compile ? is it convention in c++? [duplicate]

This question already has answers here:
Why do you need ./ (dot-slash) before executable or script name to run it in bash?
(9 answers)
Closed 9 years ago.
g++ main.cpp main
./main
why do we use ./ sign? why can't we type "run main" ? Who made this convention? is it purely historical?
Also, can we change this ./ to run main forever? I suppose you can do it with bash script?
It has nothing to do with C++, it's your command shell. It's simply the way of telling it that the file exists in the current directory.

Grep across the file system has no output in a shell script

I'm trying to create a pre-commit hook in Git that will check for any debugging code and prompt the user to fix it. I have a regex that I'm grepping for (ignore the fact that it won't exclude occurrences in multiline comments!):
grep -IiRn --exclude-dir={node_modules,vendor,public,lib,contrib} --include=\*.{module,inc,install,php,js} -P '^\s*(?!\/\/)\s*(dpm\(|dsm\(|console.log\()' /path/to/code/
This works fine when I run it normally in the console, but when I try it in an executable .sh script it does nothing. None of the following has worked for me:
#!/bin/sh
grep ...
MYVAR =`grep ...` # Note the backticks!
echo $MYVAR
MYVAR =$(grep ...)
echo $MYVAR
MYVAR ="`grep ...`"
echo $MYVAR
I tried doing it with Python and os.system() but that did nothing either. It seems to just have no STDOUT. There's possibly something obvious I'm missing but I'm at a loose end.
Any help would be much appreciated! Thanks.
Edit:
This is the exact script, even though it's at the earliest possible stage due to not being able to actually do the first bit. I've hidden the exact folder names because it's probably best to not share my company's code base on SO ;)
#!/bin/bash
echo "Test!"
ONE=`grep -IiRn --exclude-dir={node_modules,vendor,public,lib,contrib} --include=\*.{module,inc,install,php,js} -P '^\s*(?!\/\/)\s*(dpm\(|dsm\(|console.log\()' /company/projects/company/www/sites/all/modules/custom/`
TWO=$(grep -IiRn --exclude-dir={node_modules,vendor,public,lib,contrib} --include=\*.{coffee} -P '^\s*(?!\#)\s*(dpm\(|dsm\(|console.log)' /company/projects/company/www/sites/all/modules/custom/)
echo $ONE
echo "$TWO"
... and running bash -x pre-commit returns:
ubuntu#ip-12-34-56-78:/company/projects/company/scripts$ bash -x pre-commit
+ echo 'Test!'
Test!
++ grep -IiRn --exclude-dir=node_modules --exclude-dir=vendor --exclude-dir=public --exclude-dir=lib --exclude-dir=contrib '--include=*.module' '--include=*.inc' '--include=*.install' '--include=*.php' '--include=*.js' -P '^\s*(?!\/\/)\s*(dpm\(|dsm\(|console.log\()' /company/projects/company/www/sites/all/modules/custom/
+ ONE='/company/projects/company/www/sites/all/modules/custom/some_module/some_module.report.inc:594: dsm('\''test'\'');
/company/projects/company/www/sites/all/modules/custom/goals_app/goals_app.module:170: console.log(e.stack);
/company/projects/company/www/sites/all/modules/custom/company_usage_reports/js/script.js:300: console.log('\''fetch success'\'');
/company/projects/company/www/sites/all/modules/custom/another_module/js/another_module_change_workgroup.js:19: console.log('\''wtf?'\'');
/company/projects/company/www/sites/all/modules/custom/another_module/js/another_module_reorder_table.js:33: console.log(resp);
/company/projects/company/www/sites/all/modules/custom/another_module/js/another_module_reorder_table.js:39: console.log(ui.placeholder);
/company/projects/company/www/sites/all/modules/custom/another_module/js/another_module_goal_form.js:4: console.log($( ".required" ));
/company/projects/company/www/sites/all/modules/custom/another_module/js/another_module_reorder.js:40: console.log(resp);
/company/projects/company/www/sites/all/modules/custom/company_goals/js/views/goal-list.js:87: console.log(data);'
++ grep -IiRn --exclude-dir=node_modules --exclude-dir=vendor --exclude-dir=public --exclude-dir=lib --exclude-dir=contrib '--include=*.{coffee}' -P '^\s*(?!\#)\s*(dpm\(|dsm\(|console.log)' /company/projects/company/www/sites/all/modules/custom/
+ TWO=
+ echo /company/projects/company/www/sites/all/modules/custom/some_module/some_module.report.inc:594: 'dsm('\''test'\'');' /company/projects/company/www/sites/all/modules/custom/goals_app/goals_app.module:170: 'console.log(e.stack);' /company/projects/company/www/sites/all/modules/custom/company_usage_reports/js/script.js:300: 'console.log('\''fetch' 'success'\'');' /company/projects/company/www/sites/all/modules/custom/another_module/js/another_module_change_workgroup.js:19: 'console.log('\''wtf?'\'');' /company/projects/company/www/sites/all/modules/custom/another_module/js/another_module_reorder_table.js:33: 'console.log(resp);' /company/projects/company/www/sites/all/modules/custom/another_module/js/another_module_reorder_table.js:39: 'console.log(ui.placeholder);' /company/projects/company/www/sites/all/modules/custom/another_module/js/another_module_goal_form.js:4: 'console.log($(' '".required"' '));' /company/projects/company/www/sites/all/modules/custom/another_module/js/another_module_reorder.js:40: 'console.log(resp);' /company/projects/company/www/sites/all/modules/custom/company_goals/js/views/goal-list.js:87: 'console.log(data);'
/company/projects/company/www/sites/all/modules/custom/some_module/some_module.report.inc:594: dsm('test'); /company/projects/company/www/sites/all/modules/custom/goals_app/goals_app.module:170: console.log(e.stack); /company/projects/company/www/sites/all/modules/custom/company_usage_reports/js/script.js:300: console.log('fetch success'); /company/projects/company/www/sites/all/modules/custom/another_module/js/another_module_change_workgroup.js:19: console.log('wtf?'); /company/projects/company/www/sites/all/modules/custom/another_module/js/another_module_reorder_table.js:33: console.log(resp); /company/projects/company/www/sites/all/modules/custom/another_module/js/another_module_reorder_table.js:39: console.log(ui.placeholder); /company/projects/company/www/sites/all/modules/custom/another_module/js/another_module_goal_form.js:4: console.log($( ".required" )); /company/projects/company/www/sites/all/modules/custom/another_module/js/another_module_reorder.js:40: console.log(resp); /company/projects/company/www/sites/all/modules/custom/company_goals/js/views/goal-list.js:87: console.log(data);
+ echo ''
... but running it without the -x flag STILL doesn't work.
Edit two:
In case anyone is wondering, my env is as follows...
ubuntu#ip-12-34-56-78:~$ uname -a
Linux ip-12-34-56-78 3.2.0-31-virtual #50-Ubuntu SMP Fri Sep 7 16:36:36 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
ubuntu#ip-12-34-56-78:~$ whereis sh && whereis bash
sh: /bin/sh /bin/sh.distrib /usr/share/man/man1/sh.1.gz
bash: /bin/bash /etc/bash.bashrc /usr/share/man/man1/bash.1.gz
I can't say for sure until you post the actual script you're running, but in your current code snippet have
#!/bin/sh
Depending on your OS, this may be a link to /bin/bash, for example, or it may be the actual Bourne shell, which does not support brace expansion (e.g. {a, b, c}). Even if /bin/sh does point to /bin/bash on your machine, you should only use portable constructs if your shebang is #!/bin/sh (i.e. say what you mean). If you want to use brace expansion in your script, change the shebang to #!/bin/bash.
If you put
set -x
at the top of your script, it will print detailed information that can help with debugging. You can also do this by invoking the shell directly instead of modifying your script, for example
sh -x /path/to/script
or
bash -x /path/to/script
EDIT: On Ubuntu, /bin/sh is dash, the Debian Almquist shell. Like the Bourne shell, dash is fairly restrictive, and does not support brace expansion. See this page for a discussion of portability issues and dash.