For example
echo "abc-1234a :" | grep <do-something>
to print only abc-1234a
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I think these are closer to what you’re getting at, but without knowing what you’re really trying to achieve, it’s hard to say.
… though this will also match lines that have no colon. If you only want lines with colons, and you must use only grep, this might work:
Of course, this only makes sense if your
echo "abc-1234a :"is an example that would be replace with possibly multiple lines of input.The smallest tool you could use is probably
cut:And
sedis always available…For this last one, if you only want to print lines that include a colon, change it to:
Heck, you could even do this in pure bash:
For information on the
%%bit, you canman bashand search for “Parameter Expansion“.UPDATE:
You said:
The solutions with
grepprobably aren’t your best choice, then, since they’ll also print data from subsequent lines that might include colons. Of course, there are many ways to solve this requirement as well. We’ll start with sample input:You could use multiple pipes, for example:
Or you could use sed to process only the first line:
Or tell sed to exit after printing the first line (which is faster than reading the whole file):
Or do the same thing but only show output if a colon was found (for safety):
Or have awk do the same thing (as the last 3 examples, respectively):
Or in bash, with no pipes at all: