The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages here.

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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-21-2008
tiger99 tiger99 is offline
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
  #2 (permalink)  
Old 01-22-2008
dennis.jacob dennis.jacob is offline Forum Advisor  
dj -------
  
 

Join Date: Feb 2007
Location: Singapore/Bangalore/Cochin
Posts: 560
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
  #3 (permalink)  
Old 01-30-2008
tiger99 tiger99 is offline
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
  #4 (permalink)  
Old 01-31-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,078
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:16 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0