What would be the most succinct way of doing this (preferably in 1 line, maybe 2):
searching the first 10 characters of every line in a text file for a specific string, and if it was found, print out characters 11-20 of the line on which the string was found.
In this case, it's known that there are no duplicates in the file, so it either finds the string on one line or none of them.
I'm guessing one of grep, sed or awk can be used but I can't figure out the best way.
I agree. Jairaj's proposed solution is definitely incorrect. However, without some clarification from the original poster, yours could be as well.
If the string being searched must occur in the first ten characters, but can itself be less than 10 characters, it could then occur at different locations within the first 10 characters. If this scenario is a possibility, then
v==substr($0,1,10)
would be insufficient.
If this is the case, then the following would be the correct approach:
This is not about right vs. wrong OR what if suppositions.
The OP ask to "searching the first 10 characters" and that's a fact and we should start from here. Everything else are only suppositions.
I can provide answers/solutions(short) base on facts, otherwise the list will be too long
Your solution is incorrect, regardless (as was mine). The problem requested printing characters 11-20 if a match is found; that second substr call returns 11-30 (the third argument is length, not an index). The 20 should be a 10.