Sponsored Content
Operating Systems Solaris Find and sed for an email address in Solaris 10 Post 302779255 by alister on Tuesday 12th of March 2013 12:14:16 PM
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
 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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. 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

7. 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

8. 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

9. 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
All times are GMT -4. The time now is 09:46 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy