Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
Hi everyone, I was wondering if anyone can help me modify my shell script. It's basically an enquiry tool which is used to get details of any UK railway station. ...
  1. #1
    Just Joined!
    Join Date
    Oct 2008
    Location
    Edinburgh, Scotland
    Posts
    18

    Cool Script to get data from CSV file(s)

    Hi everyone,

    I was wondering if anyone can help me modify my shell script.

    It's basically an enquiry tool which is used to get details of any UK railway station.

    Currently I input a 3 letter code and it calls up another shell script (the station info), which in turn reverts back to my script. This means I need to create over 2,500 scripts for each station.

    rather than that I have 2 csv files which contain all the data that I need, but haven't got a clue how to integrate them. After having Googled for a few hours I've noticed that awk and IFS look the most likely options to use.

    What I want to achieve is after typed in the input i want the script to determine whether it's 1.) a 3 character (letters only) input, or 2) a 4 character (numbers or alphanumeric) input. If its 1. then file1 gets parsed, if 2 then file2 gets parsed. The input is the first entry in the CSV files. After the files are parsed I want the rest of the entries on each row to populate various fields.

    Here is my existing code:

    Code:
    #!/bin/sh 
    clear
    
    
    echo
    echo
    echo
    echo -e '\E[37;44m				'"\033[1m Welcome to StationMaster \033[0m" #The 1 enables bold text
    echo
    echo "				         Version 1.0 "
    echo
    echo "				   All Rights Reserved"
    echo "				 ========================"
    echo
    echo "				   For help type HELP"
    echo
    echo
    echo
    echo
    echo
    echo
    echo
    echo
    echo -n "Enter Station Code or NLC: " 
    
    
    
    
    read FILE 
    FILE=${FILE}".sh" 
    if [ -f ${FILE} ]; then 
        ./${FILE} 
    else
    	cd NLC
       if [ -a ${FILE} ]; then 
        ./${FILE} 
        else
        cd ..
    	
        echo "Station not found." 
        fi
    fi 
    
    
    #Pause function
    
    function pause(){ 
       read -n 1 -p "$*" 
       echo 
    } 
    pause 'Press any key to continue...'
    ./sm4.sh

  2. #2
    Just Joined! sathiya's Avatar
    Join Date
    Feb 2008
    Location
    Bangalore, India
    Posts
    97
    You can use perl scripts which can do it simply...

    If you dont know how to write perl program, explain the requirement better, somebody else can do for you.

    But for running that, you would wanted a perl interpreter.

  3. #3
    Just Joined!
    Join Date
    Oct 2008
    Location
    Edinburgh, Scotland
    Posts
    18

    Exclamation

    basically what I want to achieve is:

    1. Run my script, a "data entry" screen prompt appears.
    2. Enter either a) a 3 letter code or b) a 4 digit number.
    3. The script determines what has been entered then it searches the relevant csv file.
    4. The if the search finds a match (i.e. the input matches the first column on the relevant csv file) it brings up the data for that station (the entries on the remeinder of the row in the file), if no match is found it returns an error.
    5. After either outcome a pause function is run then returns to 1.

    e.g.

    sample from file 1:

    EDB, Edinburgh, EDB, 9856, First ScotRail,
    sample from file 2:

    9856, Edinburgh, EDB, 9856, First ScotRail,
    At the Prompt I enter either EDB or 9856

    The relevant file is searched

    It returns:

    Station Name: Edinburgh
    CRS Code: EDB
    NLC: 9856
    Operator: First ScoRail

    can anyone help?

  4. #4
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    I don't see why you need two files in the first place. The data seems to be the same in both cases.

  5. #5
    Just Joined!
    Join Date
    Oct 2008
    Location
    Edinburgh, Scotland
    Posts
    18
    I was thinking it may be faster to use 2 files as using one file there would be more data to parse.

    If I only need to use one file, can anyone advise me how I go about my script?

    Regards

  6. #6
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    Try something along these lines:

    Code:
    awk -v KEY=/EDB/ 'KEY { print "Station Name: " $2 }' file1
    Oh, and of course, "All rights reserved."

  7. #7
    Just Joined!
    Join Date
    Oct 2008
    Location
    Edinburgh, Scotland
    Posts
    18
    I assume that the first instance of KEY is the input I type in?

    In the example I gave only 1 line of my csv file the input could be any 3 letter code or any 4 digit number.

    would that work?

  8. #8
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    Quote Originally Posted by Inbhir Pheofharain View Post
    I assume that the first instance of KEY is the input I type in?

    In the example I gave only 1 line of my csv file the input could be any 3 letter code or any 4 digit number.

    would that work?
    That is correct. My simple example will work as long as the key does not occur in any other line. I don't know whether that is the case with regard to your data. The following might be more appropriate:

    Code:
    awk -F", *" -v KEY=EDB '$3 == KEY { print whatever; }'
    This uses ", *" as a field separator and then checks whether field number three equals KEY (your input). If so, the code enclosed in braces can format and print the record.

  9. #9
    Just Joined!
    Join Date
    Oct 2008
    Location
    Edinburgh, Scotland
    Posts
    18
    Ah I'm with you now.

    My input will vary as it can be any 3 letter code or any 4 digit number not just 1 set value.

    Basically the csv file is a collection of records and the first entry on each row is the 'input', after this is inputted I want the rest of the row displayed on the screen under various headings. So I would type EDB or 9856 if I wanted to view details about Edinburgh, but the input would be different if I wanted to view details about Glasgow or Aberdeen etc.

  10. #10
    Just Joined!
    Join Date
    Oct 2008
    Posts
    10
    while [ true ]
    do
    echo "Enter Station:"
    read input
    length=${#input}
    if [ $length -eq 3 ]
    then
    SN=$(grep $input data1 | cut -d, -f2)
    CRS=$(grep $input data1 | cut -d, -f3)
    NLC=$(grep $input data1 | cut -d, -f4)
    OP=$(grep $input data1 | cut -d, -f5)
    else
    SN=$(grep $input data2 | cut -d, -f2)
    CRS=$(grep $input data2 | cut -d, -f3)
    NLC=$(grep $input data2 | cut -d, -f4)
    OP=$(grep $input data2 | cut -d, -f5)
    fi
    echo "Station Name: $SN"
    echo "CRS Code: $CRS"
    echo "NLC: $NLC"
    echo "Operator: $OP"
    echo; echo
    done

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...