1

How can i stop printing after the file has reached the pattern ?

pattern='...........'
cat file | <command> $pattern

example: file contents :

aaaaaaaaa
aaaaaaaaa
.........
bbbbbbbb
bbbbbbbbb
bbbbbbbbb
.237813981238

desired output :

aaaaaaaaa
aaaaaaaaa
computer10
  • 37
  • 5
  • In your question you have written `aaaaa`, `bbbb`, `.....` etc. in different lines. But notice how the parser has changed that into one line with fields separated by a space. Does your pattern, and each line you have written, represent one different line in the file? If so, please use 4 spaces before each line. – Peque May 18 '14 at 13:15

2 Answers2

2

If your pattern is indeed one line in the file, then your question is a duplicate of question 5227295:

 cat file | sed -n '/_pattern_/q;p'

Notice you may need to escape some characters in _pattern_ in order to make it work as expected (i.e. if your pattern has dots as in ...., you should write \.\.\.\.).

If your pattern may be in the middle of one line, then, in pure Bash:

contents=$(<file)
echo "${contents%%_pattern_*}"

Use ........... (or your pattern) instead of _pattern_. Notice this time you do not need to scape the pattern.

Community
  • 1
  • 1
Peque
  • 13,638
  • 11
  • 69
  • 105
0

Try this:

export PATTERN_ESCAPED=`echo $PATTERN | sed -e 's/[]\/$*.^|[]/\\&/g'`
cat file | sed -r 's/'"$PATTERN_ESCAPED"'.*//'

The first line escapes all symbols that may be used as part of regex. The second replaces the pattern and all symbols after it by nothing.

Alex
  • 9,891
  • 11
  • 53
  • 87