I have a file with following lines in a text file in following format And a json file in following format Now i want output of json as follows So how can achieve this using bash? I’ve tried with following but it only appends last line of the file. Answer This will do it:
Tag: bash
Using netcat to issue a http get request in bash
I have written the following script (it does not support index yet). The issue I am having is regarding using netcat to load the page. I can do this manually using the command line but when I try to have my script issue the exact same commands I can it does nothing without error. The only possible thing I can
Keep zero after Hexdecimal conversion in bash
I’m writing a very simple script in bash: I want to print all numbers with minimum three digits as 000, 001, 002 .. until 099. With integer numbers it works good, but after obase = 16; $ i ‘| bcs number return with one or two digits. How can I solve it in the easiest way? Answer You might want
How to change *** to textbf with sed
I export results from my analysis into a table in Latex. I would like to be able to change all coefficients that are significant to ***, **, * to bold face text textbf. This requires me to search with sed and change the stars to wrap around the coefficient. I want to change 0.047$^{***}$ to textbf{0.047} I…
How to validate the number of arguments (mandatory and optional) in a shell script?
I am having trouble validating my script arguments. I am trying to achieve 5 mandatory arguments and 2 optional arguments to my script. Here is what I have tried so far. The problem is validating the total number of arguments to this script as I can have a total of 5 or 7 arguments. Providing -a additional -c…
Error “syntax error near unexpected token ‘(‘” in Bash script when selecting files
Running this script, bash ./cleanup.bash, Gives the error: cleanup.bash: line 10: syntax error near unexpected token ‘(‘ cleanup.bash: line 10: ‘rm -rf !(composer.json|.git)’ But if I run in in the terminal directly, there aren’t any problems: I tried stripping out all other line…
Bash script that will read a number from file and run command if number equals or is higher than X value
I would like to have a script that reads a number from a file, and if this number equals or higher a certain value, it will run another command, if not, script will just die, it would be something like this: Thanks for any help. Answer
advanced printf explanation in bash
I just found this programming segment in my son’s Bash file. I am quite a newbie and unable to understand the printf syntax. Can someone explain me the COMMENTED printf in the segment below?? Answer It’s is nothing but a busy/wait spinner and the lines commented do nothing but set a blue foregroun…
How can I execute a command inside another folder and return to original directory?
I use this command to make inside a folder: But I want to return to original directory after the command I tried this: But this doesn’t work. How can I do it? Would help in quick testing… Answer Use a subshell, like so: The shell forks before running the commands in brackets, so once it completes …
Sorting an array of dates in bash
I want to sort an array of dates. Example format: “2017-11-13_07-55-40” or Year-Month-Date_Hour-Minute-Second Answer Since your dates are already in YYYY-MM-DD-HH-MM-SS format, you can use numeric sort: To store output in another array use: