2

I need to grep all strings that start with "[" and finish with a certain string, e.g. "apal". So all chars in between these 2 chars would be shown as well. Given an input such as:

[44060]apal223reaea[55000]opoer4nr4on[95749]assad fdfdf Bhassrj sdaapald33qdq3d3da3ded[66000]dsfsldfsfldkj[77000]porpo4o4o3j3mlkfxxxx[101335]KaMMMM MMM lapa[131322]sadasds ddd apaladsdas[138133]sadasdadasddsss KMMapaldsadsadwe[150000]idhoqijdoiwjodwiejdw

The output would be something lie

[44060]apal
[95749]assad fdfdf Bhassrj sdaapal
[101335]KaMMMM MMM lapal
[131322]sadasds ddd apal
[138133]sadasdadasddsss KMMapal
  • Would you please. post a sample of your in file? – fd0 Apr 17 '16 at 15:24
5

Use:

grep -o '\[.*apal' file.txt

Replace file.txt with the actual filename.

On the other hand, if you want to match [ at the start of the line:

grep -o '^\[.*apal' file.txt
|improve this answer|||||
  • Thanks a lot. I am on a MacOS. For me, this command still prints the whole line where the string is - and since this is a huge file with line breaks I can't read the output then. Is it possible just to print the specific matched string (and the chars in between)? – testTester Apr 17 '16 at 14:50
  • This would imply, if you used the command line as shown exactly by heemayl, that MacOS silently ignores the "-o" switch. If so you'll have to consider another tool which can do parentheses excerpted printing (awk or perl). However at least consider the merits of piping your output to "more" if you're suffering "huge file ...can't read..output". That is, above then: | more – Theophrastus Apr 17 '16 at 16:58
  • @testTester, I'm on a Mac as well and can confirm that this command works as indicated. However, please edit your question to provide a sample input file; if you have multiple pairs of brackets on a given line you will need to tweak the regex involved. – Wildcard Apr 22 '16 at 22:42
  • @Wildcard - I can't provide the sample input file unfortunately, as it is not a public file - but I will edit the above and make it clearer. The file is round 50MBs, no "\n"s on the file anywhere. I ended up achieving what I need by using grep -o -P '.{0,45}apal.{0}' which prints the match, plus 45 chars before it, which in general ends up covering the the first "[" needed. The output then presents a result per line. Thanks a lot for the help. – testTester Apr 24 '16 at 14:18
2

Try:

grep -o '^\[.*apal$' file.txt

The command to match a regex: .......use grep
To match only the regex written, ........use -o
To match the start of a line, .................use ^: ^\[
To match any characters in between, .use .: .*
To match the end of a line, ...................use $: apal$
A file that contains what you want to match.: file.txt

|improve this answer|||||
1

If you only want the text in between [ and apal, perl is a good choice

perl -lne '/(?<=\[)(.+?)(?=apal)/ and print $1' file

If you have GNU grep installed (via homebrew for example), you can

grep -Po '(?<=\[).+?(?=apal)' file
|improve this answer|||||
  • The grep option worked for me (Debian 9). However perl only took the first instance on each line. – jbrock Mar 31 '18 at 14:56
0

Rather than using grep, you'd do better to use sed.

sed 's/.*\(X.*Y\)/\1/p' < file.txt

Replace X with the starting pattern, and Y with the ending pattern. This command places into sed's buffer 1 everything that matches X to Y. The "\1" in the replacement pattern will print that buffer.

So, in your example:

sed 's/.*\(\[.*apal\)/\1/p' < file.txt

should do the trick.

|improve this answer|||||
  • Nice, but you should add a -n flag. – Wildcard Apr 24 '16 at 23:24
  • Yes, it requires the -n flag indeed... otherwise, the sed expression won't work! – Bogdan Stoica Sep 20 '17 at 5:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.