Question about SED for excluding lines


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Question about SED for excluding lines
# 1  
Old 11-20-2007
Question about SED for excluding lines

Hi,

I'm using the SED instruction for substitution in a shell script. I'd like to improve it a bit. The instruction looks like this right now:

sed '/PRAGMA/!s/package/apps.package/g' $fich_src

I'm replacing the word "package" by "apps.package" for all the lines of the file $fich_src EXCEPT the ones that contain the word PRAGMA.
This works fine, but I want to add one more word for the lines exclusion. In other words, I want the substitution to take place for all the lines except the ones that contain the word PRAGMA or the word END (either or these two words)
I couldn't figure out the syntax. I've tried with two -e strings, I've tried to pipe, but it didn't work. What is the syntax to achieve what I want?

Thank you
# 2  
Old 11-21-2007
Some versions of sed, including GNU sed, will support what you want to do

sed '/\(PRAGMA\|END\)/!s/package/apps.package/g' $fich_src
# 3  
Old 11-21-2007
Hi fpmurphy,

I tried your suggestion but it didn't work, the substitution occured for the lines containing either of these words.

Thanks anyway
# 4  
Old 11-21-2007
Hi.

The sed solution of fpmurphy worked for me with GNU sed version 4.1.2.

What is the version of sed that you are using?

Are you required to use sed for some reason? ... cheers, drl
# 5  
Old 11-22-2007
Hi,

I need to compile Oracle packages for a certain list of schemas. It needs to be done automatically, so doing it through a shell is the most convenient way for our DBA.
I won't provide as many packages files as there are schemas, it would be long and not really professional. Moreover there are many references to other packages within each package, and each of these references needs to be prefixed with the schema.
So the best I thought is to have a file which contains the list of the schemas, and loop through that file to compile the package files, using SED to prefix each package reference with the schema's name.

How do I find out which version of sed I'm using, and if gnu sed is available?

Greg
# 6  
Old 11-22-2007
Hi.
Quote:
Originally Posted by greg
...
How do I find out which version of sed I'm using, and if gnu sed is available?
Like so:
Code:
% sed --version
GNU sed version 4.1.2
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
to the extent permitted by law.

or man sed ... cheers, drl
# 7  
Old 11-22-2007
Hi.

Here's a fairly vanilla awk script:
Code:
#!/usr/bin/env sh

# @(#) a1       Demonstrate feature.

set -o nounset
echo

## Use local command version for the commands in this demonstration.

echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version bash awk

echo
FILE=${1-data1}

echo
echo " Input file:"
cat $FILE

echo
echo " Results:"
awk '
/PRAGMA|END/    { print ; next }
                { gsub("package","apps.package")
                print
                }
' $FILE

exit 0

Producing:
Code:
% ./a1

(Versions displayed with local utility "version")
GNU bash 2.05b.0
GNU Awk 3.1.4


 Input file:
one package
two another package
thr package with PRAGMA (should not prepend apps.)
fou addition package at END (should not prepend apps.)
fiv yet another package
six last package

 Results:
one apps.package
two another apps.package
thr package with PRAGMA (should not prepend apps.)
fou addition package at END (should not prepend apps.)
fiv yet another apps.package
six last apps.package

Best wishes ... cheers, drl
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

2. Shell Programming and Scripting

ksh sed - Extract specific lines with mulitple occurance of interesting lines

Data file example I look for primary and * to isolate the interesting slot number. slot=`sed '/^primary$/,/\*/!d' filename | tail -1 | sed s'/*//' | awk '{print $1" "$2}'` Now I want to get the Touch line for only the associate slot number, in this case, because the asterisk... (2 Replies)
Discussion started by: popeye
2 Replies

3. Shell Programming and Scripting

Stripping ret of the lines in a file (sed question)

Hi all, I didn't use SED for 20 years and was never an expert. So my current knowledge is about zero. Please be patient with me. I'm neither a native speaker. I have a huge dictionary file and want the rest of the lines stripped. Everything after (and including) the "/" should be stripped. I... (2 Replies)
Discussion started by: Hinnerk2005
2 Replies

4. Shell Programming and Scripting

Use sed to print first n lines and last n lines of an output.

Use sed to print first n lines and last n lines of an output. For example: n=10 Can it be done? Thanks. (7 Replies)
Discussion started by: carloszhang
7 Replies

5. UNIX for Dummies Questions & Answers

Excluding a specific column from sed replacement

Hi, I would like to replace all 0's to 1's throughout my text file. However I do not want column 3 altered. How do I go about doing that? Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

6. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum using sed, awk

Hi friends, This is sed & awk type question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers whenever i find it and produce an output file with the sum. For example ###start of input text file #### abc def ghi 1 2 3 4 kjld random... (3 Replies)
Discussion started by: kaaliakahn
3 Replies

7. Shell Programming and Scripting

Sed/awk to delete single lines that aren't touching other lines

Hello, I'm trying to figure out how to use sed or awk to delete single lines in a file. By single, I mean lines that are not touching any other lines (just one line with white space above and below). Example: one two three four five six seven eight I want it to look like: (6 Replies)
Discussion started by: slimjbe
6 Replies

8. Shell Programming and Scripting

sed show lines text between 2 blank lines

I have a file like blah blah blah blah this is the text I need, which might be between 1-4 lines, but always has a blank line above and below it, and is at the end of the text file the code tags don't show the trailing blank line. I started by deleting the last blank line with: ... (2 Replies)
Discussion started by: unclecameron
2 Replies

9. Shell Programming and Scripting

print lines up to pattern excluding pattern

11 22 33 44 55 66 77 When pattern 55 is met, print upto it, so output is 11 22 33 44 (1 Reply)
Discussion started by: anilcliff
1 Replies

10. Shell Programming and Scripting

sed problem - delete all lines until a match on 2 lines

First of all, I know this can be more eassily done with perl or other scripting languages but, that's not the issue. I need this in sed. (or wander if it's possible ) I got a file (trace file to recreate the control file from oracle for the dba boys) which contains some lines another line... (11 Replies)
Discussion started by: plelie2
11 Replies
Login or Register to Ask a Question