Page 1 of 4 123 ... LastLast
Results 1 to 10 of 36

Thread: Searching a string, output it to an array then delete it based on user input

  1. #1
    Join Date
    Apr 2013
    Beans
    23

    Searching a string, output it to an array then delete it based on user input

    Hey,

    So I'm developing a simple Address book and its going well.
    It uses a singular text file to store all its information.

    At the minute i can search the file for a specific string, not a problem.

    I'm pretty sure i can get it to delete a string based on a users input, thats kind of easy but i thought for extra snazziness it would be
    a neat idea if I could grab the output of the search term - a multi-line output say - and store each new line into an array, each assigned a number.
    From there the user could specify they would like to delete the line based on their input.

    So far this is the 'delete' script:

    Code:
    ADDRESSBOOK=~/Documents/Address-Book/add_data.txt
    
    export ADDRESSBOOK
    
    echo "=============================== Delete a Record ==============================="
    echo "==============================================================================="
    echo "============ Search A Record, then delete it! Type 'exit' to exit =============
    
    "
    
    exit=0
    search(){
    while [ $exit -ne 1 ]
            do
            
            echo "Find something for me to delete"
            echo -n "-----------------------> "
    
            read searchTerm
        if [ "$searchTerm" = "exit" ]
        then 
            exit=1
        else
    
            searchOutput=$(grep $searchTerm*.$ $ADDRESSBOOK | sort)
    
            echo "$searchOutput"
        fi
        done
    
    exit 0
    }
    
    search
    The address book automatically adds each record with /n so it displays each record on a new line anyway.

    Is it possible to take the $searchRecord variable and pass that to an array?

    And before I go wrapping my head around arrays, is this even the best approach to take for what I'm trying to do?
    Last edited by fr0s7y; April 13th, 2013 at 05:32 PM. Reason: Solved!!

  2. #2
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: [Bash] Searching a string, output it to an array then delete it based on user inp

    Something like
    Code:
    while
      read -p 'Find something for me to delete> ' searchTerm
      [[ $searchTerm != 'exit' ]]
    do
      ifs=$IFS; IFS=$'\n'; searchOutput=( $(grep "$searchTerm" "$ADDRESSBOOK"| sort) ); IFS=$ifs
      if ((${#searchOutput[@]}>1)); then
        PS3="Select line to delete: "
        select line in "${searchOutput[@]}"; do
          [[ -z $line ]] && break
          echo Deleting $line
        done
      else
        echo Deleting $searchOutput
      fi
    done
    Last edited by schragge; April 13th, 2013 at 04:41 PM.

  3. #3
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: [Bash] Searching a string, output it to an array then delete it based on user inp

    arrays are very useful, so it's not like you waste your time even if they won't help here (though they should, as they eliminate the need of parsing strings by hand on every turn)

    example

    Code:
    $ array=()
    $ while read -r line; do   array+=( "$line" ); done < <( echo $'wut\nomg\na bc\ndef\ng h\ni' | sort )
    $ printf "'%s'\n" "${array[@]}"
    'a bc'
    'def'
    'g h'
    'i'
    'omg'
    'wut'
    $ select s in "${array[@]}"; do echo "selected: '$s'"; break; done
    1) a bc
    2) def
    3) g h
    4) i
    5) omg
    6) wut
    #? 5
    selected: 'omg'
    example is one-linerish because it was typed directly in terminal, obviously in script this should have nice indentation
    echo is an equivalent of your grep

    removing element from array can be done with unset array[n] so you need to loop over the array, find proper index and parametrize unset

    Code:
    array=( "ab" "c d" "e" )
    search="e"
    $ for(( i=0; i<${#array[@]}; i++ )); do [ "$search" = "${array[$i]}" ] && echo $i && break; done
    2
    $ unset array[$i]
    $ printf "'%s'\n" "${array[@]}"
    'ab'
    'c d'
    Last edited by Vaphell; April 13th, 2013 at 04:45 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  4. #4
    Join Date
    Apr 2013
    Beans
    23

    Re: [Bash] Searching a string, output it to an array then delete it based on user inp

    ok so, there were a few syntax bits, unexpected '(' and the like as i integrated the statement into the existing code.

    Here is the code at the minute:

    Code:
    while [ $exit -ne 1 ]
            do
            
            echo "Find something for me to delete"
            echo -n "-----------------------> "
    
            read searchTerm
            ifs=$IFS; IFS=$'\n'; searchOutput=$(grep $searchTerm*.$ $ADDRESSBOOK | sort); IFS=$ifs
            if [[ ${#searchOutput[@]} gt 1 ]]; then
             PS3="Select line to delete: "
             select line in "${searchOutput[@]}";
               [[ -z $line ]] && break
               echo Deleting $line
    
            else
                  echo Deleting $searchOutput
            fi
        done
    
    exit 0
    which is giving me:

    Code:
    Find something for me to delete
    -----------------------> a
    grep: /home/fr0s7y/Docume: No such file or directory
    grep: ts/Address-Book/add_data.txt: No such file or directory
    ./delete.sh: 30: ./delete.sh: [[: not found
    Deleting
    strange how grep is cutting off the last couple of digits from documents, also I dont understand where the file path for 'ts/' is being declared :/

  5. #5
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: [Bash] Searching a string, output it to an array then delete it based on user inp

    I've updated my post, it now includes the full while loop. BTW are you using bash? Looks like you're trying to execute it with /bin/sh
    Last edited by schragge; April 13th, 2013 at 04:48 PM.

  6. #6
    Join Date
    Apr 2013
    Beans
    23

    Re: [Bash] Searching a string, output it to an array then delete it based on user inp

    is the first line array=() intended to be an empty array? all examples like to populate it with array=(num1, num2, num3) etc.

    bash is telling me that '(' is unexpected on that line

  7. #7
    Join Date
    Apr 2013
    Beans
    23

    Re: [Bash] Searching a string, output it to an array then delete it based on user inp

    Quote Originally Posted by schragge View Post
    I've updated my post, it's now includes the full while loop. BTW are you using bash? Looks like you're trying to execute it with /bin/sh
    sorry yeah, its a .sh script, would that be why theres an error in the file path?

  8. #8
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: [Bash] Searching a string, output it to an array then delete it based on user inp

    yes, it's empty because i want to populate it with += operator (add elem(s)) inside while read loop for each line. I updated my previous post with how to remove elements from array

    Code:
    $ array=()
    $ printf "%s\n" "${array[@]}"
    
    $ array+=( 'a' )
    $ printf "%s\n" "${array[@]}"
    a
    $ array+=( 'b' 'c' 'd' )
    $ printf "%s\n" "${array[@]}"
    a
    b
    c
    d

    array is a bash feature so you either need to run the script with bash script or explicitly declare bash in the very first line of the script: #!/bin/bash and run with ./script
    Last edited by Vaphell; April 13th, 2013 at 04:55 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  9. #9
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: [Bash] Searching a string, output it to an array then delete it based on user inp

    No matter what the extension is, it depends on the shebang line:
    Code:
    !#/bin/bash

  10. #10
    Join Date
    Apr 2013
    Beans
    23

    Re: [Bash] Searching a string, output it to an array then delete it based on user inp

    yeah the shebang is included at the top of the file like so :
    Code:
    #!/bin/bash -       
    #title           :delete.sh
    #description :This script will delete an entry in  the Address Book
    #date          :April 2013
    #version      :1.0
    #==============================================================================
    the script is part of a larger program as in i have a run.sh that handles the main menu etc. with different .sh files for different parts of the application, add, search, print etc.

    all .sh files are in the same directory, the address book is stored in the users documents.
    Last edited by fr0s7y; April 13th, 2013 at 05:00 PM.

Page 1 of 4 123 ... LastLast

Tags for this Thread

Bookmarks

Posting Permissions

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