shell script to edit the content of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell script to edit the content of a file
# 1  
Old 01-21-2008
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  
Old 01-22-2008
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  
Old 01-30-2008
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  
Old 01-31-2008
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Open and edit a file using a shell script

Hello Folks, I have a file named as date.dat present at /tmp/abc location which has following data - 20161030,20161031,20161101 I need to remove this line and replace it with something like below - $param1,$param2,$param3 Param1, Param2 and param3 stores the date based on some calculation in... (1 Reply)
Discussion started by: ektubbe
1 Replies

2. Homework & Coursework Questions

Edit the file in shell script

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I am trying to automate hadoop installation procedure using shell script. It involves go to perticular directory... (3 Replies)
Discussion started by: Abdul Navaz
3 Replies

3. Shell Programming and Scripting

Automating using shell script : edit the file in a directory

I am trying to automate hadoop installation procedure using shell script. It involves go to perticular directory and add some more lines to the file /etc/sysctl.conf. How this can be done? Regards Navaz (1 Reply)
Discussion started by: Abdul Navaz
1 Replies

4. Shell Programming and Scripting

shell script to edit a file

i have a file called number which contains data as 1 2 3 4 5 6 7 8 9 0 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 needed a shell script to print the output as 1 7 7 1 4 and (2 Replies)
Discussion started by: jacky29
2 Replies

5. Shell Programming and Scripting

Shell script to edit a file

Hello, I have a big file in wich I would like to rename inside this exactly the string '_ME' and not rename in case we have 'ABC_MELANGE'. Is there a way to do it by using a shell script? Any tip will be apreciated. The file is like described bellow, after using command more filename : ... (3 Replies)
Discussion started by: Titas
3 Replies

6. Shell Programming and Scripting

Script to Check & Edit Content of a file (Addition of comma in each lines of code)

Hi all, I need to write an automated bash shell script which performs such operations: 1. Grep the header of everyline with the initial of "T" in "FILE_A" 2. Perform a for loop, Count the numbers of comma in the line of code, if (no. of comma < 17) ADD the comma until 17; ... (2 Replies)
Discussion started by: big_nutz
2 Replies

7. Shell Programming and Scripting

Script to Edit the file content and create new file

I have a requirement, which is as follows *. Folder contains list of xmls. Script has to create new xml files by copying the existing one and renaming it by appending "_pre.xml" at the end. *. Each file has multiple <Name>fileName</Name> entry. The script has to find the first occurance of... (1 Reply)
Discussion started by: sudesh.ach
1 Replies

8. Shell Programming and Scripting

shell script to edit file and delete entry

Can anyone provide me a shell script to edit a xml file and delete one entry. To do manually i can edit(vi editor) the file and 'dd' will delete the file.But I wiluld to know if I can do with a script. Thanks in advance Tannu (6 Replies)
Discussion started by: tannu
6 Replies

9. Shell Programming and Scripting

Edit a config file using shell script

I need to edit a config file using shell script. i.e., Search with the 'key' string and edit the 'value'. For eg: below is what I have in the config file "configfile.cfg". Key1=OldValue1 Key2=OldValue2 I want to search for "Key1" and change "OldValue1" to "NewValue1" Thanks for your... (7 Replies)
Discussion started by: rajeshomallur
7 Replies

10. AIX

How to edit txt file by shell script?

What I want to do is just delete some lines from a text file, I know it's easy using copy and redirect function, but what I have to do is edit this file (delete the lines) directly, as new lines may be added to the text file during this period. Can AIX do this ? # cat text 1:line1 2:line2... (3 Replies)
Discussion started by: dupeng
3 Replies
Login or Register to Ask a Question