Redirect error messages to a file in sublime text - sublimetext

Reference image
Is it possible to redirect the message in the shell to a file say error.txt. I use the following build system
{
"cmd": ["g++.exe","-std=c++17", "${file}", "-o", "${file_base_name}.exe", "&&" , "${file_base_name}.exe<input.txt>output.txt2>error.txt"],
"selector": "source.cpp",
"shell": true,
"working_dir": "$file_path"
}

Yes it possible to redirect the message in the shell to a file "error.txt" using following build system.
{
"cmd": ["g++.exe","-std=c++17", "${file}", "-o", "${file_base_name}.exe", "&&" , "${file_base_name}.exe<input.txt","2>error.txt>output.txt"],
"selector": "source.cpp",
"shell": true,
"working_dir": "$file_path"
}
There must be a space between input.txt and error.txt so we separate them with a ',' delimeter . 2>error.txt redirects error stream to error.txt file then output stream is redirected to output.txt file.

Related

bash or powershell parsing jason file content

I would like to manipulate the content of jason file.
I've tried with powershell or linux bash but I was unable to get what I want.
On linux, I was thinking to use jq tool, despite obtains data, I cannot manipulate them.
jq '.[].pathSpec, .[].scope' jasonfilepath
Current output:
"file"
"file"
"/u01/app/grid/*/bin/oracle"
"/u01/app/oracle/product/*/db_1/bin/oracle"
My goal is to obtain something similar as:
scope pathSpec
Like:
file /u01/app/grid/*/bin/oracle
file /u01/app/oracle/product/*/db_1/bin/oracle
Jason file sample
[
{
"actions": [
"upload",
"detect"
],
"deep": false,
"dfi": true,
"dynamic": true,
"inject": false,
"monitor": false,
"pathSpec": "/u01/app/grid/*/bin/oracle",
"scope": "file"
},
{
"actions": [
"upload",
"detect"
],
"deep": false,
"dfi": true,
"dynamic": true,
"inject": false,
"monitor": false,
"pathSpec": "/u01/app/oracle/product/*/db_1/bin/oracle",
"scope": "file"
}
]
Do you have any idea to get this kind of expected output in Powershell and bash?
Thanks by advance,
Assuming a JSON input file named file.json:
In a Linux / Bash environment, use the following:
jq -r '.[] | .scope + " " + .pathSpec' file.json
In PowerShell, use the following (adapted from a comment by JohnLBevan):
(Get-Content -Raw file.json | ConvertFrom-Json) |
ForEach-Object { '{0} {1}' -f $_.scope, $_.pathSpec }
Note the (...) around the pipeline with the ConvertFrom-Json call, which is necessary in Windows PowerShell (but no longer in PowerShell (Core) 7+) to ensure that the parsed JSON array is enumerated in the pipeline, i.e. to ensure that its elements are sent one by one - see this post for more information.

Appending to a configuration file

I am creating a script which updates hosts in an application, the config file for each host looks like that below. The script generates the hosts correctly but I need to append every } with a comma , except the last host.
I have tried numerous things but the closest I have got is putting the hosts content on a single line and running a IFS statement against it. Im also not sure how best to approach this, can anyone advise?
{
"cmd": "ssh user#webserver",
"inTerminal": "new",
"name": "webserver",
"theme": "basic",
"title": "Webserver",
}
example of what I am trying to achieve
{
"cmd": "ssh user#webserver",
"inTerminal": "new",
"name": "webserver",
"theme": "basic",
"title": "Webserver",
},
{
"cmd": "ssh user#db",
"inTerminal": "new",
"name": "db server",
"theme": "basic",
"title": "db",
},
{
"cmd": "ssh user#mail",
"inTerminal": "new",
"name": "mail server",
"theme": "basic",
"title": "mail server",
}
You can do things like:
#!/bin/sh
for f in $(generate-host-list); do
read -d \000 c < "$f"
list="$list${list+,
}$c"
done
echo "$list"
If you are just writing to a file that can be simpler (no need for the read, just cat the file). Similarly, if you don't care about munging whitespace, you could do list="$list${list+,}$(cat "$f"). If you are using bash or some other shells you can do non-portable things like += to clean it up.
You can do it like this:
sed '$q; s/^}$/},/' <in_file >out_file
The above sed command works as follows: First check if you've reached the last
line, and if so quit. Otherwise, it'll check if the only character on the line
is }, and if so replace it with },.

How to suppress output in Sublime Text cmd?

How can I suppress the output in Sublime Text Build System output?
I tried this but it says Error: unrecognized or incomplete command line.
{
"cmd": ["ipconfig", ">nul 2>&1"],
"name": "ipconfig",
"shell": "true"
}
{
"name": "ipconfig",
"shell_cmd": "ipconfig >nul 2>&1"
}
I always use shell_cmd because you can just write the command like you would on the command line instead of trying to figure out the multiple arguments nonsense in the array. This worked for me.

Error in custom sublime build for knitr markdown

