The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Edit a config file using shell script rajeshomallur Shell Programming and Scripting 7 05-02-2008 11:57 AM
shell script to search content of file with timestamps in the directory psychobeauty Shell Programming and Scripting 10 04-21-2008 02:37 AM
Urgent: selecting unique specific content of a file using shell script jisha Shell Programming and Scripting 2 01-08-2008 05:45 AM
How to edit txt file by shell script? dupeng AIX 3 09-29-2005 12:43 AM
Can we edit crontab using a shell script rudrarajumk Shell Programming and Scripting 3 03-31-2005 06:51 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-21-2008
Registered User
 

Join Date: Nov 2005
Posts: 22
shell script to edit the content of a file

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
Reply With Quote
Forum Sponsor
  #2  
Old 01-21-2008
dj -------
 

Join Date: Feb 2007
Location: Cochin/Bangalore, India
Posts: 439
One approch:

Code:
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
Reply With Quote
  #3  
Old 01-29-2008
Registered User
 

Join Date: Nov 2005
Posts: 22
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?

Code:
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
Reply With Quote
  #4  
Old 01-31-2008
Registered User
 

Join Date: Jun 2007
Location: Beijing China
Posts: 495
awk

Hi
try below one.

Code:
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[i]
}' filename
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:33 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0