LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 06-22-2006, 03:05 PM   #1
vidyashankara
Member
 
Registered: May 2006
Posts: 46

Rep: Reputation: 15
Cat and Sed Command Help.


I need to know something about the cat command.

Lets say i have a text file named test.tct

It contains
ABC A 123
ABC A 456
ABC A 789
HET A 123
HET A 123
HET A 567
ABC A 123
ABC A 565
ABC A 123
TER
ABC B 123
ABC B 456
ABC B 789
HET B 123
HET B 123
HET B 567
ABC B 123
ABC B 565
ABC B 123
TER
HET 123
HET 123
HET 678

Is there a way for linux to print all the lines between ABC A and TER(Lines 1-10) ?
The file is dynaminc, some files might have a 100 lines and some might have just 5. so head -n 5 doesnt work. Let me know if you know some command.
 
Old 06-22-2006, 03:10 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Using sed is one way:

sed -n '1,10p' infile

The -n turns of normal printing of lines,
'1,10p' prints (p) lines 1 to 10 (or up to as many as ten).

Hope this helps.
 
Old 06-22-2006, 03:13 PM   #3
vidyashankara
Member
 
Registered: May 2006
Posts: 46

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by druuna
Hi,

Using sed is one way:

sed -n '1,10p' infile

The -n turns of normal printing of lines,
'1,10p' prints (p) lines 1 to 10 (or up to as many as ten).

Hope this helps.
The thing is I dont know the line numbers. All i know is the First 6 characters of my string are "ABC A" and the script should stop read when it hits TER
 
Old 06-22-2006, 04:17 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Maybe this will help. It's the same command, but using regexp to determine the line numbers:

sed -n '/ABC A/,/TER/p' infile

If the input given in your first post is used, the following is the output:

ABC A 123
ABC A 456
ABC A 789
HET A 123
HET A 123
HET A 567
ABC A 123
ABC A 565
ABC A 123
TER

Hope this works for you.
 
Old 06-22-2006, 05:54 PM   #5
vidyashankara
Member
 
Registered: May 2006
Posts: 46

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by druuna
Hi,

Maybe this will help. It's the same command, but using regexp to determine the line numbers:

sed -n '/ABC A/,/TER/p' infile

If the input given in your first post is used, the following is the output:

ABC A 123
ABC A 456
ABC A 789
HET A 123
HET A 123
HET A 567
ABC A 123
ABC A 565
ABC A 123
TER

Hope this works for you.

Thanks a lot! You helped me out a lot!

One small question.
is it possible to make sed read the lines only if its starts with ABC?
like
sed -n ^'/ABC A/,/TER/p' infile
that doesnt work... what do i do?
 
Old 06-22-2006, 06:11 PM   #6
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
You could put grep in front of it....
Code:
grep -v "HET A" < file.txt | sed -n '/ABC A/,/TER/p'
 
Old 06-23-2006, 10:11 AM   #7
vidyashankara
Member
 
Registered: May 2006
Posts: 46

Original Poster
Rep: Reputation: 15
SED Command in Linux

I have a huge text file in Linux. I use Cat to view the output. my command is

cat file.txt sed -n ^"/.....................A/,/TER/p"

It should read the file for a string starting with ".....................A" where . can be any character and it should stop with TER.

This outputs "sed: -e expression #1, char 1: unknown command: `^'"

whats wrong?
 
Old 06-23-2006, 10:36 AM   #8
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
Code:
sed -n "/^.....................A/,/^TER/p" file.txt
 
Old 06-23-2006, 10:55 AM   #9
vidyashankara
Member
 
Registered: May 2006
Posts: 46

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by spirit receiver
Code:
sed -n "/^.....................A/,/^TER/p" file.txt

It doesnt work... It outputs all the lines in the file... what do i do now?
 
Old 06-23-2006, 11:06 AM   #10
oneandoneis2
Senior Member
 
