read/writte/input data in file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting read/writte/input data in file.
# 1  
Old 06-22-2006
scripts that allow searches, edits, and deletes of your file

Hi,

how can i do to show when i do with scrip list entris, or list specific entry, it show 2 lines when i specify the entry to search...¿?
i paste the script:

Code:
# Global variables
film=/opt/etc/film/film.txt
export film

confirm()
{
  echo -en "$@"
  read ans
  ans=`echo $ans | tr '[a-z]' '[A-Z]'`
  if [ "$ans" == "Y" ]; then
    return 0
  else
    return 1
  fi
}

num_lines()
{
  grep -i "$@" $film|wc -l| awk '{ print $1 }'
}

find_lines()
{
  # Busca lineas de entrada $1
  res=-1
  if [ ! -z "$1" ]; then
    grep -i "$@" $film
    res=$?
  fi
  return $res
}

list_items()
{
  # Lista los resultados encontrados
  if [ "$#" -eq "0" ]; then
    echo -en "Buscar usuario: (en blanco muesta toda la lista): "
    read search
    if [ -z "$search" ]; then
      search="."
    fi
    echo
  else
    search="$@"
  fi
  find_lines "${search}" | while read i
  do
    echo "$i" | tr ':' '\t'
  done
  echo -en "Encontrados: "
  num_lines "$search"
}

add_item()
{
  echo
  echo
  echo
  echo "          NUEVA PELI"
  echo " COMPLETA TODOS LOS DATOS"
  echo "  - AÑO MES DIA DIRECTOR TIPO - "
  echo
  echo -en "Nombre: "
  read name
  find_lines "^${name}:"
  if [ `num_lines "^${name}:"` -ne "0" ]; then
    echo "El usuario, $name ya existe en el fichero."
    return
  fi
  echo -en "Año: "
  read "ano"
  echo -en "Director: "
  read filmaker
  echo -en "Genero:"
  read genre
  echo -en "Calificacion:"
  read calification

  # Confirmacion
  echo "${name} Password=${ano}
        ${calification}, ${filmaker}, ${genre}" >> $film
  echo
  echo
  echo -en "--> ALTA FILM"
  echo
  echo
}

locate_single_item()
{
  echo -en "Peli a buscar: "
  read search
  n=`num_lines "$search"`
  if [ -z "$n" ]; then
    n=0
  fi
  while [ "${n}" -ne "1" ]; do
    #list_items "$search"
    echo -en "${n} peli encontrada. Selecciona una "
    case "$n" in
      "0") echo "menos" ;;
      "*") echo "mas" ;;
    esac
    echo "especifica un patron de busqueda (q para volver al menu anterior): "
    read search
    if [ "$search" == "q" ]; then
      return 0
    fi
    n=`num_lines "$search"`
  done
  return `grep -in $search $film |cut -d":" -f1`
}

#remove_item()
#{
#  locate_single_item
#  search=`head -2 $film`
#  if [ -z "${search}" ]; then
#       return
#  fi
#  list_items "$search"
#  confirm "Borrar?"
#  if [ "$?" -eq "0" ]; then
#    grep -v "$search" $film > ${film}.tmp ; mv ${film}.tmp ${film}
#  else
#    echo "NO BORRADO"
#  fi
#}

edit_item()
{
  locate_single_item
  search=`head -$? $film | tail -1|tr ' ' '.'`
  if [ -z "${search}" ]; then
        return
  fi
  list_items "$search"
  thisline=`grep -i "$search" $film`
  oldname=`echo $thisline|cut -d"" -f1`
  oldano=`echo $thisline|cut -d"," -f2`
  oldfilmaker=`echo $thisline|cut -d"," -f3`
  oldcalification=`echo $thisline|cut -d"," -f4`
  oldgenre=`echo $thisline|cut -d"," -f5`

  echo "BUSCAR : $search"
  grep -v "$search" $film > ${film}.tmp ; mv ${film}.tmp ${film}
  echo -en "Pelicula [ $oldname ] "
  read name
  if [ -z "$name" ]; then
    name=$oldname
  fi
  find_lines "^${name}:"
  if [ `num_lines "^${name}:"` -ne "0" ]; then
    echo "La pelicula, $name esta en el fichero."
    return
  fi
  echo -en "ano [ $oldano ] "
  read ano
  if [ -z "$ano" ]; then
    ano=$oldano
  fi
  echo -en "Pelicula [ $oldfilmaker ] "
  read filmaker
  if [ -z "$filmaker" ]; then
    filmaker=$oldfilmaker
  fi
  echo "${name}:${ano}:${filmaker}" >> $film
}




