Hi
I need some help using shell script to edit a file.
My original file has the following format:
/txt/email/myemail.txt
/txt/email/myemail2.txt
/pdf/email/myemail.pdf
/pdf/email/myemail2.pdf
/doc/email/myemail.doc
/doc/email/myemail2.doc
I need to read each line. If the path is /txt/email/, it should change to /emailtxt/. Before display the result, there should be a heading #txt email. If the path is /doc/email/, it should change to /emaildoc/ with heading #doc email. The changed text should move to the beginning of the file. The rest of the document is untouched.
For example, the result of the above file should look like
#txt email
/txt/email/myemail.txt
/txt/email/myemail2.txt
#doc email
/doc/email/myemail.doc
/doc/email/myemail2.doc
/pdf/email/myemail.pdf
/pdf/email/myemail2.pdf
I am new to shell scripting and I hope people can give me some guidence/example on how to write a shell script to do the above.
Any input would be really appreciated
Thanks
One approch:
echo '#txt email' >>new_file
sed -e 's!/txt/email!/emailtxt!g' -e 's!/doc/email!/emaildoc!g' test_file | grep emailtxt >>new_file
echo '#doc email' >>new_file
sed -e 's!/txt/email!/emailtxt!g' -e 's!/doc/email!/emaildoc!g' test_file | grep emaildoc >>new_file
Thank you Dennis for the example
There is another substitution I need to do and I am having a lot of trouble getting it to work.
I need to check a file for any line that begins with /database/proc/ and ends with .proc. Then replace the beginning with /online/proc/
i.e.
/database/proc/hello.proc
/database/proc/abc.proc
becomes
/online/proc/hello.proc
/online/proc/abc.proc
I wrote the following script. However, it doesn't return anything.
Could someone please help me? Why isn't my code working?
sed -e '/database/proc/*.proc!/s!/database/proc!/online/proc!g' test_file | grep online/proc >> new_file
Thanks in advance for any advice/help
Hi
try below one.
nawk 'BEGIN{FS="\/"}
{
if ($2=="txt")
{
email[$2]=sprintf("%s\n%s",email[$2],$0)
}
else if($2=="doc")
{
email[$2]=sprintf("%s\n%s",email[$2],$0)
}
else
{
email[$2]=sprintf("%s\n%s",email[$2],$0)
}
}
END{
print "#txt email"
print email["txt"]
print ""
print "#doc email"
print email["doc"]
for (i in email)
if (i!="txt" && i!="doc")
print email
}' filename