homepage Welcome to WebmasterWorld Guest from 54.197.15.196
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Hardware and OS Related Technologies / Linux, Unix, and *nix like Operating Systems
Forum Library, Charter, Moderators: bakedjake

Linux, Unix, and *nix like Operating Systems Forum

    
Find and replace text in unix
haryanto

10+ Year Member



 
Msg#: 991 posted 10:31 pm on Mar 9, 2004 (gmt 0)

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

10+ Year Member



 
Msg#: 991 posted 11:36 pm on Mar 9, 2004 (gmt 0)

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

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 991 posted 12:23 am on Mar 10, 2004 (gmt 0)

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

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 991 posted 12:30 am on Mar 10, 2004 (gmt 0)

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

10+ Year Member



 
Msg#: 991 posted 5:35 am on Mar 10, 2004 (gmt 0)

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

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 991 posted 5:54 am on Mar 10, 2004 (gmt 0)

/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

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 991 posted 10:41 am on Mar 10, 2004 (gmt 0)

How about

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

SeanW

10+ Year Member



 
Msg#: 991 posted 2:15 pm on Mar 10, 2004 (gmt 0)

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

10+ Year Member



 
Msg#: 991 posted 4:11 am on Mar 13, 2004 (gmt 0)

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

10+ Year Member



 
Msg#: 991 posted 4:53 am on Mar 13, 2004 (gmt 0)

Oh wait,

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

SeanW

10+ Year Member



 
Msg#: 991 posted 5:59 am on Mar 13, 2004 (gmt 0)


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

10+ Year Member



 
Msg#: 991 posted 7:13 am on Mar 14, 2004 (gmt 0)

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

10+ Year Member



 
Msg#: 991 posted 6:26 pm on Mar 14, 2004 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Hardware and OS Related Technologies / Linux, Unix, and *nix like Operating Systems
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved