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. |
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. |
Quote:
|
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. |
Quote:
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? |
You could put grep in front of it....
Code:
grep -v "HET A" < file.txt | sed -n '/ABC A/,/TER/p' |
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? |
Code:
sed -n "/^.....................A/,/^TER/p" file.txt |
Quote:
It doesnt work... It outputs all the lines in the file... what do i do now? |
Shouldn't you be using "grep" to do this..?
|
Quote:
|
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 |
Quote:
Its from the same example... but bit modified Here the textfile Quote:
How do i do that? |
How about this?
Code:
sed -n '/.* A/{N;/HET/!p;N}' file.txt |
Quote:
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. |