Welcome to WebmasterWorld Guest from 54.221.119.45

Forum Moderators: bakedjake

Message Too Old, No Replies

Find and replace text in unix

     

haryanto

10:31 pm on Mar 9, 2004 (gmt 0)

10+ Year Member



Hi guys.

Say I have a file at /home/mysite/babes.txt

babes.txt contains:
Britney Spears
Jessica Simpson
Hilary Duff

How do I run a command to appeand to this text file without creating duplicate entries everytime I run it?

Say I want to add 'Anna Kournikova' to the list.

How can I run a command to appeand to it and even if I run it many times there will not be duplicate entries of Anna?

rogerdp

11:36 pm on Mar 9, 2004 (gmt 0)

10+ Year Member



Disclaimer: My experience with standard Unix tools is limited to the ports I use on other OSes.

Take a look at sort, which can do exactly what you want IIRC, and also sed, awk, or perl.

sugarkane

12:23 am on Mar 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In Bash?

There's prolly a fiendishly clever one-liner to do it, but this should work:

echo 'Anna Kournikova' >> /home/mysite/babes.txt ; cat /home/mysite/babes.txt sort uniq > /home/mysite/babes.txt

(replace the character with a pipe (solid vertical line))

SlowMove

12:30 am on Mar 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How can I run a command to appeand to it and even if I run it many times there will not be duplicate entries of Anna?

Just don't duplicate "Anna", "Anna Kournikova", the first name, or the first and last names of whatever is being appended? The code would be a little different depending on what exactly it is that you need to do?

haryanto

5:35 am on Mar 10, 2004 (gmt 0)

10+ Year Member



Hi Slowmove,

I mean don't duplicate anna kournikova.
I don't want a code that just append to the list like if I run a code to append to the list 5 time we are going to have

Britney Spears
Jessica Simpson
Hilary Duff
Anna Kournikova
Anna Kournikova
Anna Kournikova
Anna Kournikova
Anna Kournikova

Probably we can do a delete command first to delete Anna Kounikova if it exist, then we append to it,
This way we will not have too many Anna Kournikova in the list.

Sugarcane, does that code prevents duplicates?
Im going to try it anyway.

What do you think is the best solution guys?

danny

5:54 am on Mar 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



/home/mysite/mysite/babes.txt sort uniq > /home/mysite/babes.txt

You can't do this in one line -- it will clobber babes.txt before it finishes reading from it!

You want something like

sort <babes.txt uniq >temp; mv temp babes.txt

sugarkane

10:41 am on Mar 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about

(cat babes.txt; echo 'Anna Kournikova') sort uniq > babes.txt

SeanW

2:15 pm on Mar 10, 2004 (gmt 0)

10+ Year Member



A shell script will do it fairly easily in one line

append.sh:

egrep -q "^$1$" babes.txt ¦¦ echo "$1" >> babes.txt

Usage:

append.sh "Anna Kournikova"

You don't need the shell script, but it saves a lot of typing ;)

Sean

haryanto

4:11 am on Mar 13, 2004 (gmt 0)

10+ Year Member



I want to thank you Linux gods.
All the functions you guys provided works!
I tried each and every one. Thanks for the variations too!

I learnt a lot today.
That made me a happy boy!
OK, I will go build up my babes.txt now.

haryanto

4:53 am on Mar 13, 2004 (gmt 0)

10+ Year Member



Oh wait,

How do I modify the code so that it reads newbabes.txt and append the contents to babes.txt?

SeanW

5:59 am on Mar 13, 2004 (gmt 0)

10+ Year Member




egrep -q "^$1$" babes.txt echo "$1" >> babes.txt

The first "babes.txt" is what you're checking, the second is what you're appending to. So, if you wanted to check newbabes.txt and append to babes.txt, you'd have

egrep -q "^$1$" newbabes.txt echo "$1" >> babes.txt

Why you'd need to do that, I'm not sure, your original question wanted the same file, this solution doesn't need an intermediate file.

Sean

haryanto

7:13 am on Mar 14, 2004 (gmt 0)

10+ Year Member



Hi sean,

That is because if I have like 30 names to add I will just need to type them in a text file and run the linux command to merge them instead of entering the name one by one!

Anyway I will try out this new method!
Thanks!

SeanW

6:26 pm on Mar 14, 2004 (gmt 0)

10+ Year Member



Ah! Hrm, I'm not sure that'll work, then. Use append in the original mode (ie read/write from one file), then have a script called "merge":

while read A; do
append $A
done

And then

cat newbabes.txt ¦ merge

Sean

 

Featured Threads

Hot Threads This Week

Hot Threads This Month