I have a file with the following content:
--------------------
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.
$ 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.
PikK45
November 20, 2012, 2:25am
3
why not use "sed" ??
sed 's/SQL>//g' file > newfile
rm file
mv newfile file
pamu
November 20, 2012, 2:27am
4
grep -v "^SQL>" file > temp_file
OR
awk '!/^SQL>/' file > temp_file
then move this file to original file
mv temp_file file
Thanks a ton guys..it was really helpful.
I do have another challange. My file abc.log has the content:
-----------
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
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
pamu
November 20, 2012, 4:24am
7
And Please use code tags for code and data samples.
adopting subramanian's solution
use
head -2 file;tail -3 file
OR
awk 'NR<3
{A[NR]=$0}END{for(i=(NR-2);i<=NR;i++){print A}}' file
awk 'BEGIN{i=1}NR<3{print}{a[i%3]=$0;i++}END{printf("%s\n%s\n%s\n",a[(i-3)%3],a[(i-2)%3],a[(i-1)%3]);print i}' input.txt
RudiC
November 20, 2012, 5:02am
9
You may want to do it all in one go, remove SQL lines, print the header, and print n last lines:
awk '!/^SQL/&&!/^ *$/ {Ar[++i]=$0}
/Table/,/--*/
END{for (j=i-nrln+1;j<=i;j++) print Ar[j]}
' nrln=3 file
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