0

I have this file: 1.txt

===================================================================
File: .cvsignore        Status: Up-to-date

   Working revision:    1.1     Thu Jan 16 11:00:00 2020
   Repository revision: 1.1     /usr/local/rancid/var/CVS/Routers/.cvsignore,v
   Sticky Tag:          (none)
   Sticky Date:         (none)
   Sticky Options:      (none)

===================================================================
File: router.db         Status: Up-to-date

   Working revision:    1.6     Fri Jan 17 11:57:39 2020
   Repository revision: 1.6     /usr/local/rancid/var/CVS/Routers/router.db,v
   Sticky Tag:          (none)
   Sticky Date:         (none)
   Sticky Options:      (none)

===================================================================
File: .cvsignore        Status: Up-to-date

   Working revision:    1.1     Thu Jan 16 11:00:00 2020
   Repository revision: 1.1     /usr/local/rancid/var/CVS/Routers/configs/.cvsignore,v
   Sticky Tag:          (none)
   Sticky Date:         (none)
   Sticky Options:      (none)

===================================================================
===================================================================
File: 1.1.1.1       Status: Up-to-date

   Working revision:    1.2     Fri Jan 17 07:56:21 2020
   Repository revision: 1.2     /usr/local/rancid/var/CVS/Routers/configs/1.1.1.1,v
   Sticky Tag:          (none)
   Sticky Date:         (none)
   Sticky Options:      -ko

I want to extract string between File: and Status: (.cvsignore, router.db, 1.1.1.1)

and between Repository revision: and /usr/local/rancid/ (1.1, 1.6, 1.1 )

so final output should be:

.cvsignore 1.1

router.db 1.6

.cvsignore 1.1

1.1.1.1 1.2

for filename i created following filter:

sed 's/^.*File: //; s/Status:.*$//' 1.txt

Output:

===================================================================
.cvsignore       

   Working revision:    1.1     Thu Jan 16 11:00:00 2020
   Repository revision: 1.1     /usr/local/rancid/var/CVS/Routers/.cvsignore,v
   Sticky Tag:          (none)
   Sticky Date:         (none)
   Sticky Options:      (none)

===================================================================
router.db        

   Working revision:    1.6     Fri Jan 17 11:57:39 2020
   Repository revision: 1.6     /usr/local/rancid/var/CVS/Routers/router.db,v
   Sticky Tag:          (none)
   Sticky Date:         (none)
   Sticky Options:      (none)

===================================================================
.cvsignore       

   Working revision:    1.1     Thu Jan 16 11:00:00 2020
   Repository revision: 1.1     /usr/local/rancid/var/CVS/Routers/configs/.cvsignore,v
   Sticky Tag:          (none)
   Sticky Date:         (none)
   Sticky Options:      (none)

===================================================================
1.1.1.1      

   Working revision:    1.2     Fri Jan 17 07:56:21 2020
   Repository revision: 1.2     /usr/local/rancid/var/CVS/Routers/configs/172.18.1.24,v
   Sticky Tag:          (none)
   Sticky Date:         (none)
   Sticky Options:      -ko

===================================================================

For Repository revision i'm trying

sed 's/^.'*Repository revision:' //; s#usr#local#rancid:.*$//' 1.txt

But getting

sed: -e expression #1, char 15: unterminated `s' command

I need one regex for filename and second for Repository revision

5
  • Do you really want text like # Repository version to be output after some of the values? If so, how do you decide which ones? Commented Jan 20, 2020 at 14:50
  • i want it like this one file=$(awk for filename) and repo=$(awk for Repository version) Commented Jan 20, 2020 at 14:51
  • That would not produce the output you say in your question you want so fix your question or, even better, accept one of the answers you already got to the question you asked and then ask a new question. When doing so be clear - in that model would file contain a single file name or a list of all file names? It really seems like you're thinking about this the wrong way and so heading down the wrong path here. Commented Jan 20, 2020 at 14:54
  • i need to extract filename and corresponding Repository version so i can use those variables later on in different command Commented Jan 20, 2020 at 14:58
  • 1
    That's fine but it's extremely unlikely you should do that using the method you propose where you have separate commands for each and run them independently. Instead you should just use the output you get from one of the answers you got to the question you asked. Commented Jan 20, 2020 at 15:01

3 Answers 3

2

Your example isn't clear but is this what you're trying to do?

$ awk 'sub(/^[[:space:]]*(File|Repository revision):[[:space:]]*/,""){print $1}' file
.cvsignore
1.1
router.db
1.6
.cvsignore
1.1
1.1.1.1
1.2

or maybe this:

$ awk 'sub(/^[[:space:]]*(File|Repository revision):[[:space:]]*/,""){printf "%s%s", $1, ((++c)%2 ? OFS : ORS)}' file
.cvsignore 1.1
router.db 1.6
.cvsignore 1.1
1.1.1.1 1.2
Sign up to request clarification or add additional context in comments.

6 Comments

yes, that's it !, but i need it in sepate variables, one variable for filename and second for Repository version
awk variables or shell variables or something else?
shell variable, i'll use these expressions in BASH script
OK and what do you mean by "it" in the sentence "i need it in sepate variables"?
No. Whatever it is you're trying to do, you do not need/want that. That would be an anti-pattern. You asked how to get awk to produce a given output from a given input and you got answers for how to do that. Accept one and if you have a followup question about how to do something with that output in a bash script then ask that as a separate question.
|
1
$ awk '/^File:/ { printf "%s # filename ", $2} 
        /Repository revision:/{printf "%s # Repository version\n", $3}' input
.cvsignore # filename 1.1 # Repository version
router.db # filename 1.6 # Repository version
.cvsignore # filename 1.1 # Repository version
1.1.1.1 # filename 1.2 # Repository version

Comments

1

This might work for you (GNU sed):

sed -En '/^File:\s*(\S+).*/{s//\1/;h};/^\s*Repository revision:\s*(\S+).*/{s//\1/;H;g;s/\n/ /p}' file

Store the file name in the hold space and append the revision, then replace the introduced newline by a space and print the result.

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.