LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Cat and Sed Command Help. (https://www.linuxquestions.org/questions/linux-general-1/cat-and-sed-command-help-457334/)

vidyashankara 06-22-2006 03:05 PM

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.

druuna 06-22-2006 03:10 PM

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.

vidyashankara 06-22-2006 03:13 PM

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

druuna 06-22-2006 04:17 PM

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.

vidyashankara 06-22-2006 05:54 PM

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?

homey 06-22-2006 06:11 PM

You could put grep in front of it....
Code:

grep -v "HET A" < file.txt | sed -n '/ABC A/,/TER/p'

vidyashankara 06-23-2006 10:11 AM

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?

spirit receiver 06-23-2006 10:36 AM

Code:

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

vidyashankara 06-23-2006 10:55 AM

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?

oneandoneis2 06-23-2006 11:06 AM

Shouldn't you be using "grep" to do this..?

vidyashankara 06-23-2006 11:15 AM

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...

homey 06-23-2006 11:16 AM

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

vidyashankara 06-23-2006 11:28 AM

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?

homey 06-23-2006 11:55 AM

How about this?
Code:

sed -n '/.* A/{N;/HET/!p;N}' file.txt

vidyashankara 06-23-2006 12:14 PM

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"


All times are GMT -5. The time now is 09:26 PM.