How to display (echo) a large json file in bash? - bash

I have a json file and I know the location of it. let us say C:\desktop\file. I am adding a bash script in an existing application that would read this bash script take the output and use the json that it extracts for something else. How would I go about doing that.
output should be something like
echo Start
echo C:\desktop\file
echo End
The file is also large

Related

How can I give a script that reads input from a file input from stdin?

I am working on a project that uses a script (blastn from NCBI's BLAST+ package) that, as far as I can tell, can only read input from a file. I'd like to be able to pass it a string in stdin and have it treat that as if it's the contents of a query file. I could do that by writing the string to a file then giving this script the name of a file but is there a different way to do it that I could try?
2 things come to mind:
many tools (but not all) treat the filename - as "read from stdin"
seq 20 | paste - -
use a Process Substitution, that looks to the program like a filename
rev <( echo "hello world" )

How to split a real-time stdout stream into several files?

I have a python script which is continuously writing a text stream to stdout.
Something like this (genstream.py):
while 1:
print (int(time.time()))
time.sleep(1)
I want a bash script which launch the python script, save its output to a set of files, let's say to split the output every hour to avoid the creation of a huge file which is difficult to manage.
The so created files will be then processed (i.e. one at the end of each hour) by the same bash script to insert the values into a database and moved to an archive folder.
I did my search in google/stack overflow (e.g. split STDIN to multiple files (and compress them if possible) Bash reading STDOUT stream in real-time or https://unix.stackexchange.com/questions/26175/ ) but I didn't find any solution so far.
I've tried to use also something easy like this (so without taking in account the time but only the number of lines)
python3 ./genstream.py | split -l5 -
but I have no output.
I've tried a combination of (named-)pipes and tee but nothing seems to work.
Try this:
python3 ./genstream.py | while read line; do
echo "$line" >> split_$(date +%Y-%m-%d-%H)
done

getting interactive, multiple line, formatted input from stdin in bash

I want to be able to interactively get output from the terminal in a way similar to a hereDOC. Ie I want the user to be able to type multiple lines, then have that information passed into a file with all the formatting maintained. Something like this.
echo "Type your message below. To finish the letter type DONE by itself on a line"
file=mktmp
cat << DONE > $file
obviously this doesn't work, because the EOF is found before DONE. I thought about passing the user to something like VIM, but my less computer savy coworkers have a hard time with vim/emacs/nano.
You need to use an editor; standard input is just a stream of bytes, not an editor. However, you don't have to hard-code a specific editor. EDITOR is a standard environment variable meant to allow your script's caller to choose which editor is used.
: ${EDITOR:?Please set the environment variable EDITOR to the editor of your choice}
echo "Type your message below, then save and exit your editor."
"$EDITOR" "$file"
EDITOR is typically set by the user in their shell configuration file, but can be set on-demand when you run your script.
$ EDITOR=nano yourScript.sh
okay, so I came up with this, but please help me find something better or improve on it.
echo "Type your message below, to finish the letter press CTL+D"
mapfile message
file=`mktemp`
for x in `seq 0 ${#message[#]}`
do printf "${message[$x]}" >> $file
done
cat $file

How to write String to a text File

My question is about file handling in bash.
I want to write a string to text file in Bash script.
I have already tried
echo string>>filename.txt
your code is correct.you can write to a file using that.But you have to check the file access permission of the file you want to write the data in to.
Check following for more details.
Create text file and fill it using bash
Open and write data to text file using bash/shell scripting
That is a valid way of doing it. I would however consider this:
echo "string" >> file.txt
The quotes make it possible to handle more than one word (in the case of echo, that works anyway, but it's good practice to always use it). You should also distinguish between > and >>
> truncates the file (erases it) and replaces its contents with your string
>> on the other hand appends your string to the bottom of your file

Can't call a script from within a script

I am trying to call a script from within another script. The idea is that the program should take in email that is sent to it directly from unix mail as stdin, and then parse some stuff out and send it to a new script.
I am having trouble reaching the new script. However, this problem only occurs when the script is accepting the email directly. If I cat a file into it, there is no problem and it finds the new script.
IE: if i have a test file called "email.txt" and i do the command:
cat email.txt | ./receiveEmail.sh
then the script calling works fine.
but if receiveEmail.sh receives the email directly, it fails to call the new script. I know this is the point where it fails because I get logs that the script is working all the way up to where it tries to call the new script.
--------receiveEmail.sh----------
#!/bin/bash
###do some stuff to parse the stdin coming in and get variable $subject and $body
issue=`. /home/dlaf/bin/makeissue.sh` ->>>> this is the line that doesn't seem to work when the input is straight from the email rather than from a txt file i send it.
I am confused why. I think it might be because I am missing some part of the path? Maybe the email coming in has no idea what my full path actually is? Im not sure though because when I type in to the command line echo $LD_LIBRARY_PATH I just get a blank line, so I assume its not even set so I don't know how this could be a problem
When saving output to a variable with Bash, I usually do this
read issue < <(~/bin/makeissue.sh)
echo "$issue"
If the output is multiple lines you can do this
read -d'' issue < <(~/bin/makeissue.sh)
echo "$issue"
or this
mapfile issue < <(~/bin/makeissue.sh)
echo "${issue[#]}"

Resources