Problem in reading file (bash)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem in reading file (bash)
# 1  
Old 04-18-2010
Problem in reading file (bash)

i get a name from user first name : last name, in this format. Now i am saving this to a file. what i want is, I do not want to save any name if I already have one entry o that same name..what should i do

for example
user give robert fernandez
this will save in file as robert:fernandez.
if user give same name in future i dont want tht to be saved in the file, instead it should give message that this name is already there.

Thanks
# 2  
Old 04-18-2010
listfile is the list of names
Code:
newname="robert:fernandez"
grep -q "$newname"   listfile

if [[ $? -eq 0   ]; then
  echo "not okay"
  # insert  code to handle an existing entry
else
  echo  "ok"
  # code to handle new name
fi

# 3  
Old 04-19-2010
Code:
#!/bin/bash
read -p "First name: " FN
read -p "Last name : " LN
NAME="${FN}:${LN}"
if grep -q "$NAME" listfile
then
  echo "$NAME already in file"
else
  echo "$NAME" >> listfile && echo "adding name to file"
fi


# 4  
Old 04-19-2010
Quote:
Originally Posted by frans
Code:
#!/bin/bash
read -p "First name: " FN
read -p "Last name : " LN
NAME="${FN}:${LN}"
if grep -q "$NAME" listfile
then
  echo "$NAME already in file"
else
  echo "$NAME" >> listfile && echo "adding name to file"
fi


thanks
# 5  
Old 04-19-2010
Quote:
Originally Posted by jim mcnamara
Code:
if [[ $? -eq 0   ]; then

Here is a square bracket open too much.
When one is removed, the code works perfectly.

Regards,
pc
# 6  
Old 04-19-2010
All of those grep invocations should be using -xF to ensure that a fixed-string full-line match occurs.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash Variable scope - while loop while reading from a file

Cope sample1: test.sh i=0 echo " Outside loop i = $i " while do i=$(( $i + 1)) echo "Inside loop i = $i " done echo " Out of loop i is : $i " When run output : Outside loop i = 0 Inside loop i = 1 Inside loop i = 2 Inside loop i = 3 Inside loop i = 4 Inside loop i = 5 Inside... (8 Replies)
Discussion started by: Adarshreddy01
8 Replies

2. Shell Programming and Scripting

Reading from file bash command

Hello, I have a file in the following format id sample platform R1 R2 gene1 gene2 gene3 1 abc llumina R1_001.fastq.gz R2_001.fastq.gz apoe prnpp asp 2 def llumina R1_001.fastq.gz R2_001.fastq.gz apoe prnpp 3 ghi llumina ... (3 Replies)
Discussion started by: nans
3 Replies

3. UNIX for Beginners Questions & Answers

Reading a file from a different directory in a Bash script

Hi all, Given here under a section of a script I am using. SIMDIR="/home/Ins/forces" cd $SIMDIR for file in `ls *.forces` do basename=`echo $file | sed 's/\.*$//'` extname=`echo $file | sed 's/*\(*\)\.\(.*\)/\2\1/'` echo "Processing file: "$basename python convert.py... (4 Replies)
Discussion started by: Theo Score
4 Replies

4. Shell Programming and Scripting

Reading a text file using bash

I've a file in linux with following text: ;ip address hostname put-location alt-put-location tftpserver 192.168.1.1 r01-lab1-net /mnt/nas1/fgbu/ /opt/fgbu/devicebackup 192.168.1.254Now I want to read these values and assign them to particular variables... (6 Replies)
Discussion started by: kashif.live
6 Replies

5. Shell Programming and Scripting

Bash: Reading a file and assigning variables from file

I have a file that has four values on each line and I'd like to give each column a variable name and then use those values in each step of a loop. In bash, I believe you could use a while loop to do this or possibly a cat command, but I am super new to programming and I'm having trouble decoding... (2 Replies)
Discussion started by: ccorder22
2 Replies

6. Shell Programming and Scripting

Help in reading a cv file in bash

Hi All, I am trying to read a .csv file which has some 6 columns. Eg: samp.csv one, two, three, four six, seven, eight, nine I used the following code, for line in `cat samp.csv` do echo "$line" done It displays every comma seperated values in each line like, one,... (1 Reply)
Discussion started by: johnwilliams.sp
1 Replies

7. Shell Programming and Scripting

Problem in reading a file

Hi Guys, I am having a file which does not have any name when i do a ls -l -rw-r--r-- 1 dctrdat1 dctrdata 35 Feb 09 08:04 -rw-r--r-- 1 dctrdat1 dctrdata 11961 Feb 08 06:40 DAI_data.txt Now i want to see what is inside that file. Can you please let me know how to read... (9 Replies)
Discussion started by: mac4rfree
9 Replies

8. Shell Programming and Scripting

Bash: Reading out rows of a file into a dynamic array and check first literal

Hello, i have a file "Movie.ini" looking e.g. like follows * MOVIE A bla bla MOVIE B blubb blubb MOVIE C I'd like to read the file "Movie.ini" with cat and grep and check whether it includes the string MOVIE only with a '*' at the beginnig. By doing "cat Movie.ini| grep MOVIE... (14 Replies)
Discussion started by: ABE2202
14 Replies

9. Shell Programming and Scripting

bash: reading filenames from file

Hi, I'm trying to write a script that reads filenames from a file and use these filenames in a loop. The filenames are all on one line and the problem is that these filenames have wildcards like * and braces like in them. Right now what I'm doing is something like this: echo "reading from... (0 Replies)
Discussion started by: warp17
0 Replies

10. Shell Programming and Scripting

help me ...problem in reading a file

hi, while reading a file line by line # name of the script is scriptrd while read line do echo $line done while executing bash$ ./scriptrd if i give the input as * the output is like it displays the contents of the current directory i jus wanted it to print as * (6 Replies)
Discussion started by: brkavi_in
6 Replies
Login or Register to Ask a Question