Find and sed for an email address in Solaris 10


 
Thread Tools Search this Thread
Operating Systems Solaris Find and sed for an email address in Solaris 10
# 1  
Old 03-11-2013
Find and sed for an email address in Solaris 10

in Solaris 10 I am able to run:

Code:
find . -type f -name "copy*" exec grep example.com {} \;

and I get results.

but when I try to find and sed:

Code:
find . -type f -name "copy*" exec sed -e 's/user@example\.com/user2@example\.com' {} \;

the command executes correctly but doesn't change anything.

anyone got any ideas what I am doing wrong?
# 2  
Old 03-11-2013
Firstly, sed does not edit files in place

Secondly, those are different commands. The grep will match the word "example" followed by any character followed by "com". The sed regular expression will only match "user@example" followed by a dot followed by "com".

Apples and oranges.

Regards,
Alister
# 3  
Old 03-11-2013
obviously I have misunderstood the syntax

So let me ask the question a little differently.

How would one go about searching for files named "example*" in a particular directory and then
looking in those files for a string "user@example.com" and replace that string with "user2@example.com"?

Thanks for the reply.
# 4  
Old 03-12-2013
This might do what you are looking for:
Code:
find . -type f -name "example*" -print > list.txt
for file in `cat list.txt`; do
  sed -i "s/user@example\.com/user2@example.com/g" $file
done

If there are LOTS of files, the "cat list.txt" will overload the command line buffer. The above syntax should work unless LOTS of files. I don't remember the alternate syntax.

---------- Post updated at 11:16 PM ---------- Previous update was at 11:15 PM ----------

Oh, of course, if no -i option for your sed, redirect output to temp file and mv temp file to replace original file.
# 5  
Old 03-12-2013
Use perl!
Code:
find . -type f -name "copy*" exec perl -pe 's/user\@example\.com/user\@example.com/' {} \;

And, if looks okay, change to exec perl -i -pe ...
NB: in perl you always need the \@ escape.
A loop has some pitfalls:
Code:
find . -type f -name "copy*" -print |
while read file; do
 cp "$file" "$file".tmp &&
 sed 's/user@example\.com/user2@example.com/' "$file".tmp > "$file" &&
 rm "$file".tmp
done

NB: quote variables in arguments, do overwrite not (re)create files.
Some shells allow the even safer read -r ...

Last edited by MadeInGermany; 03-12-2013 at 08:18 AM..
# 6  
Old 03-12-2013
Here is one way to do it with the standard Solaris tools and avoiding the loop issues:
Code:
find . -type f -name "example*" -exec ksh -c 'sed "s/user@example.com/user2@example.com/g" "$1" > /tmp/sed.$$.out && mv /tmp/sed.$$.out "$1"' ksh {} \;


Last edited by jlliagre; 03-12-2013 at 07:26 PM.. Reason: some double quotes added
# 7  
Old 03-12-2013
Quote:
Originally Posted by os2mac
How would one go about searching for files named "example*" in a particular directory and then
looking in those files for a string "user@example.com" and replace that string with "user2@example.com"?
Well, if it's just one directory, you don't need find, as suggested by others. A for-loop and file globbing should suffice. For the editing task, ed will do nicely.
Code:
for fname in example*; do
    [ -f "$fname" ] || continue
    printf '%s\n' 'g/user@example\.com/s//user2@example.com/g' w q | ed -s "$fname"
done

If you are familiar with the nuances of the here-document, you may prefer this more readable version:
Code:
for fname in example*; do
    [ -f "$fname" ] || continue
    ed -s "$fname" <<-'EOED'
         g/user@example\.com/s//user2@example.com/g
         w
         q
    EOED
done

Note that no other characters (invisible or not) may occur on the line where "EOED" terminates the here-document, except leading tabs (not spaces).

Regards,
Alister

---------- Post updated at 12:14 PM ---------- Previous update was at 11:57 AM ----------

Quote:
Originally Posted by MadeInGermany
A loop has some pitfalls:
Code:
find . -type f -name "copy*" -print |
while read file; do
...<snip>...

Some shells allow the even safer read -r ...
Quote:
Originally Posted by jlliagre
Here is one way to do it with the standard Solaris tools and avoiding the loop issues:
There shouldn't be any pitfalls looping with a POSIX-compliant sh:
Code:
find ... | while IFS= read -r filename; do ...

Both IFS= and -r are necessary to read text verbatim. The former prevents leading and trailing whitespace from being discarded; the latter disables backslash escaping.

The only limitation that remains is the inability to read a filename with an embedded newline. In my opinion, such a filename is pathological and should be rejected with extreme prejudice. Smilie

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Forum Support Area for Unregistered Users & Account Problems

Cant use certain email address

I tried to re-register using my new email address which is <firstname>@<surname>.me But it never sent out the email confirmation. I had to hit the back button and use my gmail address instead and it came through instantly. Is there a problem with using .me addresses? (1 Reply)
Discussion started by: frustin
1 Replies

2. Shell Programming and Scripting

SED With Regex to extract Email Address

Hi Folks, In my program, I have a variable which consists of multiple lines. i need to use each line as an input. My intention is to extract the email address of the user in each line and use it to process further. The email address could be anywhere in the whole line. But there will be only... (5 Replies)
Discussion started by: ragz_82
5 Replies

3. IP Networking

Tracing a MAC address to IP address: Solaris

Hi there I lost connectivity to one of our remote systems and when I checked the messages log I found the following: Aug 10 23:42:34 host xntpd: time reset (step) 1.681729 s Aug 16 13:20:51 host ip: WARNING: node "mac address" is using our IP address x.x.x.x on aggr1 Aug 16 13:20:51 host... (9 Replies)
Discussion started by: notreallyhere
9 Replies

4. Solaris

find and sed comand for Solaris 9

Hi, I'm looking to combine the two commands find and sed together for the work that i'm worknig on. I need to find the ip addresse in any file from current directory and below, once is found i need to replace it with new ip addresse. Here is what i have but not complete. Find IP from... (4 Replies)
Discussion started by: lamoul
4 Replies

5. Shell Programming and Scripting

Finding an email address

Hi all, I have a file with X number of lines. Somewhere within each line will be an email address following the format: <telephone_number>@domain.net Each line is not a set length and there are no delimiters to work with. I want to write a script that extracts the telephone number... (3 Replies)
Discussion started by: mandriver
3 Replies

6. UNIX for Dummies Questions & Answers

redirection to email address

Can someone please tell me the format to redirect an output file to an email address? thanks (4 Replies)
Discussion started by: 4Given
4 Replies

7. Post Here to Contact Site Administrators and Moderators

changing my email address

Hello, Is there a link somewhere to my profile where I can change my email address to my new work email address? I have a new job and would like to receive a notification of responses to my new work address - how can I get that address to you without posting it online for everyone to see? ... (1 Reply)
Discussion started by: tekster757
1 Replies

8. UNIX for Dummies Questions & Answers

Send email where # is in the email address - Using Unix

Hi All, How do I send an email using malix where email address contains a #. I have a email address like this : #test@test.com I want to send email like malix -s "TEST" #test@test.com < SOMEFILE I tried \# but doesn't work. Please let me know how we can achieve this? I am in... (1 Reply)
Discussion started by: jingi1234
1 Replies

9. Post Here to Contact Site Administrators and Moderators

i have removed my email address

I have removed my email address from the posted email. what else do I need to do? Thanks K-one (1 Reply)
Discussion started by: K-ONE
1 Replies
Login or Register to Ask a Question