Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Non printing option in sed is behaving oddly Post 303036672 by Paul Walker on Sunday 7th of July 2019 12:59:41 PM
Old 07-07-2019
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 except I noticed that some lines that were matching my pattern are missing from my output.

After doing a little digging I found that after the command matched a pattern, if the next line also matched the pattern it would fail to output the second matched pattern line.
Can any one see why its behaving this way?
Below is the command I am using
Code:
sed -n -e '/\<PreviousJobNum\>[A-Z]*[0-9][0-9]*[A-Z]*\<\/PreviousJobNum\>/{p;n;}' /MyInputFile > /MyOutPutFile

The file I am reading in is MyInputFile, the content of which is
Code:
<PreviousJobNum>93296</PreviousJobNum>
<PreviousJobNum>95879D</PreviousJobNum>

When I run the above command my output in MyOutPutFile is
Code:
<PreviousJobNum>93296</PreviousJobNum>

If I change the data slightly in the original MyInputFile to have line between the two matched pattern lines, like below,
Code:
<PreviousJobNum>93296</PreviousJobNum>


<PreviousJobNum>95879D</PreviousJobNum>

Then my output Picks up both matched pattern lines in the MyOutPutFile as below.
Code:
<PreviousJobNum>93296</PreviousJobNum>
<PreviousJobNum>95879D</PreviousJobNum>

I suppose I could double space my input file to get around this but I think it would be better to understand why its behaving this way?
if anyone was able to offer some assistance with this I would be very grateful.
Thank you very much
Paul

Last edited by Scrutinizer; 07-07-2019 at 10:24 PM.. Reason: Removed hyperlink
 

10 More Discussions You Might Find Interesting

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

2. UNIX for Dummies Questions & Answers

sed -i option example

Can anyone give detailed and example for sed -i option. Where can we use this option?:) (3 Replies)
Discussion started by: gwgreen1
3 Replies

3. Shell Programming and Scripting

sed 'b' option

Hi, What does the following command do on files? sed '1b; $d' To me it just deletes the last line of the file. If so what is the significance of sed's 'b' option? Thanks (1 Reply)
Discussion started by: royalibrahim
1 Replies

4. Shell Programming and Scripting

sed behaving oddly, repeats lines

Hi, all. Here's the problem: 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: $ sed '/how/,/today/p' file.txt how are you... (9 Replies)
Discussion started by: pereyrax
9 Replies

5. Shell Programming and Scripting

sed with -e option

sed "s/^/8,A1,$dat,id2_3,/g" -e sed "s/$/,,,,,,,,,,,/g" temporary wn m running this script m getting a error... plz help me with this.... O/p sed: -e expression #1, char 3: unterminated `s' command ---------- Post updated at 11:35 AM ---------- Previous update was at 11:33 AM... (7 Replies)
Discussion started by: nikhil jain
7 Replies

6. Shell Programming and Scripting

sed -n option

Hi i am facing problem with sed -n option could you please help me on this, i have a file test the contents of the file is width="75">10/0 4/12</td>^M><a href='courtorders/100412zr.pdf' target="_blank">Miscellaneous Order</a></td>^M width="75">10/01/12</td>^M><a href='courtorde... (8 Replies)
Discussion started by: ragilla
8 Replies

7. Shell Programming and Scripting

sed one Liner option -e

Hi, I have the following command.(Delete all trailing blank lines at the end of a file.) sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' I don't understand the logic of this command and also I don't understand why -e is used. Can you please let me know the logic of this command and why three -e... (5 Replies)
Discussion started by: TomG
5 Replies

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

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

10. 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
GREP(1) 						      General Commands Manual							   GREP(1)

NAME
grep - search a file for lines containing a given pattern SYNOPSIS
grep [-elnsv] pattern [file] ... OPTIONS
-e -e pattern is the same as pattern -c Print a count of lines matched -i Ignore case -l Print file names, no lines -n Print line numbers -s Status only, no printed output -v Select lines that do not match EXAMPLES
grep mouse file # Find lines in file containing mouse grep [0-9] file # Print lines containing a digit DESCRIPTION
Grep searches one or more files (by default, stdin) and selects out all the lines that match the pattern. All the regular expressions accepted by ed and mined are allowed. In addition, + can be used instead of * to mean 1 or more occurrences, ? can be used to mean 0 or 1 occurrences, and | can be used between two regular expressions to mean either one of them. Parentheses can be used for grouping. If a match is found, exit status 0 is returned. If no match is found, exit status 1 is returned. If an error is detected, exit status 2 is returned. SEE ALSO
cgrep(1), fgrep(1), sed(1), awk(9). GREP(1)
All times are GMT -4. The time now is 04:45 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy