sed behaving oddly, repeats lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed behaving oddly, repeats lines
# 1  
Old 07-07-2012
Question sed behaving oddly, repeats lines

Hi, all.

Here's the problem:

Code:
sed '/FOO/,/BAR/p'

That should print anything between FOO and BAR, right?

Well, let's say I have file.txt that contains just one line "how are you today?".
Then I run something like the above and get:

Code:
$ sed '/how/,/today/p' file.txt
how are you today?
how are you today?

It prints the line twice when I was expecting it to print just "are you".
I tried several text files with different content and also played around a little bit with quoting and regexps and in every case I got the line or the entire text duplicated.

I'm pretty sure the problem is mine and not sed's but I can't figure out what I'm doing wrong.

Any ideas?
- - - - - - - - -

Off Topic
Thanks to all of you out there. Reading this forum got me out of trouble many times Smilie
# 2  
Old 07-07-2012
From man pages for sed:

Code:
       -n     Suppress	the  default  output  (in which each line, after it is
	      examined for editing, is written to standard output). Only lines
	      explicitly selected for output are written.

This should also help.
This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 07-07-2012
Explain this Smilie

Code:
 echo "how are you today?" | sed '/how/,/today/p'

This gives me as below

Code:
 
how are you today?
how are you today?

Why is this?
# 4  
Old 07-07-2012
Because sed prints every line by default anyway, unless you use -n or unless you delete the line. The p command in that sed script also prints it. So, that script will print lines that match twice and lines that don't match once.

You can either use -n option or you can invert the logic:
Code:
sed '/how/,/today/!d'

Note the !. That deletes every line that does not match.

Regards,
Alister
# 5  
Old 07-07-2012
Oh, I see! Thank you very much!!

I misunderstood how sed and '/FOO/,/BAR/p' work.

In a multiple line file, I get all the lines between the patterns, as expected, using the -n flag, without any extra output. .

When providing a one line file (or a line of text using | ), it prints out the whole line, which makes sense after reading what you explained in the other post.

I'm working on a file with several lines, so the -n does the trick. But I'm still curious on how to print a string between two patterns of a single line using sed.
# 6  
Old 07-07-2012
I get that clearly.

So what if I want to just get the characters between how and today??

Isn't this sed-1-liner supposed to do that?
# 7  
Old 07-07-2012
Alister, thanks! That's really helpful. I need to get my hands on a thorough sed manual
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Non printing option in sed is behaving oddly

Hi I'm having a problem with a sed command that I thought I was using correctly but apparently that's not the case. I was hoping someone here could point out what it is I am doing wrong? I am using the print, no print option for a matched pattern in sed. Everything seemed to be working fine... (2 Replies)
Discussion started by: harveyclayton
2 Replies

2. UNIX for Beginners Questions & Answers

Non printing option in sed is behaving oddly

Hi I'm having a problem with a sed command that I thought I was using correctly but apparently that's not the case. I was hoping someone here could point out what it is I am doing wrong? I am using the print, no print option for a matched pattern in sed. Everything seemed to be working fine... (5 Replies)
Discussion started by: Paul Walker
5 Replies

3. Shell Programming and Scripting

sed parser behaving strange on replacing multiple words in multiple files

I have 4000 files like $cat clus_grp_seq10_g.phy 18 1002 anig_OJJ65951_1 ATGGTTTCGCAGCGTGATAGAGAATTGTTTAGGGATGATATTCGCTCGCGAGGAACGAAGCTCAATGCTGCCGAGCGCGAGAGTCTGCTAAGGCCATATCTGCCAGATCCGTCTGACCTTCCACGCAGGCCACTTCAGCGGCGCAAGAAGGTTCCTCG aver_OOF92921_1 ... (1 Reply)
Discussion started by: sammy777888
1 Replies

4. Solaris

Remove oddly named file

I accidentally saved a txt file in vi with the name ":q!". no amount of regex tomfoolery I can think of will allow me to remove the file. anyone got any ideas? (4 Replies)
Discussion started by: os2mac
4 Replies

5. Shell Programming and Scripting

Remove brackets repeats and separate in columns

Hi all, I want to remove the remove bracket sign ( ) and put in the separate column I also want to remove the repeated entry like in first row in below input (PA156) is repeated ESR1 (PA156) leflunomide (PA450192) (PA156) leflunomide (PA450192) CHST3 (PA26503) docetaxel... (4 Replies)
Discussion started by: manigrover
4 Replies

6. UNIX for Dummies Questions & Answers

Can't figure out why this repeats

#!/bin/sh while IFS=: read address port; do : ${port:=443} address=$address port=$port cd $f_location number=`grep "$address" thing.txt -A 1 | grep "addresses=" | cut -d'"' -f2` echo "$address,$port,$number,$answer" >>... (9 Replies)
Discussion started by: shade917
9 Replies

7. UNIX for Dummies Questions & Answers

awk repeats counter

if I wanted to know if the word DOG(followed by several random numbers) appears in col 1, how many times will that same word DOG* appeared in col 2? This is a very large file Thanks! (7 Replies)
Discussion started by: verse123
7 Replies

8. Shell Programming and Scripting

sed and cut behaving differently

I have attached a file with few records. First 2 characters of each record are binary characters. I can remove it by and it works fine. But is behaving differently and removing more than expected characters. Can someone help me in accomplishing it through sed? Thanks in advance. (13 Replies)
Discussion started by: amicon007
13 Replies

9. UNIX for Dummies Questions & Answers

Search for repeats in text file - how?

I have a text file that I want to search for repeated lines and print those lines. These would be lines in the file that appear more than once. Is there a way to do this? Thanks (4 Replies)
Discussion started by: aarondesk
4 Replies
Login or Register to Ask a Question