2

I need to extract two parameters from each line of a svn log but I am not able to do it with grep.

My Svn log command, such as

svn log http://svn.apache.org/repos/asf/xerces/java/trunk/ | grep "^r[0-9]\+ | " | cut -c2-

Outputs the result in this format:

318150 | lehors | 2002-01-28 20:48:11 +0100 (Mon, 28 Jan 2002) | 2 lines
318149 | elena | 2002-01-28 20:46:33 +0100 (Mon, 28 Jan 2002) | 12 lines
318148 | lehors | 2002-01-28 20:33:36 +0100 (Mon, 28 Jan 2002) | 2 lines
318147 | lehors | 2002-01-28 20:22:51 +0100 (Mon, 28 Jan 2002) | 2 lines

How can I grep the release number (first parameter) and the date in this format?

318150 2002-01-28
318149 2002-01-28
318148 2002-01-28
318147 2002-01-28
0

4 Answers 4

2

Use a more robust Awk for this to pattern-match/extract from individual columns.

.. | awk 'BEGIN{FS="|"}{split($3,temp, " "); print $1,temp[1]}'
318150  2002-01-28
318149  2002-01-28
318148  2002-01-28
318147  2002-01-28

The .. | part represents the command to be included that produces the required output which is pipe-lined to Awk

The logic is pretty straight-forward, split input lines with de-limiter as | which is done by FS="|". Now $1 represents the first field you want, and for the second part, split the part $3 and use the split() function to split on delimiter, a white-space character and store it in array temp, so that it can be accessed as temp[1], the other space fields are present in the array from the next index on wards.

So ideally I guess it should be,

svn log http://svn.apache.org/repos/asf/xerces/java/trunk/ | \
   awk 'BEGIN{FS="|"}{split($3,temp, " "); print $1,temp[2]}'

Alternatively you could use GNU grep with its -E extended regular expression capabilities, but it is just not good enough to show the matching entries on same line like,

grep -oE '[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}' file

(and)

grep -oE '^[[:digit:]]{6}' file

but not together as I have used the -o flag to print the match only part.

Sign up to request clarification or add additional context in comments.

1 Comment

Just realized that this will print the time and OP wants the date, so changing temp[2] by temp[1] will do it :)
1

As your file is separated by a single space and you want to have the first and 5th columns, this is another solution by using cut:

cut -d' ' -f1,5 < svn_log_output_file

(or piping cut -d' ' -f1,5 to your command)

1 Comment

Also, you could do process substitution as, cut -d' ' -f1,5 < <(svn log http://svn.apache.org/repos/asf/xerces/java/trunk/)
1

A much simpler approach with multiple delimiters-

awk -F '[| ]' '{print $1, $7}' file

Where file contains the output you showed in your question.

Output-

318150 2002-01-28
318149 2002-01-28
318148 2002-01-28
318147 2002-01-28

Of course, you don't need to store in an intermediate file. You can do-

svn log http://svn.apache.org/repos/asf/xerces/java/trunk/ \
| grep "^r[0-9]\+ | " | cut -c2- | \
awk -F '[| ]' '{print $1, $7}'

Comments

0
awk '{print $1,$5}' file

318150 2002-01-28
318149 2002-01-28
318148 2002-01-28
318147 2002-01-28

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.