Cutting specific lines from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cutting specific lines from a file
# 1  
Old 06-02-2005
Error 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
# 2  
Old 06-02-2005
This should do it... but the script will work only for your mani.txt.

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

Vino
# 3  
Old 06-02-2005
hi,
in sed -e '/^My Name/!d mani.txt > output.txt- where to close the quote
# 4  
Old 06-02-2005
Sorry about that. Smilie

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

Vino
# 5  
Old 06-02-2005
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
# 6  
Old 06-02-2005
As I understand, you want to remove lines containing SQL.

This is what you do (using sed).

Code:
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,

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

gives you the last line.

Similarily

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

Vino
# 7  
Old 06-02-2005
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Cutting specific columns from lines

I am trying to remove columns 81-97 from a line that can be as long as 114 characters. Because a number of lines might not have under 80 characters, using the cut command following by paste could be a problem. While sed might work, is there some other utility that could do this more easily? ... (9 Replies)
Discussion started by: wbport
9 Replies

2. UNIX for Dummies Questions & Answers

Quick UNIX command to display specific lines in the middle of a file from/to specific word

This could be a really dummy question. I have a log text file. What unix command to extract line from specific string to another specific string. Is it something similar to?: more +/"string" file_name Thanks (4 Replies)
Discussion started by: aku
4 Replies

3. Shell Programming and Scripting

Cutting rows at specific length

Hi, i have a file containing nrows and 3cols. i want to cut it in specific length and save output to individual files. 1 2 3 4 5 6 5 8 9 10 11 12 13 14 15 16 17 18 i need to cut the file say every 2 rows and save it in individual file. 01.dat contains 1 2 3 4 5 6 02.dat 7 8 9... (10 Replies)
Discussion started by: ida1215
10 Replies

4. Shell Programming and Scripting

Cutting out text from specific portion on filename

Hi, how do I go about cutting out the first numeric characters after the word "access"? access1005101228.merged-00.15.17.86.d8.b8.log.gz (16 Replies)
Discussion started by: GermanJulian
16 Replies

5. Shell Programming and Scripting

Cutting specific line of a file by checking condition

testfile.csv 0","1125209",,"689202CBx18888",,"49",,,"NONMC",,,,,"01112010",,,,,,,"MTM- "1","",,"689202ABx19005",,"49",,,"NONMC",,,,,"01072010",,,,,,,"MTM- testfile.csv looks like above format if the second column is null then get 23rd column and store in a different varible .. add all the... (1 Reply)
Discussion started by: mgant
1 Replies

6. Shell Programming and Scripting

cutting lines

Dear All, Is there a way to cut the lines that have been "head" Here is what i m trying to do Please advice there is file name dummy.txt now i am trying to head this file 4 time by using a loop and every time this file is head with different values e.g in first instance it will... (7 Replies)
Discussion started by: jojo123
7 Replies

7. Shell Programming and Scripting

How to cut first line only from a text near a specific column without cutting a word

First I have to say thank you to this community and this forum. You helped me very much builing several useful scripts. Now, I can't get a solution the following problem, I'm stuck somehow. Maybe someone has an idea. In short, I dump a site via lynx and pipe the output in a file. I need to... (7 Replies)
Discussion started by: lowmaster
7 Replies

8. UNIX for Dummies Questions & Answers

how to display specific lines of a specific file

are there any basic commands that can display lines 99 - 101 of the /etc/passwd file? I'm thinking use of head and tail, but I forget what numbers to use and where to put /etc/passwd in the command. (2 Replies)
Discussion started by: raidkridley
2 Replies

9. Shell Programming and Scripting

Cutting specific name from configuration file

Hi falks, I have the following configuration file structure: file1:N file2:Y file3:Y file4:N ...... I need to cut from the configuration file only the file with the sign "Y" in the end and copy it to some directory. What is the best way to do it? Thanks in advance, Nir (8 Replies)
Discussion started by: nir_s
8 Replies

10. UNIX for Dummies Questions & Answers

Cutting n consecutive lines from a file...

Hi, I have this problem of separating 10 consecutive lines from a file, say starting from 21 to 30... I have used a filter like this.. head -n 30 myfile | tail -n 10 Is there a simpler way than this? (2 Replies)
Discussion started by: Vishnu
2 Replies
Login or Register to Ask a Question