Modifying the contents of a file and saving it


 
Thread Tools Search this Thread
Operating Systems Solaris Modifying the contents of a file and saving it
# 1  
Old 11-20-2012
Modifying the contents of a file and saving it

I have a file with the following content:
--------------------
Code:
SQL> @DBmonitor_WMHA_SQL_script.sql;
 
Tablespace                      Size (GB)  Free (GB)     % Free     % Used      
------------------------------ ---------- ---------- ---------- ----------      
WMHAIT_IS                               8          8        100          0      
WMHA_IM                               .98        .97         99          1      
WMHA_IS                               .98         .9         93          7      
WMHA_DS                             10.98        9.5         87         13      
WMHAPP_IS                              .1        .08         85         16      
WMHAPP_DS                            8.93       7.19         80         20      
WMHAIT_DS                              32      24.05         75         25      
WMHA_DM                               348      37.49         11         89      
SQL> 
SQL> EXIT;
 
 
 
SQL> @DBmonitor_WMOPT_SQL_script.sql;

------------

Question: I want to get rid of all 'SQL>' and re-saving it. Please help me with the command. I tried using tr command, but its disturbing other texts starting with 'S'..which I didnt expect.

Last edited by Scrutinizer; 11-20-2012 at 03:27 AM.. Reason: Code tags; removed spurious formatting
# 2  
Old 11-20-2012
Code:
 
$ sed 's,^SQL>,,' input.txt
 @DBmonitor_WMHA_SQL_script.sql;
Tablespace Size (GB) Free (GB) % Free % Used 
------------------------------ ---------- ---------- ---------- ---------- 
WMHAIT_IS 8 8 100 0 
WMHA_IM .98 .97 99 1 
WMHA_IS .98 .9 93 7 
WMHA_DS 10.98 9.5 87 13 
WMHAPP_IS .1 .08 85 16 
WMHAPP_DS 8.93 7.19 80 20 
WMHAIT_DS 32 24.05 75 25 
WMHA_DM 348 37.49 11 89 
 
 EXIT;

if your sed supports -i then use it like sed -i

Otherwise, redirect the output and store it in some other file.
# 3  
Old 11-20-2012
why not use "sed" ??

Code:
sed 's/SQL>//g' file > newfile
rm file
mv newfile file

# 4  
Old 11-20-2012
Code:
grep -v "^SQL>" file > temp_file

OR

awk '!/^SQL>/' file > temp_file

then move this file to original file

Code:
mv temp_file file

# 5  
Old 11-20-2012
Thanks a ton

Thanks a ton guys..it was really helpful.
I do have another challange. My file abc.log has the content:
-----------
Code:
Tablespace                      Size (GB)  Free (GB)     % Free     % Used      
------------------------------ ---------- ---------- ---------- ----------      
WMHAIT_IS                               8          8        100          0      
WMHA_IM                               .98        .97         99          1      
WMHA_IS                               .98         .9         93          7      
WMHA_DS                             10.98        9.5         87         13      
WMHAPP_IS                              .1        .08         85         16      
WMHAPP_DS                            8.93       7.19         80         20      
WMHAIT_DS                              32      24.05         75         25      
WMHA_DM                               348      37.49         11         89

Question: What command should I use to get the below output
(only the last 3 lines):

Tablespace Size (GB) Free (GB) % Free % Used
------------------------------ ---------- ---------- ---------- ------- WMHAPP_DS 8.93 7.19 80 20
WMHAIT_DS 32 24.05 75 25
WMHA_DM 348 37.49 11 89

Last edited by Avik Datta; 11-30-2012 at 12:00 PM..
# 6  
Old 11-20-2012
head -n +3 filename; tail -n -3 filename
-----------
Tablespace Size (GB) Free (GB) % Free % Used
------------------------------ ---------- ---------- ----------
WMHAPP_DS 8.93 7.19 80 20
WMHAIT_DS 32 24.05 75 25
WMHA_DM 348 37.49 11 89
# 7  
Old 11-20-2012
And Please use code tags for code and data samples.

adopting subramanian's solution

use

Code:
head -2 file;tail -3 file

OR

Code:
awk 'NR<3
{A[NR]=$0}END{for(i=(NR-2);i<=NR;i++){print A[i]}}' file


