Adding lines to a linux file using bash script [closed] - linux

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
The community reviewed whether to reopen this question last year and left it closed:
Original close reason(s) were not resolved
Improve this question
I want to add lines '{"index":{"_id":$number_of_id}}' to my file name ss.josn before each json object (which is a line)
cat ss.json
input example:
{"msisdn":"255742005249","subscription_date":"2021-05-26T16:41:40.467Z","optout_date":"2022-01-03T19:12:59.808Z","total_charged_amount":1300}
{"msisdn":"255742009077","subscription_date":"2019-12-31T22:00:00.000Z","optout_date":"2022-01-09T14:08:00.282Z","total_charged_amount":3370}
{"msisdn":"255742009703","subscription_date":"2019-12-31T22:00:00.000Z","optout_date":"2022-01-01T12:34:02.104Z","total_charged_amount":400}
Desired output:
{"index":{"_id":1}}
{"msisdn":"255742005249","subscription_date":"2021-05-26T16:41:40.467Z","optout_date":"2022-01-03T19:12:59.808Z","total_charged_amount":1300}
{"index":{"_id":2}}
{"msisdn":"255742009077","subscription_date":"2019-12-31T22:00:00.000Z","optout_date":"2022-01-09T14:08:00.282Z","total_charged_amount":3370}
{"index":{"_id":3}}
{"msisdn":"255742009703","subscription_date":"2019-12-31T22:00:00.000Z","optout_date":"2022-01-01T12:34:02.104Z","total_charged_amount":400}

If you can ensure that each line is a json struct and you don't have multiline case, a script like this should do the job:
#!/bin/bash
_id=0
while read -r line; do
echo "{"index":{"_id":$_id}}" >> result.json
echo $line >> result.json
_id=$((_id+1))
done < ss.json
Maybe not the most elegant option, but you will have a result.json file with your desired output.

Related

Add rows in an excel file with a shell script [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 days ago.
Improve this question
I'm trying to create a shell script that creates an excel output file and add data in it.
I don't know how to add rows in this excel file. The goal being that the data will be added in separate cells.
I tried many instruction that i found in the web, but none worked for me.
the file after my script would be like below:
Is there an instruction to do this please?
Thanks for your help
This code worked for me:
touch Output.xls
chmod 777 Output.xls
printf "Date\tTotal number\tSuccessful number\tFailed numbe\n" >> Output.xls
printf "2016_05_11\t20\t5\t15\n" >> Output.xls
printf "2016_06_30\t30\t16\t14\n" >> Output.xls
The output file is as below:
enter image description here

Delete the particular line in files using bash script [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 days ago.
Improve this question
Write a program to open the file and delete a specific line from a text file. The line number of the line to be replaced is asked as input. A new file is opened in write mode named output.txt. The input file name should be input.txt and output file name should be output.txt.
someone please help me through it

How do i change bi-us-rds-September text value in a file to bi-us-rds- + current date with september as month name? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
This post was edited and submitted for review 12 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
i have a file which contains few lines of code
identifier = "bi-us-rds-15september"
i have to change "bi-us-rds-15september" with "bi-us-rds-current date & current month " .
For example - "bi-us-rds-15september" with "bi-us-rds-18september .
I want to accomplish this using task shell script but i am not good with scripting .
Just use the date format
$ identifier="bi-us-rds-`date +%d%B`"
$ echo $identifier
bi-us-rds-18September
I'd suggest maybe using https://www.tutorialkart.com/bash-shell-scripting/bash-date-format-options-examples/
Also fyi your question is a bit unhelpful - I'd suggest next time at least offer your current script so we can be more precise with the assistance.
edit: removed the extra hyphen just so it matches your exact output required
edit: re: your comment...
$replace=Hi
$replacewith="Hey"
$ echo "Hi how are you?" | sed -e "s/$replace/$replacewith/g"
Hey how are you?

Assign a value to a variable which has =," [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to print the line which has below value in my text file(ex: file.txt).
I have tried multiple ways but no luck. Can someone please help me the right command.
selector="((Source IN ('VPS','VPSLegacy')) OR (JMS_TIBCO_SENDER='rig'))"
Please note that i need to use entire above value to check in the file.
The easiest way to assign a literal (potentially containing quotes or other shell syntax) to a variable, even though it comes with a performance penalty, is to use a quoted heredoc:
content=$(cat <<'EOF'
selector="((Source IN ('VPS','VPSLegacy')) OR (JMS_TIBCO_SENDER='rig'))"
EOF
)
echo "Value is: <$content>"
...correctly emits as output:
Value is: <selector="((Source IN ('VPS','VPSLegacy')) OR (JMS_TIBCO_SENDER='rig'))">

I need a linux script to report all error lines from a log file, and export the results of error lines into a .csv file? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have log file for checking transactions, and I have error lines, so I need those error lines to be exported to a .csv file? is there any code using linux bash shell script can do this?
suppose your error lines consists ERROR.
then
grep "ERROR" errorfile.txt | tr -s '[:blank:]' ',' >> errorfile.csv
csv conversion is based on blank spaces to each cell.you can replace blank filter with anything

Resources