Scripts I need that allow search, edit, and delete data from you file.


how can i do..??


thank your for your help.
please i need your help..!
cheers.

dorek Smilie

Last edited by dorek; 06-22-2006 at 12:57 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script does not read input file

Hi everyone, I have problems with this script. This script should check for a folder for each server in the list of the list.txt file. The script only checks the first host, and then exits, why? #!/bin/bash file='/etc/list.txt' while read line; do echo $line if ssh root@$line "stat /var >... (2 Replies)
Discussion started by: nashrik
2 Replies

2. Shell Programming and Scripting

In PErl script: need to read the data one file and generate multiple files based on the data

We have the data looks like below in a log file. I want to generat files based on the string between two hash(#) symbol like below Source: #ext1#test1.tale2 drop #ext1#test11.tale21 drop #ext1#test123.tale21 drop #ext2#test1.tale21 drop #ext2#test12.tale21 drop #ext3#test11.tale21 drop... (5 Replies)
Discussion started by: Sanjeev G
5 Replies

3. Shell Programming and Scripting

How to get the shell script to read the .txt file as an input/data?

i have written my shell script in notepad however i am struggling to pass the data file to be read to the script the data file is of .txt format. My target is to run the shell script from the terminal and pass 3 arguments e.g. polg@DESKTOP-BVPDC5C:~/CS1420/coursework$ bash valsplit.sh input.txt... (11 Replies)
Discussion started by: Gurdza32
11 Replies

4. Shell Programming and Scripting

Read input files and merge them in given order and write them to input one param or one file

Dear Friends, I am looking for a shell script to merge input files into one file .. here is my idea: 1st paramter would be outfile file (all input files content) read all input files and merge them to input param 1 ex: if I pass 6 file names to the script then 1st file name as output file... (4 Replies)
Discussion started by: hyd1234
4 Replies

5. Shell Programming and Scripting

Read user input, Encrypt the data and write to file

Hi, can some one help me how to encrypt and decrypt a file. AIM: reade user input, encrypt it and save it to file. while decryption read the encrypted file decrypt it and save the output in some variable. Example: consider we have Credentials.txt file with content username: password... (5 Replies)
Discussion started by: saichand1985
5 Replies

6. Shell Programming and Scripting

Read text file and use it as input

I need to take a text file that holds a bunch of data and run each the stuff in it as an input for the program. the file would hold stuff like this: thing1.awesomesite.com 80 123.456 thing2.awesomesite.com 80 789.098 thing3.awesomesite.com 80 765.432 ... Now I already know the... (1 Reply)
Discussion started by: shade917
1 Replies

7. AIX

Read input data within a specified period of time

Hi All, I am working on a script which requires an input from user within one 1 min. So if the user gives the required input within 1 min., it will execute on the basis of input provided by the user.Other wise it will execute on a default value(hard coded inside the script). Now, I am... (19 Replies)
Discussion started by: rajsharma
19 Replies

8. Shell Programming and Scripting

Need a script that will read from 2 input file

Hi Everyone. I am new in this scripting world. I need to know about script that I can use on linux/sun operating systems. I have 2 input file. File 1: Its has an header information and then data in 2 different columns. File 2 : It has data in 9 different columns/ I need an script that... (3 Replies)
Discussion started by: syahmed
3 Replies

9. Shell Programming and Scripting

Need script to take input from file, match on it in file 2 and input data

All, I am trying to figure out a script to run in windows that will allow me to match on First column in file1 to 8th Column in File2 then Insert file1 column2 to file2 column4 then create a new file. File1: 12345 Sam 12346 Bob 12347 Bill File2:... (1 Reply)
Discussion started by: darkoth
1 Replies

10. UNIX for Dummies Questions & Answers

read input file

echo "enter employee #:/c" read employee grep -w $employee /tmp/file.txt when it asked me employee #, i typed employee, worked fine. when it asked me employee #, i type ENTER, it just sit there. if someone type in NULL or ENTER key, i want to exit out. (2 Replies)
Discussion started by: tjmannonline
2 Replies
Login or Register to Ask a Question