Last edited by pamu; 11-20-2012 at 05:31 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Modifying contents of the file in shell script

Hello all, I have a Kconfig file that looks like something below ... ================================ menu "Application type" config GUI_TYPE_STANDARD bool "Standard Application" source "cfg/config/std.in" source... (12 Replies)
Discussion started by: anand.shah
12 Replies

2. Shell Programming and Scripting

Folder contents getting appended as strings while redirecting file contents to a variable

Hi one of the output of the command is as below # sed -n "/CCM-ResourceHealthCheck:/,/---------/{/CCM-ResourceHealthCheck:/d;/---------/d;p;}" Automation.OutputZ$zoneCounter | sed 's/$/<br>/' Resource List : <br> *************************** 1. row ***************************<br> ... (2 Replies)
Discussion started by: vivek d r
2 Replies

3. Programming

[Solved] Removing duplicates from the file and saving as new file

Dear All I have 200 data files and each files has many duplicates. I am looking for the automated awk script such that it checks and removes the duplicates from the each file and saving them as new files for all 200 files in the respective folder. For example my data looks like this.. ... (12 Replies)
Discussion started by: bala06
12 Replies

4. Shell Programming and Scripting

Replace partial contents of file with contents read from other file

Hi, I am facing issue while reading data from a file in UNIX. my requirement is to compare two files and for the text pattern matching in the 1st file, replace the contents in second file by the contents of first file from start to the end and write the contents to thrid file. i am able to... (2 Replies)
Discussion started by: seeki
2 Replies

5. Shell Programming and Scripting

I want to delete the contents of a file which are matching with contents of other file

Hi, I want to delete the contents of a file which are matching with contents of other file in shell scripting. Ex. file1 sheel,sumit,1,2,3,4,5,6,7,8 sumit,rana,2,3,4,5,6,7,8,9 grade,pass,2,3,4,5,6,232,1,1 name,sur,33,1,4,12,3,5,6,8 sheel,pass,2,3,4,5,6,232,1,1 File2... (3 Replies)
Discussion started by: ranasheel2000
3 Replies

6. UNIX for Dummies Questions & Answers

compare 2 file contents , if same delete 2nd file contents

Give shell script....which takes two file names as input and compares the contents, is both are same delete second file's contents..... I try with "diff"...... but confusion how to use "diff" with if ---else Thanking you (5 Replies)
Discussion started by: krishnampkkm
5 Replies

7. Shell Programming and Scripting

Modifying a file?

Hi, I want to convert a file that looks like this >joe XXXXXXXXXXXXXXXXXXX >man BBBBBBBBBBBBBBBBBBBBBSSSSSSSS to something that looks like this (where the spacing is tab seperated) joe XXXXXXXXXXXXXXXXXXX man BBBBBBBBBBBBBBBBBBBBBSSSSSSSS I am able to do the reverse but the other... (3 Replies)
Discussion started by: kylle345
3 Replies

8. Shell Programming and Scripting

Retain file permissions when saving .sh file from internet [OS X]

Hello. I have written a bash script that I am sharing with an OS X community I am a member of. The purpose of the script is to execute a series of commands for members without them having to get involved with Terminal, as it can be daunting for those with no experience of it at all. I have renamed... (4 Replies)
Discussion started by: baza210
4 Replies

9. Shell Programming and Scripting

Modifying file from outside

hi , I have a javascript file like this: function fnValidateId(){ var array_id = new Array("444","7888","6633","555","146562","3333","33332") var tmp_id = document.form1.id.value; var num=0; while(1) { if(tmp_id ==... (9 Replies)
Discussion started by: Sreejith_VK
9 Replies

10. Shell Programming and Scripting

can someone help me with modifying this file

Hi, I have a file which has data kind of like this. Apr 13 08:20:38 uslis10a sendmail: m3DDKSx3006432: usliss26.ca.com Apr 13 08:20:38 uslis10b sendmail: m3DDKSoK006433: usliss26.ca.com Apr 13 08:20:38 uslis10b sendmail: m3DDKcSo006442: usliss26.ca.com Apr 13 08:20:38 uslis10c... (2 Replies)
Discussion started by: eamani_sun
2 Replies
Login or Register to Ask a Question