Registered: Nov 2003
Location: London, England
Distribution: Ubuntu
Posts: 1,460

Rep: Reputation: 46
Shouldn't you be using "grep" to do this..?
 
Old 06-23-2006, 11:15 AM   #11
vidyashankara
Member
 
Registered: May 2006
Posts: 46

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by oneandoneis2
Shouldn't you be using "grep" to do this..?
can you use grep to get information between 2 lines in the file? i dint know you can...
 
Old 06-23-2006, 11:16 AM   #12
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
If this is from the same example you were using yesterday, here is another way using sed.
Code:
sed -n '/.*A/{N;/HET/!p}' file.txt
 
Old 06-23-2006, 11:28 AM   #13
vidyashankara
Member
 
Registered: May 2006
Posts: 46

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by homey
If this is from the same example you were using yesterday, here is another way using sed.
Code:
sed -n '/.*A/{N;/HET/!p}' file.txt

Its from the same example... but bit modified

Here the textfile
Quote:
ATOM 6 CG PHE A 10 41.541 39.112 129.318 1.00 17.02 1LYN 122
ATOM 7 CD1 PHE A 10 40.620 38.062 129.219 1.00 14.04 1LYN 123
ATOM 8 CD2 PHE A 10 42.614 39.064 130.228 1.00 13.11 1LYN 124
ATOM 9 CE1 PHE A 10 40.803 36.968 130.050 1.00 17.76 1LYN 125
HETATM 10 CE2 PHE A 10 42.775 37.957 131.042 1.00 13.57 1LYN 126
HETATM 11 CZ PHE A 10 41.863 36.927 130.940 1.00 14.80 1LYN 127
HETATM 12 N LEU A 11 39.446 39.166 125.736 1.00 9.66 1LYN 128
ATOM 13 CA LEU A 11 38.377 39.739 124.894 1.00 6.95 1LYN 129
ATOM 14 C LEU A 11 36.961 39.283 125.285 1.00 8.02 1LYN 130
ATOM 15 O LEU A 11 36.756 38.173 125.782 1.00 13.20 1LYN 131
ATOM 16 CB LEU A 11 38.751 39.340 123.479 1.00 4.67 1LYN 132
ATOM 17 CG LEU A 11 38.383 40.028 122.209 1.00 6.25 1LYN 133
ATOM 18 CD1 LEU A 11 38.872 39.094 121.148 1.00 13.72 1LYN 134
ATOM 19 CD2 LEU A 11 36.926 40.178 121.929 1.00 10.47 1LYN 135
ATOM 20 N ASN A 12 35.919 40.058 125.133 1.00 7.45 1LYN 136
ATOM 21 CA ASN A 12 34.571 39.734 125.566 1.00 5.18 1LYN 137
TER
So it must start reading ".....................A" and end at TER.

How do i do that?
 
Old 06-23-2006, 11:55 AM   #14
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
How about this?
Code:
sed -n '/.* A/{N;/HET/!p;N}' file.txt
 
Old 06-23-2006, 12:14 PM   #15
vidyashankara
Member
 
Registered: May 2006
Posts: 46

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by homey
How about this?
Code:
sed -n '/.* A/{N;/HET/!p;N}' file.txt

returns p event not found...

isnt there an easy way to read all the information between 2 lines?

Like
cat text.txt |grep ^".....................A","TER" ?

the sed command would work if i defined to read the file only if it starts with ".....................A"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
MSDOS version for cat command frankie_DJ General 9 07-22-2005 08:08 AM
cat command for text files minm Linux - Newbie 5 07-03-2005 12:32 AM
cat command juanb Linux - Newbie 4 08-19-2004 11:14 AM
what are the limits to the 'cat' command? Frybyte Linux - General 12 04-22-2004 04:32 PM
cat Command Moeses Linux - General 1 06-20-2002 03:27 AM


All times are GMT -5. The time now is 01:15 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration