Cutting specific lines from a file

Hi,

I have a file named Mani.txt. The contents are like this

cat Mani.txt
--------------------------------------------------------
Hi there how r u

My Name is Mani

Bye
------------------------------------------------------------
I want to cut the first and last lines from the file Mani.txt. I want the output file to contain only the line 'My Name is Mani'.

Please help me out. If , possible give me a snippet of the code

Thanx in advance

This should do it... but the script will work only for your mani.txt.

sed -e '/^My Name/!d mani.txt > output.txt

Vino

hi,
in sed -e '/^My Name/!d mani.txt > output.txt- where to close the quote

Sorry about that. :frowning:

sed -e '/^My Name/!d' mani.txt > output.txt

Vino

Hi,

Thanx. What should I do if want the first and last line of Mani.txt
I have a file called Log.txt. The contents of the file are
--------------------------------------------------------------------------

Creating Control File for sqlloader
Control file (data.ctl) Created for sqlloader
The execution of tool has started at Tue, May 31, 2005 03:45:37 AM
The User running the tool is DACSCAN
The present working directory is /usr/dacscan/toolbin
Temporary Table Created for Updating the Original Table
Temp Table have been created for storing the updated Records
Updation Successfully Completed on the Original Table
SQL> select * from spr1;

DOMAIN_NAME CKT COLOR SYS_UPDATE
-------------------- ------------------------------ ----- ----------
AREA_hickory 76L/37_690021/7JK1-711020/7JK1 R Y
AREA_hickory 76L/37_690021/7JK1-711020/7JK2 G N
AREA_aspen 76L/37_690021/7JK1-711020/7JK1 R Y

SQL> spool off;
Temp Table for storing updated records have been dropped
Temp Table created for updating the original Table is Dropped
All the Original Files have been moved to /dacscan/trace Folder
The execution of tool has finished execution at Tue, May 31, 2005 03:45:48 AM
The tools execution time is from Tue, May 31, 2005 03:45:37 AM to Tue, May 31, 2005 03:45:48 AM
------------------------------------------------------------------------

I want to remove the sentences SQL>select * from spr1 and
SQL> spool off;

What should I do

Thanx in advance

As I understand, you want to remove lines containing SQL.

This is what you do (using sed).

sed -e '/SQL/d' Log.txt > output.txt

It says for every line that is read, if you encounter SQL, delete the line. Whatever is not deleted gets into output.txt

For your Mani.txt,

tail -1 Mani.txt > Mani.txt.lastline 

gives you the last line.

Similarily

head -1 Mani.txt > Mani.txt.firstline

Vino

You told us you want everything except for the last and the first line, but presented us a 5-line file in your first post. Does this mean you want to skip all empty lines?

Supposing you want empty lines to go into your result (this would yield not only the line "my name is mani" in you first example, but also the two empty lines surrounding it) you could use:

# cat <file> | sed -n '1d; $d; p'

This will ignore the last and first line and print out everything else. In case you want to skip empty lines (lines containing only whitespace) too:

# cat <file> | sed -n '1d; $d; /^[<blank><tab>]*$/d; p'

To display the first/last line is trivial and could be done by sed too, but you have gotten a working solution already. To display the first nonblank line use:

# cat <file> | sed -n '/^[<blank><tab>]$/d; /^..$/ {; p; q;}'

You should be able to work out the solution for the last nonblank line now for yourself. "<blank>" and "<tab>" in the text above is to be replaced by literal blanks and tabs of course.

bakunin

I want to delete a next line as well after i have come across a matching text :

Say from a file like this:
--------------

  1. The conversation started and went on like this
    Hello Sir:
    How r you

Hello Sir:
I am fine.
----------------
then, I wantto delete two consecutive lines only like this:

Hello Sir:
How r you

&
Hello Sir:
I am fine.

My output should be:

  1. The conversation started and went on like this
sed -e '1d' -e '$d'

Hello all,

i m a new bie to shell scripting. i need to extract the lines for the file which contains logs for a month.

i need to make different files date wise.

please help..Thanks in advance.

How your log file looks like ?..

Pls post the input and output required and read Forum guidelines.

please find the logfile output.

