0

I have a text file:

a b n
d f h 
e f y

I want to edit it and make it like:

a [email protected] n
d [email protected] h 
e [email protected] y

How can I do this? Is there any command which can help?

3 Answers 3

5

Try this awk one liner:

$ awk '$2=$2"@gmail.com"' file
a [email protected] n
d [email protected] h
e [email protected] y
Sign up to request clarification or add additional context in comments.

1 Comment

+1 The funny thing is some people will look at how simple this is and how complicated the sed alternative is and then choose the sed command anyway because some versions of sed support -i. Sigh.... Anyway - newer versions of gawk support -i inedit to change the original file too if that's a consideration.
2

Using sed inline:

sed -i.bak 's/\(^[^ ]* *\)\([^ ]*\)\(.*\)$/\1\[email protected]\3/' file

This will save the changes in the original file itself.

4 Comments

please explain the regular expression
[^ ]* means match anything except a space and .* means any 0 or more length text.
sed -r 's/^\S+\s+\S+/&@gmail.com/' file is more readable and robust (GNU sed).
This does not make the changes in the original file. It creates a new file and then gives it the same name as the original. This is a significant distinction if the original file has other links. It is also not valid in all implementations of sed.
0

This might work for you (GNU sed):

sed -i 's/\S\+/&@gmail.com/2' file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.