Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
10-02-2007, 07:46 PM
|
#1
|
LQ Newbie
Registered: Oct 2007
Posts: 20
Rep:
|
last pattern with sed?
I have this file (numbers.txt):
number_0
number_1
banana
number_2
orange
banana
number_3
number_4
number_1
apple
number_2
number_5
number_6
In the following sed command
$ sed -e '/number_1/,/number_2/ !d ' numbers.txt > out.txt
$ cat out.txt
number_1
banana
number_2
number_1
apple
number_2
I had twice answears.
However, I want only the last one.
How can I get this?
I want only the last pattern.
|
|
|
10-02-2007, 09:49 PM
|
#2
|
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
You could invert your logic:
Code:
sed -n '/number_1/,/number_2/{
p;
/number_2/q }' numbers.txt
number_1
banana
number_2
By the way, you don't need "-e" to precede a single sed command. Also, you can separate commands with a semicolon instead. At least with gnu sed.
Last edited by jschiwal; 10-02-2007 at 09:54 PM.
|
|
|
10-03-2007, 05:05 PM
|
#3
|
LQ Newbie
Registered: Oct 2007
Posts: 20
Original Poster
Rep:
|
Thanks for your help.
If I add another number_1 (there is only one number_2 in the text), I will have a new problem.
number --> add
number_1 --> add
number_0
number_1
banana
number_2
orange
banana
number_3
number_4
number_1
apple
number_2
number_5
number_6
number_1 --> add
number_100 --> add
$ sed -n '/number_1/,/number_2/{
> p;
> /number_2/q }' numbers.txt
number_1
number_0
number_1
banana
number_2
I solved this with:
$ sed : numbers.txt | sed -n '/number_1/,/number_2/{p;/number_2/q }' | sed '1! G ; h ; $! d' | \
> sed -n '/number_2/,/number_1/{p; /number_1/q}' | sed '1! G ; h ; $! d'
number_1
banana
number_2
Is another way to solve this?
|
|
|
10-03-2007, 06:43 PM
|
#4
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Quote:
Originally Posted by xpto09
I solved this with:
$ sed : numbers.txt | sed -n '/number_1/,/number_2/{p;/number_2/q }' | sed '1! G ; h ; $! d' | \
> sed -n '/number_2/,/number_1/{p; /number_1/q}' | sed '1! G ; h ; $! d'
number_1
banana
number_2
Is another way to solve this?
|
don't quite really understand what you are trying to get. where's the
number_1
apple
number_2
?
maybe you want to describe clearly what you are trying to get.?
|
|
|
10-03-2007, 07:21 PM
|
#5
|
LQ Newbie
Registered: Oct 2007
Posts: 20
Original Poster
Rep:
|
Oh! I thought that I had got to solve it.
The text below is a book from the Bible. In portuguese is "Eclesiastes".
If you noted, the chapter 1 begin with " and finish with (Ecl 1), the chapter
2 with " and finish with (Ecl 2) and so on.
If I have a file than has only one chapter, I can read the verse that I choose to read.
However, when I put two chapters inside it I do not get to choose the chapter and the verse
that I want to read.
For example, read chapter 2 and verse 4.
I started to learn sed trying to do this!
"1. Palavras do Eclesiastes, filho de Davi, rei de Jerusalém.
2. Vaidade das vaidades, diz o Eclesiastes, vaidade das vaidades! Tudo é vaidade.
3. Que proveito tira o homem de todo o trabalho com que se afadiga debaixo do sol?
4. Uma geração passa, outra vem; mas a terra sempre subsiste.
5. O sol se levanta, o sol se põe; apressa-se a voltar a seu lugar; em seguida, se levanta de novo.
.
.
.
18. Porque no acúmulo de sabedoria, acumula-se tristeza, e que aumenta a ciência, aumenta a dor".
(Ecl 1)
"1. Eu disse comigo mesmo: Vamos, tentemos a alegria e gozemos o prazer. Mas isso é também vaidade.
2. Do riso eu disse: Loucura! e da alegria: Para que serve?
3. Resolvi entregar minha carne ao vinho, enquanto meu espírito se . . .
4. Empreendi grandes trabalhos, construí para mim casas e plantei vinhas;
5. fiz jardins e pomares, onde plantei árvores frutíferas de toda espécie;
.
.
.
25. pois, quem come e bebe, senão graças a ele? Àquele que lhe é agradável Deus dá sabedoria, ... passa".
(Ecl 2)
|
|
|
10-04-2007, 03:00 AM
|
#6
|
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
There is a pattern that is very useful.
sed -n '/<pattern>/,/^$/'
If you organize your data so that sections are separated by blank lines, then this will select the entire section. ( Such as a chapter ).
So if you had:
(Ecl 1)
1. ...
2. ...
...
(Ecl 2)
1. ...
2. ...
...
Then you could use
Code:
#!/bin/bash
chapter="$1"
verse="$2"
sed -n '/'$chapter'/,^$/{/'$verse'\. /p}' bibletext
To print out a particular verse like: showverse 'Ecl 1' 2
You could test for the number of arguments, and if there is just one argument run:
Code:
sed -n '/'$chapter'/,^$/p' bibletext
instead to print out the entire chapter.
|
|
|
10-04-2007, 08:01 PM
|
#7
|
LQ Newbie
Registered: Oct 2007
Posts: 20
Original Poster
Rep:
|
When I run the codes there is the following message:
sed: -e expressão #1, caractere 5: unexpected `,'
expressão = expression
caractere character
I could not find a solution.
I "googled", but I did not the answer.
However, I am learning very much about sed!!!
|
|
|
All times are GMT -5. The time now is 01:46 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|