![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
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
|
|||
| Google The UNIX and Linux Forums |