1

I need to print lines between those that contain a "/" in the name. I tried with:

awk '/+SOLUTION/ESTIMATES/,/-SOLUTION/ESTIMATES/' $F > fil$F

and

awk '/+SOLUTION"/"ESTIMATES/,/-SOLUTION"/"ESTIMATES/' $F > fil$F

and

awk '/"{+SOLUTION/ESTIMATES}"/,/"{-SOLUTION/ESTIMATES}"/' $F > fil$F

but the error is always more or less the same like "unterminated string".

How can I print lines between those patterns?

1
  • edit your question to show sample input and expected output that cover all of your non-trivial cases so we can provide the best answer for you as there are many possibilities. Commented Nov 27, 2023 at 13:19

3 Answers 3

2

/regex/ is short for $0 ~ /regex/, which you can also write $0 ~ "regex", so besides escaping the / by prefixing it with \ or using \octal with the / character value (\57 on ASCII-based systems, \141 on EBCDIC-based systems for instance), you can also do:

awk '$0 ~ "\\+SOLUTION/ESTIMATES", $0 ~ "-SOLUTION/ESTIMATES"' < "$F" > "fil$F"

Beware + is an extended regex operator, so needs escaped either as \+ or [+]. As \ also has a special meaning in the syntax of "strings", it's better to double it so a \ be passed to the regex engine. With some awk implementations, you can get away with just "\+" but not all.

Note that using redirection rather than passing the file path as argument is preferable when possible as awk would choke on values of $F such as - or that contain = (or that start with - with older versions of busybox awk). You also forgot to quote that $F argument making it subject to split+glob. Quoting parameter expansions in targets of redirections is generally not needed except for the notable exceptions of ksh88 and the bash shell when not POSIX mode (the latter still widely used) so it's still a good habit to quote them there as well.

Here, instead of a regex match, you could do a substring search:

awk 'index($0, "+SOLUTION/ESTIMATES"), index($0, "-SOLUTION/ESTIMATES")' < "$F" > "$fil$F"

To print from the line that is exactly +SOLUTION/ESTIMATES to the one that is exactly -SOLUTION/ESTIMATES (or to the end of the input if not found):

awk '$0 == "+SOLUTION/ESTIMATES", $0 == "-SOLUTION/ESTIMATES"' < "$F" > "$fil$F"

Using perl gives more flexibility:

# regex, but with different delimiters (m@...@, m{...}...)
perl -ne 'print if m{\+SOLUTION/ESTIMATES} .. m{-SOLUTION/ESTIMATES}'
# regex but use \Q...\E regex quoting for anything inside to be taken literally:
perl -ne 'print if m{\Q+SOLUTION/ESTIMATES\E} .. m{-SOLUTION/ESTIMATES}'
# substring search with q(...) as strong quotes, same as '...' except
# easier to use inside shell '...' quotes.
perl -ne 'print if index($_, q(+SOLUTION/ESTIMATES)) >= 0 ..
                   index($_, q(-SOLUTION/ESTIMATES)) >= 0'
# string equality with eq
perl -lne 'print if $_ eq q(+SOLUTION/ESTIMATES) ..
                    $_ eq q(-SOLUTION/ESTIMATES)'
0
2

How can I print lines between those patterns?

By replacing / with \57 records between patterns (and records with those patterns) can be printed.

But please read this before using range expressions.

$ echo 'foo/bar was here' | awk '/foo\57bar/'
foo/bar was here
1

When used inside an awk regular expression constant, the forward slash character must be escaped to distinguish it from the surrounding delimiters. For example with GNU awk:

$ echo 'foo/bar was here' | gawk '/foo/bar/'
awk: cmd. line:2: /foo/bar/
awk: cmd. line:2:          ^ unexpected newline or end of string

but

$ echo 'foo/bar was here' | gawk '/foo\/bar/'
foo/bar was here

As well, the + character has a special meaning in the Extended Regular Expression (ERE) dialect used by awk, and should be escaped if you want to match a literal plus character. So:

/\+SOLUTION\/ESTIMATES/,/-SOLUTION\/ESTIMATES/
1
  • Thanks to all. I used this suggestion and it really worked. awk '/\+SOLUTION\/ESTIMATES/,/\-SOLUTION\/ESTIMATES/' $F > temp Commented Nov 26, 2023 at 20:05

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.