Computing.Net > Forums > Unix > Extracting lines from a file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Extracting lines from a file

Reply to Message Icon

Name: bakame
Date: August 1, 2003 at 08:47:40 Pacific
OS: Solaris
CPU/Ram: Intel
Comment:

I want to extract certain lines of a file between separators such as:
------
aaabbbb
------
aaaaa
xxxxxx
------
xxxxx

What utility (other than awk or perl) would help me to accomplish this task?
Thanks!



Sponsored Link
Ads by Google

Response Number 1
Name: LANkrypt0
Date: August 1, 2003 at 13:27:02 Pacific
Reply:

Are you looking to basically get everything in the file EXCEPT the:
------

?

If so are all the ------ the same size?

If not, how are you looking/going to specify which portion you want to extract?


0

Response Number 2
Name: bakame
Date: August 2, 2003 at 11:21:05 Pacific
Reply:

Yes, and the separators are all the same size. However, there are 2 or 3 or 4 lines of data that I need between the separators in the file.
ex:
------
aaaa
bbbb
------
xxxxxxx
yyyyyyy
zzzzzzz
-------
jjjjjjj
kkkkkkk
-------
llllll
llllll
llllll


0

Response Number 3
Name: Dlonra
Date: August 3, 2003 at 17:14:38 Pacific
Reply:

is this different than excluding the separating lines,
aaaa
bbbb
xxxxxxx
yyyyyyy
zzzzzzz
jjjjjjj
....


0

Response Number 4
Name: FishMonger
Date: August 4, 2003 at 08:41:00 Pacific
Reply:

Have you tried grep? I'm on a windows system (without grep) so, I can't test and verify if my systax is correct but this should be close.

grep -v /regex pattern for line seperator/ file.txt

The -v option finds all lines that don't match the pattern.

man page for grep
http://unixhelp.ed.ac.uk/CGI/man-cgi?grep


0

Response Number 5
Name: FishMonger
Date: August 4, 2003 at 14:11:46 Pacific
Reply:

I ran a couple tests and egrep would be better since it has better support for regex's.

Assuming the line seperator is a row of dashes, this egrep command will extract everything else and send it to a file.

egrep -v '^\-+$' file1.txt > file2.txt

If your line seporator is something else, we'll need to modify the regex.


0

Related Posts

See More



Response Number 6
Name: FishMonger
Date: August 4, 2003 at 14:19:22 Pacific
Reply:

After another test I discovered that you don't need to escape the dash in the regex. grep didn't like the dash in the regex but egrep didn't care.

here's the cleaner rexeg:
'^-+$'


0

Response Number 7
Name: bakame
Date: August 5, 2003 at 09:19:02 Pacific
Reply:

Thanks!
However, what should I do if I want to extract only some lines:
xxxxxxx
yyyyyyy
zzzzzzz
because the answer you gave me will redirect the content of file1.txt to file2.txt except the separators... and what I am looking for is to be able to extract certain lines but not the whole file.


0

Response Number 8
Name: FishMonger
Date: August 5, 2003 at 17:02:24 Pacific
Reply:

Can you convince me that this is not your homework assignment?


0

Response Number 9
Name: bakame
Date: August 6, 2003 at 07:11:34 Pacific
Reply:

It is part of my project. However, if you feel unconfortable giving me another hint don't worry I will figure it out!


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Extracting lines from a file

Extracting information from a file www.computing.net/answers/unix/extracting-information-from-a-file/7770.html

Get the first line from a file www.computing.net/answers/unix/get-the-first-line-from-a-file/7981.html

getting a specific line from a file www.computing.net/answers/unix/getting-a-specific-line-from-a-file/7708.html