0

I have a file as mention below

import org.slf4j.LoggerFactory

class UpdateAvamDb {

        private final def DEFAULT_STALE_BACKUP_HOURS = 1.0

From an another file i want to read this file and get the integer value after DEFAULT_STALE_BACKUP_HOURS into a variable.

I have read about awk but i am not able to do it, please help

Thank you

2
  • You could use grep; try searching a bit on this site. There are plenty of examples. Commented Mar 19, 2014 at 11:39
  • i tried with grep, there is a problem in using it , i have more than one line in this file which is uses 'DEFAULT_STALE_BACKUP_HOURS' and grep is returning all of them. I read many examples but could not get what i want..so posted a question Commented Mar 19, 2014 at 11:44

1 Answer 1

1
echo "import org.slf4j.LoggerFactory
class UpdateAvamDb {
        private final def DEFAULT_STALE_BACKUP_HOURS = 1.0" \
| awk  '/DEFAULT_STALE_BACKUP_HOUR/{print $NF}'

output

1.0

echo "import .... \ | is a quick way to perform a test with your data, rather than put it into a file. The awk code block will perform the same function if you replace the print "..." | with the input filename at the end:

  awk '/..../{print $NF}' myFile
  1.0

And your heading says, store value in variable, so here we go:

 myVar=$(awk '/DEFAULT_STALE_BACKUP_HOUR/{print $NF}')

 echo "myVar=$myVar"
 1.0

AND based on your latest comment NOT to get all values. (I hope you only want the first one)

 myVar=$(awk '/DEFAULT_STALE_BACKUP_HOUR/{print $NF;exit}')

 echo "myVar=$myVar"
 1.0

We exit file processing as soon as we match 'DEFAULT....' and print the value.

Anyway!, awk is reading its input, either from a pipe or the file, and asking "does this line have the pattern "DEFAULT_STALE_BACKUP_HOUR" anywhere inside it?" . When it finds a line with that, it will then print the last field on the line, specfied with $NF. NF is an variable that gets set for each line that is read and means 'Number of Fields'. Adding the $ to the front then "converts" the expression to another common awk idom, in this case $6, which means print the 6th field from the current line.

A good tutorial about awk is at Grymoire's awk tutorial

IHTH

P.S. (The awk code is simple, but fragile because of your question is very specific. It is not a generalized answer. We'd need to see a better question definition to provide a solution that will work for more than just DEFAULT_.... )

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

4 Comments

@user3429026 : I'm glad to hear it. Please accept my answer by clicking the green arrow you will see when you "hover" your mouse near the vote counts. Good luck.
Hi Shellter, earlier i have used the same awk command but added ^ before 'DEFAULT_STALE_BACKUP_HOUR' so i did not get the output value.awk '/^DEFAULT_STALE_BACKUP_HOUR/{print $NF}' can you explain me the reason
In awk (and sed), you match a line of input with /matchForMe/. In your example, you are matching for the most basic type, a string. but the /matchForMe/ can be a regular expression (which is the subject of book-length discussions). Your ^ char is a reg-expr symbol that means 'Starting at the beginning of the line'. Note that my original explanation I said that /DEF.../ will match anywhere on the line. Now, with the ^ char you limiting the search to match that word only if it is at the beginning of the line. Any further questions about reg-exps should be posted as new Qs. Good luck.
And questions are mostly easily answered, pretty much has you have done in your question above. 1. Here is my input data (small sample, simple enough to diagnose the problem, but not pages and pages of irrelavant stuff). 2. Here is my required output from that input. 3. Here is my code, 4. here is my current output. 5. here are my thoughts and question about why it is not working. (This is for the benefit of other readers!). Good luck.

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.