BASH loop -- newbie


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH loop -- newbie
# 1  
Old 03-17-2016
BASH loop -- newbie

Hi forums,

Wondering if someone could assist me!!...

I basically want to do a while read line on a file and input it into a set of parameters for a JSON geospatial demo.. With a loop where every line creates a new "card" to an effect... card example below...

Data CSV one line:

Code:
152047731,VANDALISM,42.23226359, -71.12924508

Current Script: {fails obviously just frustrated now}

Code:
#!/bin/bash
while read line GEO_Crime_Incident_Reports.csv
 do
 var=$(awk '{print $1,$2,$3,$4}')
 echo $var{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "id": "$1",
        "title": "$2"
              },
      "geometry": {
        "coordinates": [
          $4,
          $3
        ],
        "type": "Point"
      },
      "id": "$1"
    },


Thanks!!!
Skoot

Last edited by Scrutinizer; 03-17-2016 at 04:39 AM.. Reason: code tags
# 2  
Old 03-17-2016
bash code:
  1. #!/bin/bash
  2.  
  3. OUT_FILE=output.json
  4.  
  5. if [ -e $OUT_FILE ]
  6. then
  7.     rm -f $OUT_FILE
  8. fi
  9.  
  10. while IFS=, read id title lat lon
  11. do
  12.         echo "{
  13.        \"type\": \"FeatureCollection\",
  14.        \"features\": [
  15.        {
  16.        \"type\": \"Feature\",
  17.        \"properties\": {
  18.        \"id\": \"$id\",
  19.        \"title\": \"$title\"
  20.        },
  21.        \"geometry\": {
  22.        \"coordinates\": [
  23.        $lat,
  24.        $lon
  25.        ],
  26.        \"type\": \"Point\"
  27.        },
  28.        \"id\": \"$id\"
  29.    }" >> $OUT_FILE
  30. done < test.csv

The required json is written to output.json file
# 3  
Old 03-17-2016
Thank you... revision below

Thanks for reply!

I am currently trying to read a file in linux or a database table {awk} and export those fields into preset parameters into my json structure.

i.e I want query a doc or a database and awk '{print $1,$3,$5,$7}' and input those columns into set fields above...

++++thisJSON structure doesn't need to be altered minus }]++++

Code:
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"id": "$1",
"title": "$3"
},
"geometry": {
"coordinates": [
$7,
$5
],
"type": "Point"
},
"id": "$1"
},

The }] aren't precise but that's easy looking for a plugin answer basically.

Last edited by Scrutinizer; 03-17-2016 at 04:40 AM.. Reason: code tags
# 4  
Old 03-17-2016
What is wrong with balajesuri's approach?


---
Here is an alternative using a here-doc, which I prefer in cases like this, since it saves you a lot of quoting an escaping and is easier to read.
Code:
while IFS=, read id title lat lon x
do
  cat << EOF
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"id": "$id",
"title": "$title"
},
"geometry": {
"coordinates": [
$lon,
$lat
],
"type": "Point"
},
"id": "$id"
}, 

EOF
done < infile > outfile

This is based on your input in post #1

If you need to use $1 $3 $5 and $7, change:
Code:
while IFS=, read id title lat lon x

to
Code:
while IFS=, read id x title x lat x lon x

Where x is a dummy variable that you can use to discard a value..
# 5  
Old 03-17-2016
Accomplished!

Hi Experts, that actually worked amazing. Switched the $lon and $lat to get the proper coords but other than that it worked fantastic. Thanks for the help!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Newbie needs help with some bash scripting

Please bear with me, I'm a beginner but have had some experience and decent knowledge to understand things as I read them and I'm in the process of trying to learn more. I recently inherited a UNIX server which has a bash script which is part of a crontab schedule that needs to be modified (or... (3 Replies)
Discussion started by: Danylko
3 Replies

2. Shell Programming and Scripting

Bash scripting - Newbie

Hi all, I have drill to do and I'll very appreciate your help: Please create a simple CSV file as follow (3 columns and 4 rows): A,B,C A,”B,C”,D “A,B”,C,D o A,B,”C,D” - Please refer to the comma between the quotation marks as a parameter and not as a separator. - Please provide... (3 Replies)
Discussion started by: elior
3 Replies

3. Shell Programming and Scripting

Looking for help with parsing file contents in bash [newbie]

Hi I'm just messing around with bash and trying to learn it because I have a course next semester dealing with OS design where we need to know how to use SSH client and either bash or ksh. I've never done shell scripting before. I just started today and I was wondering how parsing files... (1 Reply)
Discussion started by: mehungry
1 Replies

4. Shell Programming and Scripting

Newbie bash scripting help requested

Hi, I'm very new to bash scripting and Linux in general. I'm running Ubuntu Server 10.04 and trying to write a bash script to launch a program. In doing so, I've come across a couple of things that I obviously don't understand. Here is a short script that exemplifies those things: ... (9 Replies)
Discussion started by: Carson Dyle
9 Replies

5. Shell Programming and Scripting

Newbie: How to loop round sub-directories

I want a bit of shell script that will let me loop round all the sub-directories in a directory (i.e. ignoring any ordinary files in that directory). Let's say I just want to echo the names of the sub-directories. This sounds like it should be pretty easy - but not for me, it isn't! All help... (4 Replies)
Discussion started by: cjhancock
4 Replies

6. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

7. Shell Programming and Scripting

Bash/AWK Newbie taking on more than he can chew.

A few questions: I'm trying to use Bash (although I'm not against using AWK) to try to accomplish a few things, but I'm stumped on a few points. I'm learning most of the basics quickly: but there are a few things I can't figure out. 1. I'm trying to count the number of .txt files in a... (3 Replies)
Discussion started by: Asylus
3 Replies

8. Shell Programming and Scripting

quick newbie bash question

I'm in .profile. I want to export a directory ( export MY_TOOL_HOME=/tools/my tool". Unfortunately for me, the directory I want to export to has a space in its name. So when I try to cd $MY_TOOL_HOME, i get a No such file or directory at the command line... thanks for the help (4 Replies)
Discussion started by: redsand9009
4 Replies

9. UNIX for Dummies Questions & Answers

Newbie loop help!

Hi, I'm trying create a loop from a txt file and set variables from its contents. i.e. txt file contains three entries 'command1,command2' I want to loop the txt file and set variable1 as command1 etc etc. (1 Reply)
Discussion started by: pburge
1 Replies

10. Shell Programming and Scripting

Newbie - Need help in bash scripting

Hi there, I am a student and currently working on a project. I have a file that contains about 50 filenames. (1.txt, 2.txt, 3.txt ...). I would like to know how can I store these filenames into a variable using a loop? I would appreciate if anyone can help me. Thank You. Regards, Bib (4 Replies)
Discussion started by: coolbib
4 Replies
Login or Register to Ask a Question