67.228.247.196 img-ec2-test-2.zoomindev.com - [02/Jun/2009:19:15:53 -0400] "GET /79C703D3AC980D5D06B34CB43FB3BB5882247871CF1692F99177A60AA420AEE4/getkioskfileslist.cgi?f=2811484397.order HTTP/1.1" 200 42 "-" "-" 1 "-" "-" "-" "-" "-"
67.228.247.196 img-ec2-test-2.zoomindev.com - [02/Jun/2009:19:19:53 -0400] "GET /79C703D3AC980D5D06B34CB43FB3BB5882247871CF1692F99177A60AA420AEE4/getkioskfileslist.cgi?f=2811484397.order HTTP/1.1" 200 42 "-" "-" 0 "-" "-" "-" "-" "-"
67.228.247.196 img-ec2-test-2.zoomindev.com - [02/Jun/2009:19:23:53 -0400] "GET /79C703D3AC980D5D06B34CB43FB3BB5882247871CF1692F99177A60AA420AEE4/getkioskfileslist.cgi?f=2811484397.order HTTP/1.1" 200 42 "-" "-" 1 "-" "-" "-" "-" "-"
67.228.247.196 img-ec2-test-2.zoomindev.com - [02/Jun/2009:19:27:53 -0400] "GET /79C703D3AC980D5D06B34CB43FB3BB5882247871CF1692F99177A60AA420AEE4/getkioskfileslist.cgi?f=2811484397.order HTTP/1.1" 200 42 "-" "-" 0 "-" "-" "-" "-" "-"
67.228.247.196 img-ec2-test-2.zoomindev.com - [02/Jun/2009:19:31:53 -0400] "GET /79C703D3AC980D5D06B34CB43FB3BB5882247871CF1692F99177A60AA420AEE4/getkioskfileslist.cgi?f=2811484397.order HTTP/1.1" 200 42 "-" "-" 1 "-" "-" "-" "-" "-"
67.228.247.196 img-ec2-test-2.zoomindev.com - [02/Jun/2009:19:35:53 -0400] "GET /79C703D3AC980D5D06B34CB43FB3BB5882247871CF1692F99177A60AA420AEE4/getkioskfileslist.cgi?f=2811484397.order HTTP/1.1" 200 42 "-" "-" 1 "-" "-" "-" "-" "-"
67.228.247.196 img-ec2-test-2.zoomindev.com - [02/Jun/2009:19:39:53 -0400] "GET /79C703D3AC980D5D06B34CB43FB3BB5882247871CF1692F99177A60AA420AEE4/getkioskfileslist.cgi?f=2811484397.order HTTP/1.1" 200 42 "-" "-" 0 "-" "-" "-" "-" "-"
67.228.247.196 img-ec2-test-2.zoomindev.com - [02/Jun/2009:19:43:53 -0400] "GET /79C703D3AC980D5D06B34CB43FB3BB5882247871CF1692F99177A60AA420AEE4/getkioskfileslist.cgi?f=2811484397.order HTTP/1.1" 200 42 "-" "-" 1 "-" "-" "-" "-" "-"
67.228.247.196 img-ec2-test-2.zoomindev.com - [02/Jun/2009:19:47:53 -0400] "GET /79C703D3AC980D5D06B34CB43FB3BB5882247871CF1692F99177A60AA420AEE4/getkioskfileslist.cgi?f=2811484397.order HTTP/1.1" 200 42 "-" "-" 0 "-" "-" "-" "-" "-"
67.228.247.196 img-ec2-test-2.zoomindev.com - [02/Jun/2009:19:51:53 -0400] "GET /79C703D3AC980D5D06B34CB43FB3BB5882247871CF1692F99177A60AA420AEE4/getkioskfileslist.cgi?f=2811484397.order HTTP/1.1" 200 42 "-" "-" 0 "-" "-" "-" "-" "-"
67.228.247.196 img-ec2-test-2.zoomindev.com - [02/Jun/2009:19:55:53 -0400] "GET /79C703D3AC980D5D06B34CB43FB3BB5882247871CF1692F99177A60AA420AEE4/getkioskfileslist.cgi?f=2811484397.order HTTP/1.1" 200 42 "-" "-" 1 "-" "-" "-" "-" "-"
67.228.247.196 img-ec2-test-2.zoomindev.com - [02/Jun/2009:19:59:53 -0400] "GET /79C703D3AC980D5D06B34CB43FB3BB5882247871CF1692F99177A60AA420AEE4/getkioskfileslist.cgi?f=2811484397.order HTTP/1.1" 200 42 "-" "-" 0 "-" "-" "-" "-" "-"
67.228.247.196 img-ec2-test-2.zoomindev.com - [02/Jun/2009:20:03:53 -0400] "GET /79C703D3AC980D5D06B34CB43FB3BB5882247871CF1692F99177A60AA420AEE4/getkioskfileslist.cgi?f=2811484397.order HTTP/1.1" 200 42 "-" "-" 0 "-" "-" "-" "-" "-"

i need to make seperate file for each date which will contains logs for a specific date only.

Thanks in advance.

Assumption :

The syntax and format of the Log file wont change.

sed '/^$/d' rem.txt |awk '{dat=substr($4,2,2)"_"substr($4,5,3)"_"substr($4,9,4);print > dat }'

Hi,

Thanks for the reply dear...It works perfectly fine.
i would be really happy if you could explain the work around of this command.

Thanks,
Siddhesh

-----Post Update-----

Also need 1 help sir,

It will copy the contents from the file and make new files, but what if i want to move the related data to specific date from the logfile.

In your case rem.txt.

Thanks in advance.

sed '/^$/d' rem.txt |awk '{dat=substr($4,2,2)"_"substr($4,5,3)"_"substr($4,9,4);print > dat }'

where

sed '/^$/d' rem.txt 

remove the empty lines.

  dat=substr($4,2,2)"_"substr($4,5,3)"_"substr($4,9,4);

To get the date and month in the format dd_Mon_yyyy;

print > dat 

Print the lines to the corresponding date wise file.

Inline file editing of log file is not possible.

Thanks a lot expert..