LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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

Reply
 
Search this Thread
Old 10-02-2007, 07:46 PM   #1
xpto09
LQ Newbie
 
Registered: Oct 2007
Posts: 18

Rep: Reputation: 0
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.
 
Old 10-02-2007, 09:49 PM   #2
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
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.
 
Old 10-03-2007, 05:05 PM   #3
xpto09
LQ Newbie
 
Registered: Oct 2007
Posts: 18

Original Poster
Rep: Reputation: 0
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?
 
Old 10-03-2007, 06:43 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
Quote:
Originally Posted by xpto09 View Post
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.?
 
Old 10-03-2007, 07:21 PM   #5
xpto09
LQ Newbie
 
Registered: Oct 2007
Posts: 18

Original Poster
Rep: Reputation: 0
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)
 
Old 10-04-2007, 03:00 AM   #6
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
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.
 
Old 10-04-2007, 08:01 PM   #7
xpto09
LQ Newbie
 
Registered: Oct 2007
Posts: 18

Original Poster
Rep: Reputation: 0
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!!!
 
  


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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get the pattern using sed or awk? ahpin Programming 3 08-02-2007 03:16 AM
Sed pattern matching digitalbrutus Programming 1 08-20-2006 01:37 PM
pattern matching problem in sed digitalbrutus Programming 4 08-20-2006 04:40 AM
Inputting with sed, based on matched pattern mike9287 Programming 5 07-20-2006 07:49 AM
replacing pattern with sed produces double realos Programming 1 10-17-2002 08:03 PM


All times are GMT -5. The time now is 08: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