shell script to add input at certain location into a file

Hi,

I am using Solaris 10 OS and trying to create shell script that can add input at certain location into a file.

The input that I am trying to put is new domain name
e.g
@newdomain.com

the file contains,

more test01.out
user/zzzz786@st.com/INBOX
user/zzzz@po.com/INBOX
user/zzzzaaaa/INBOX
user/zzzzry@blo.com/INBOX
user/zzzzz@syx.com/INBOX
user/zzzzzr@strex.com/INBOX
user/zzzzzz/INBOX
user/zzzzzz16@stre.com/INBOX
user/zzzzzz7@bluo.com/INBOX
user/zzzzzzz/INBOX
user/zzzzzzz@bppo.com/INBOX

output should be,

user/zzzz786@st.com/INBOX
user/zzzz@po.com/INBOX
user/zzzzaaaa@newdomain.com/INBOX
user/zzzzry@blo.com/INBOX
user/zzzzz@syx.com/INBOX
user/zzzzzr@strex.com/INBOX
user/zzzzzz@newdomain.com/INBOX
user/zzzzzz16@stre.com/INBOX
user/zzzzzz7@bluo.com/INBOX
user/zzzzzzz@newdomain.com/INBOX
user/zzzzzzz@bppo.com/INBOX

thank you for your kindly help

Try (not tested I'm on windows now):

sed '/@/! s|/INBOX|@newdomain.com/INBOX|'
1 Like
nawk -F"/" '{if($0!~/@/){print $1"/"$2"@newdomain.com/"$3}else{print $0}}' test01.out
1 Like

tested and working just fine. thanks for your help, any tips guide to construct such code guys? man I have no idea where to start

hmm... start from the basic unix commands and read about the control statements and some basic tutorials and examples.. ( just google it )
search this forum for "new to unix".. you see lot of threads and lot of suggestions from people.

1 Like

thx for the recommendation. I will look into it asap.

anyway got another question,

how can we remove certain values in the files?
a)one by one.
b)multiple values
-need separate script

e.g base on this file structure,

more file01.out
user/zzzz786@st.com/INBOX 
user/zzzz@po.com/INBOX 
user/zzzzaaaa@newdomain.com/INBOX 
user/zzzzry@blo.com/INBOX 
user/zzzzz@syx.com/INBOX 
user/zzzzzr@strex.com/INBOX 
user/zzzzzz@newdomain.com/INBOX 
user/zzzzzz16@stre.com/INBOX 
user/zzzzzz7@bluo.com/INBOX 
user/zzzzzzz@newdomain.com/INBOX 
user/zzzzzzz@bppo.com/INBOX

e.g file after removed "user/zzzzry@blo.com/INBOX"

more file01.out
user/zzzz786@st.com/INBOX 
user/zzzz@po.com/INBOX 
user/zzzzaaaa@newdomain.com/INBOX 
user/zzzzz@syx.com/INBOX 
user/zzzzzr@strex.com/INBOX 
user/zzzzzz@newdomain.com/INBOX 
user/zzzzzz16@stre.com/INBOX 
user/zzzzzz7@bluo.com/INBOX 
user/zzzzzzz@newdomain.com/INBOX 
user/zzzzzzz@bppo.com/INBOX

please use

 tag, otherwise it is really hard to read it

---------- Post updated at 01:41 PM ---------- Previous update was at 01:23 PM ----------

put the patterns inside a text file ( for multiple patterns - line by line )
 
while read pattern
do
     sed -i 's/.*"$pattern".*//' file01.out         # sed -i will replace the contents
done < patternFile
1 Like

thx its works

hi I got error when run this command,