I'm trying to create my own sublime-build in Sublime Text 3, since the default build in knitr package (https://sublime.wbond.net/packages/knitr) doesn't work (for me). I modified the one in knitr package as follows:
{
"selector": "text.html.markdown.knitr",
"working_dir": "${project_path:${folder}}",
"cmd": ["Rscript", "-e", "library(knitr); knit('$file')" ],
"shell": true
}
but get the error:
Error: '\G' is an unrecognized escape in character string starting "'C:\G"
Execution halted
[Finished in 0.4s with exit code 1]
[cmd: ['Rscript', '-e', "library(knitr); knit('C:\\GitHub\\Projects\\test\\testLaTeXing\\knitrRmd\\testRmd.Rmd')"]]
obviously some windows path escape issue, but how can I fix this, when I want to acces file path dynamically?
I'm working on windows7.
Try setting your "cmd" line to this:
"cmd": ["Rscript", "-e", "library(knitr); knit('${file/\\\\/\//}')" ],
Basically, it's a regular expression to match two \ characters and replace them with one / character. Each \ and / needs to be escaped by a \, hence the chicken scratch.
I'm not on Windows to test, but theoretically this should work. If it doesn't, another option to try is
"cmd": ["Rscript", "-e", "library(knitr); knit('${file/\\\\/\\/}')" ],
where it's replacing the \\ with a single \.
Please let me know how it works, I'm interested to know!
This is what I finally cooked up, thanks to #MattDMo.
{
"selector": "text.html.markdown.knitr",
"working_dir": "${project_path:${folder}}",
"cmd": [ "Rscript -e \"library(knitr); knit('$file', output='$file_path/$file_base_name.md')\"" ],
"shell": true,
"windows":
{
"selector": "text.html.markdown.knitr",
// "working_dir": "${project_path:${folder}}",
// "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"cmd": ["Rscript", "-e", "library(knitr); knit('${file/\\\\/\\/\/g}', output='${file_path/\\\\/\\/\/g}/$file_base_name.md' )" ],
"shell": true
}
}
where the default command is from the knitr sublime package (https://github.com/andrewheiss/SublimeKnitr/blob/master/knitr-Markdown.sublime-build)

Go-Sublime-build configuration

Im having issues trying to set up go to run the current file from Sublime text 2.
Here's what I have in my go.sublime-build file
{
"cmd": [ "go", "run", "${file}" ]
}
When I try to run build on a go source file, I get the error
[Error 6] The handle is invalid
[cmd: [u'go run', u'C:\\Users\\gprasant\\Documents\\GitHub\\programming_pearls\\src\\go\\quicksort.go']]
[dir: C:\Users\gprasant\Documents\GitHub\programming_pearls\src\go]
Is there any way to get this fixed ? Or is there another plugin in Sublime text for Go development?
Installing GoSublime should get this working for you. After installing and restarting ST2: do ctrl-B, type "run" and hit enter.
I got by with
{
"cmd": "go run $file",
"shell" : true
}
In ST3: it is changed to be:
{
"shell_cmd": "go run ${file}"
}
On my mac, I needed the following code in:
/Users/your_user_name/Library/Application Support/Sublime Text 2/Packages/User/go.sublime-build
go.sublime-build
{
"cmd": ["go run '${file}'"],
"selector": "source.go",
"path": "/usr/local/go/bin",
"shell": true
}
"cmd" line quoting is to correctly handle file paths with spaces.
"shell" line is needed since commenting it out breaks it.
"path" line is needed because the basic shell, doesn't have access to my .zshrc file include the export GOPATH statement defining the go path.
After that any .go file should build and run with command+B, leaving the stdout message in a console built into sublime text 2.
what about:
{
"cmd": ["go", "run", "${file}"],
"path": "/user/local/go/bin"
}
I like GoSublime, just hate to type run each time when click Command + B
SublimeText 2
build-system for golang, making F4/shift-F4 work (next error/prev error)
1st, create a file: ~/gosublime_build.sh
GOPATH=~/go
export GOPATH
echo "GOPATH:$GOPATH"
if [ "$3." = "RUN." ]
then
EXENAME=${1##*/}
EXENAME=$GOPATH/bin/$EXENAME
echo $EXENAME
$($EXENAME)
echo "code: $?"
exit
fi
echo "go build $2"
cd /usr/local/go/bin
./go build -o ~/temp.go.compiled $2
if [ $? -eq 0 ]
then
cd $1
echo "Project: " $1
/usr/local/go/bin/go install
echo "go install exit code: $?"
else
echo "go build exit code: $?"
fi
2nd:
chmod 777 ~/gosublime_build.sh
3rd: create a new sublime2 build-system for "go" (Tools/Build System/New)
{
"cmd": ["~/gosublime_build.sh $file_path $file"]
,"shell": true
,"selector": "source.go"
,"file_regex": "([\\w/_-]+[.]{1}[\\w./_-]+?):([0-9]+):?([0-9]+)?(.*)?"
}
4th: select your new build-system (Tools/Build System)
5th: build with Ctrl-B, F4/Shift-F4: next/prev error
If anybody knows how to instruct the go compiler to inform FULL PATH of file and line for each error, this process can be simplified

Resources