bash adding multiple names


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash adding multiple names
# 1  
Old 10-24-2010
bash adding multiple names

Hi all,
I make this bash script in which there is one option to add a name in txt file. So when I run the script it ask me for the name. When i enter the name, that name gets added in a txt file. If i add the same name again, the message comes up, the name is already there

My question is.. if i want to add same name multiple times, how can i do that, and when i delete the name only one name gets deleted

for example

in txt file there is a name sam, now what i want is when i enter same again in the script, in txt file it should show up as 2 sam or 3 sam and so on. when i delete same in script it delete one same. if 2 sam are there after using delte in txt file it should have 1 sam.

Thanks
# 2  
Old 10-24-2010
This can easily be adapted to your needs

Code:
#!/usr/bin/bash
LIST=/tmp/list
while [[ "$c" != "2" ]]
do
    echo "1 - Enter a name
    2 - display $LIST
     3 - Exit"
    echo "Make your choice 1/2/3: "
    read c
    case $c in
    1)
        echo "Enter the name :"
        read a
        if ( ! grep ^"$a"$ $LIST >/dev/null 2>&1 )
        then
            echo "$a" >>$LIST
             echo "$a has been added to $LIST"
        else
            echo "filename $a already existing in $LIST, wanna remove it from list y/[n]?"
            read q
            case $q in
                 y|Y) grep -v ^"$a"$ $LIST >$LIST.tmp ; mv $LIST.tmp $LIST ; echo "$a removed from $LIST" ;;
                 n|N) echo "ok $a will stay in $LIST" ;;
                 *) echo "wrong choice, $LIST will stay as is" ;;
            esac
        fi
    ;;
    2) cat $LIST ;;
    3) exit 0 ;;
    *) echo "Wrong choice, try again..." ;;
    esac
done


---------- Post updated at 01:25 AM ---------- Previous update was at 12:54 AM ----------


Could modify the code and add something like and call remove_once "$a"

Code:
remove_once(){
flag=0
while read a
do if [ "$a" = "$1" ]
        then
            if [ $flag -eq 0 ]
            then
                 echo "$a" >>$LIST.tmp
                 let flag++
            fi
        else
             echo "$a" >>$LIST.tmp
        fi
done <$LIST
mv $LIST.tmp $LIST
}


Last edited by ctsgnb; 10-25-2010 at 09:36 AM.. Reason: with indentation ... so Scruti will be happy ;)
# 3  
Old 10-25-2010
Thanks for reply..but now I am trying to do something else

now lets say i have

sam:john:son:$x

where $x is a number

so if i enter sam john son again the x become 2 and so on

now what i want is take $x and make it 2 if same name enter

if i enter same name 3rd time it becomes 3 and so on
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Legal and illegal bash variable names?

list of legal and illegal bash variable names and wht each is either illegal and ligal? 4. Seneca college, Toronto , Canada, peter wheeler, tech 154: (5 Replies)
Discussion started by: renegade755
5 Replies

2. Shell Programming and Scripting

Trying to do multiple dir's and multiple file names etc.

What am I missing? find: 0652-009 There is a missing conjunction find: 0652-009 There is a missing conjunction find: 0652-009 There is a missing conjunction find: 0652-009 There is a missing conjunction find: 0652-009 There is a missing conjunction find: 0652-009 There is a missing... (3 Replies)
Discussion started by: xgringo
3 Replies

3. Shell Programming and Scripting

change multiple file names

Hi is it possible to change multiple files (~10k) names with out disturbing the data in it. ? input Hynda|cgr10(+):100027702-1000312480|.txt Hynda|cgr10(+):100027702-1000312483|.txt Hynda|cgr10(+):100027702-1000312484|.txt Hynda|cgr10(+):100027702-1000312482|.txt output... (4 Replies)
Discussion started by: quincyjones
4 Replies

4. Shell Programming and Scripting

Change multiple file names

Hello, I have some files in a directory like: 01_07_2010_aa.txt 01_07_2010_bb.txt 01_07_2010_cc.txt 01_07_2010_dd.txt 01_07_2010_ee.txt 01_07_2010_ff.txt I want to change their names to : 3nm_aa.txt 3nm_bb.txt 3nm_cc.txt 3nm_dd.txt 3nm_ee.txt 3nm_ff.txt (8 Replies)
Discussion started by: ad23
8 Replies

5. Shell Programming and Scripting

Adding lines to files based on their names

I have several files on a folder something like this: If the file starts with 1a, I would like to add the following at the very beggining of the file If the file starts with 1b then I should add the following For files starting with 6 So on and so forth. Thus, at the end each file will... (18 Replies)
Discussion started by: Xterra
18 Replies

6. Shell Programming and Scripting

[bash help]Adding multiple lines of text into a specific spot into a text file

I am attempting to insert multiple lines of text into a specific place in a text file based on the lines above or below it. For example, Here is a portion of a zone file. IN NS ns1.domain.tld. IN NS ns2.domain.tld. IN ... (2 Replies)
Discussion started by: cdn_humbucker
2 Replies

7. UNIX for Dummies Questions & Answers

Finding Names in multiple files

What's the best way to see if a common name exists in two separate files? (3 Replies)
Discussion started by: Rally_Point
3 Replies

8. Shell Programming and Scripting

Parsing Directory Names for Use as Bash Variables

Hi all: I have a directory where all of the subdirectories are named by the convention "images_#1:#2_Date." My goal is to get an array for each subdirectory that has the structure (#1,#2, int). I am able to use awk to print each subdirectory's values, but cannot figure out how to get them into an... (6 Replies)
Discussion started by: aefskysa
6 Replies

9. Shell Programming and Scripting

Adding Multiple Lines to Multiple Files

Hello, I have three lines I need to add to hundreds of files. The files are all in the same format and contain the same information. I want to add the same information to the files. The new lines of information are similar. Here is an example of the three lines: line1: line2 =... (2 Replies)
Discussion started by: dayinthelife
2 Replies

10. Shell Programming and Scripting

Bash Script duplicate file names

I am trying to write a housekeeping bash script. Part of it involves searching all of my attached storage media for photographs and moving them into a single directory. The problem occurs when files have duplicate names, obviously a file called 001.jpg will get overwritten with another file... (6 Replies)
Discussion started by: stumpyuk
6 Replies
Login or Register to Ask a Question