javac flag to print only the error locations - javac

Is it possible to make javac output only the error locations and the error messages, and hide the source code dump?
Now I get:
$ javac t.java
t.java:1: <identifier> expected
class {
^
t.java:2: reached end of file while parsing
bar
^
t.java:4: reached end of file while parsing
^
3 errors
I want to get only:
$ javac ... t.java
t.java:1: <identifier> expected
t.java:2: reached end of file while parsing
t.java:4: reached end of file while parsing

I think there is no flag you could pass to javac, but you can simply filter the output through any program which removes the superfluous lines. Here an example with grep:
javac t.java 2>&1 | egrep '^[a-zA-Z0-9_/]+\.java:[0-9]+: '
You might have to change the part matching the file name if you have strange letters in your file name - this seems to work for the ASCII subset.

Related

How do I grep for a string that occurs before the error code when the number of lines prior to error code is not consistent

When I find an error string in a log file, I need to know what caused
the error. But the filename that failed is a random number of lines
prior to the error string. Example:
logfile:
This **specific backup.tar** failed\n
Because of this\n
Or because of this\n
Then some random lines of output\n
Exiting -- searched-for string appears\n
If I used 'grep Exiting --before-context=4', I would find that backup.tar failed. But the number of random lines varies.
So I would like to use --before-context="string", or in this case --before-context=".tar".
Any idea how to do this?
Unfortunately, grep isn't up to this job. Try something like this in awk:
$ cat foo.txt
This specific backup.tar failed
Because of this
Or because of this
Then some random lines of output
Exiting -- searched-for string appears
$ awk '/This specific .* failed/ { filename = $3 } /searched-for string/ { print filename }' foo.txt
backup.tar

How can I print 10 lines above and until a matching word below for each matching line?

I need to parse log file so I output the errors(with the stack trace below), and 10 lines above each error.
For example:
2017-10-29 00:00:10,440 INFO ...
2017-10-29 00:00:10,473 WARN ...
2017-10-29 00:00:10,504 INFO ...
2017-10-29 00:00:10,547 INFO ...
2017-10-29 00:00:10,610 INFO ...
2017-10-29 00:00:11,176 WARN ...
2017-10-29 00:00:11,894 WARN ..
2017-10-29 00:00:11,900 INFO ...
2017-10-29 00:00:11,900 INFO ...
2017-10-29 00:00:12,632 WARN ...
2017-10-29 00:00:12,946 ERROR...
...(stack trace)...
...(stack trace)...
...(stack trace)...
2017-10-29 00:00:12,946 WARN
I need to output 10 lines above the ERROR until the the date(2017-10-29) below(not including the line of the date)
Thought about doing it with grep -n -B10 "ERROR"(for the 10 lines above) and sed '/ERROR/,/29/p'(for the stack trace) but how do I combine the two?
With grep + head pipeline:
grep -B10 'ERROR' g1 | head -n -1
This might work for you (GNU sed):
sed -n ':a;N;/ERROR/bb;s/[^\n]\+/&/11;Ta;D;:b;p;n;/2017-10-29$/!bb' file
Gather up at most 10 lines in the pattern space then use these lines as moving window through the file. When the string ERROR is encountered print the window and then any further lines until (but not including) the string 2017-10-29 is matched. Repeat if necessary.
Try this one
grep -no -B10 ' [0-9:,]* ERROR.*' infile
Need perhaps to substitute ' ' by [[:blank:]]
here is a way using awk:
awk ' {a[++b]=$0}
/^([0-9]{2,4}-?){3}/ {f=0}
/ERROR/ {f=1; for(i=NR-10;i<NR;i++) print a[i]}
f' file
we store each line to array. When matching a date log line, we unset the flag. When matching ERROR we set the flag and we print last 10 lines of the array. And when flag is on, we print (default action so we wrote just f)
This should print expected lines for all existing ERRORs in file.
note: the date regexp used is not strict but seems enough for the case.

How to convert any GPX file to Xcode acceptable GPX file

I am trying to simulate a path in Xcode which has speed, latitude and longitude information.
There is a site which produces the same: http://www.bikehike.co.uk/mapview.php
I found one awk script which can convert this file to Xcode acceptable format: https://gist.github.com/scotbond/8a61cf1f4a43973e570b
Tried running this command in the terminal: awk -F script.awk bikehike_course >output.gpx
Where script.awk has the script, bikehike_course has the GPX file and output.gpx is the output file name
UPDATE
Tried: awk -f script.awk bikehike_course > output.gpx
Error: awk: syntax error at source line 1 source file adjust_gpx_to_apple_format.awk
context is
awk >>> ' <<<
awk: bailing out at source line 24
I think the syntax of the GPS file is broken.
The script on adjust_gpx_to_apple_format.awk on github is a call of awk with the awk script provided as parameter (in shell syntax).
Thus, the name adjust_gpx_to_apple_format.awk is somehow misleading.
Either the awk ' at the beginning and ' $1' at the end has to be removed. In this case,
awk -f adjust_gpx_to_apple_format.awk
should work (as the script looks like a correct awk script otherwise).
If left as is, the script might be called directly in the shell:
> ./adjust_gpx_to_apple_format.awk input.txt >output.txt
In the latter case, I would suggest two additions:
Insert a "hut" in the first line e.g. #!/bin/bash which makes it more obviously.
Rename the script to adjust_gpx_to_apple_format.sh.
Note:
Remember, that the file suffix does not have the strict meaning in Unix like shells as they have for example in MSDOS. Actually, the suffix could be anything (including nothing). It's more valuable for the user than the shell and should be chosen respectively.

grep warning: recursive directory loop

I'm searching recursively some location e.g. /cygdrive/c/dev/maindir/dir/
There's a loop inside that directory structure i.e. there's a link .../maindir/dir/loopedDir/loopedDir pointing to .../maindir/dir/loopedDir.
When I run:
grep --exclude="/cygdrive/c/dev/maindir/dir/loopedDir/loopedDir" 'myPattern' -R /cygdrive/c/dev/maindir/dir/
...it works fine, like expected and finds what I need.
However, I also get a warning:
grep: warning: /cygdrive/c/dev/maindir/dir/loopedDir/loopedDir: recursive directory loop
...and I'm wondering why is that. Shouldn't dir exclusion prevent this particular looping occurance? How should I modify my query in order not to get the warning?
Add grep's option -s to suppress this and other error messages.

CMake how to remove the path from compile errors

When using cmake if there is a compile error the error is output with the entire path to the file containing the error. This path can be very long (see example below) so often it word wraps and makes it difficult to read.
Example Output:
/home/nick/projects/projectA/src/environment/base/terrain/base/TestFile.h:21:37: error: ‘TestFile’ does not name a type
/home/nick/projects/projectA/src/environment/base/terrain/base/TestFile.h:21:54: error: expected unqualified-id before ‘test’
/home/nick/projects/projectA/src/environment/base/terrain/base/TestFile.h:21:54: error: expected ‘)’ before ‘test’
Is there a way to remove the path from the error?
Something like this:
TestFile.h:21:37: error: ‘TestFile’ does not name a type
TestFile.h:21:54: error: expected unqualified-id before ‘test’
TestFile.h:21:54: error: expected ‘)’ before ‘test’
Thanks
I don't know if it is possible within cmake, but you can always redirect stderr to stdout and filter the output with a short sed script. At least the common project path can be filtered
make 2>&1 | sed 's/\/home\/nick\/projects\/projectA\/src\///g'

Resources