Could not extract file contents using awk and regex

My file is called /exports/tmp/ip789 and its content are pasted below:

# ipbackup

14 00 * * * backup /usr/lib/backup --batch /var/lib/schedule/backup.SR2.1.0.13Mar12_113854.ini
14 11 * * * backup /usr/lib/backup --batch /var/lib/schedule/backup.SR2.1.0.13Mar12_114013.ini
*/2 * * * * /exports/tmp/
u can see that the file has many lines.
I get the following output when i execute " awk ' $0 ~ /^(14) /' /exports/tmp/ip789"

14 00 * * * backup /usr/lib/backup --batch /var/lib/schedule/backup.SR2.1.0.13Mar12_113854.ini
14 11 * * * backup /usr/lib/backup --batch /var/lib/schedule/backup.SR2.1.0.13Mar12_114013.ini

But the output when i execute  "awk ' $0 ~ /^(1) /' /exports/tmp/ip789"
is nothing....

any idea?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Seems that in your second version you're searching for "1 " (i.e. a "1" followed by a space) which is obviously not present in your file (at least not at the start of a line).

"awk ' $0 ~ /^(1)/' /exports/tmp/ip789"


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pvinodpAuthor Commented:
awk ' $0 ~ /^([0-9]{2,2}|[*]).*ini$/ ' /exports/tmp/ip789

is returnign nothing:

But it seems to be correct when i checked the regex in linux regex-editor
I need it to match teh pattern where the start of the line could be 2 digits or a * and th eline should end with $
The latter statement should indeed work perfectly on the data you posted in your Q.

Could it be that there are whitespace characters following ".ini" ?

In this case the regex would fail.

Better this way then:

awk ' $0 ~ /^([0-9]{2,2}|[*]).*ini[ ]{0,}$/ ' ...
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

pvinodpAuthor Commented:
i opened the file in vi editor and set list to find that there is a $ at the end of ini.... so no space after that.

what would be your command to filter the lines which have decimal or * at the begining and end with .ini?

As I already said - your command is perfect for that kind of filtering, so I don't think I could give you a better one.

I just tested it here, and indeed, it works for me.
pvinodpAuthor Commented:

do you have any other solution..
any help with grep or sed??
would be great help..
"grep -E" or "egrep" work just the same way.

grep -E  "^([0-9]{2,2}|[*]).*ini$" /exports/tmp/ip789

With standard "grep" it's a bit more complicated:

grep -e "^[0-9]\{2,2\}" -e "^[*]"  /exports/tmp/ip789 | grep "ini$"

There is no real "and" in grep.

And with "sed" it's almost the same. The problem is always the "and" conjunction:

sed -n "/^[0-9]\{2,2\}/p;/^[*]/p"  /exports/tmp/ip789 | sed -n "/ini$/p"

All the above work (tested).
pvinodpAuthor Commented:
all the commands u sent are giving the same out put:
14 00 * * * backup /usr/lib/backup --batch /var/lib/schedule/backup.SR2.1.0.13Mar12_113854.ini
14 11 * * * backup /usr/lib/backup --batch /var/lib/schedule/backup.SR2.1.0.13Mar12_114013.ini

but does not show the the line:

*/2 * * * * /exports/tmp/
pvinodpAuthor Commented:
neglect my last post.

I need a a filter to get only teh task definition from crontab -l.
grep -E  "^([0-9]{2,2}|[*])( )([0-9]{2,2}|[*])" /exports/tmp/ip789

does not filter the line starting with*/2..
*/2 * * * * /exports/tmp/
does not end with "ini"
grep -E  "^([0-9]{2,2}|[*])( )([0-9]{2,2}|[*])" /exports/tmp/ip789
*/2 * * * * /exports/tmp/
has a / after the *, not a space
As I wrote,

my "egrep" version works even with a slash following the asterisk, but of course (as ozo pointed out) only for lines ending with "ini".

grep -E  "^([0-9]{2,2}|[*]).*ini$" /exports/tmp/ip789

In case you want to see all job entries:

grep -E  "^([0-9]{1,2}|[*]).*" /exports/tmp/ip789

Please note that in this latter version the "minute" part can consist of one or two digits (besides the asterisk stuff, of course), which might better reflect the givens in a real crontab.
grep -E  "^([0-9]{1,2}|[*]).*"
matches exactly the same lines as does
grep -E  "^[0-9*]"

If you want to see all cron job entries, some versions of cron also allow strings like
for the time and date fields, and entries may have leading spaces and tabs

with this new font here at EE it's a bit hard (for writers as well as for readers) to verify whether there's a space somewhere or not.

grep -E  "^([0-9]{1,2} |[*]).*"

Now it's there.
But now it fails on
grep -E  "^([   ]{0,}[0-9]{1,2}[ ,/-]|[*@]).*" /exports/tmp/ip789

Please note that inside the first square bracket pair there is a space and a TAB.

This might work as well somewhere, but not in my shell:

grep -E  "^([ \t]{0,}[0-9]{1,2}[ ,/-]|[*@]).*" /exports/tmp/ip789

please don't mention fcrontab. Please!
pvinodpAuthor Commented:
so if the user enter @daily or @monthly... then y pattern will fail to get the tasks.
Is there a pattern to fetch all valid tasks in crontab -l output ?
Did you try my very last suggestion?

For crontab -l :

crontab -l | grep -E  "^([   ]{0,}[0-9]{1,2}[ ,/-]|[*@]).*"
pvinodpAuthor Commented:
yes.. it works.
but I am not yet sure on how to check if it can filter all valid task definition.
See man 5 crontab.

Create a test file containing all the formats mentioned there, and check.
pvinodpAuthor Commented:
My final filter command is :
 crontab -l | grep -E  '^([   ]{0,}[0-9]{1,2}[ ,/-]|[*@]).*' | awk ' $0 !~ / backup \/usr\/lib\/backup / '

I call this inside a perl file
execute_command(crontab -l | grep -E  '^([   ]{0,}[0-9]{1,2}[ ,/-]|[*@]).*' | awk ' $0 !~ / backup \/usr\/lib\/backup / ');

But what I see is that because of the presence of $0 in awk filter the output is not as expected... How do i go ahead?
Use grep instead of awk:

crontab -l | grep -E  '^([   ]{0,}[0-9]{1,2}[ ,/-]|[*@]).*' |grep -v "backup /usr/lib/backup"
pvinodpAuthor Commented:
In the command: crontab -l | grep -E  "^([   ]{0,}[0-9]{1,2}[ ,/-]|[*@]).*"

what is between [ and ]   , is it a space or a tab??
See comment #37719195 above.

It's a TAB.
pvinodpAuthor Commented:
In your comment you said "Please note that inside the first square bracket pair there is a space and a TAB."

What is the purpose of having both tab and space?
And why are you not advising to use [\s] rather?
your grep may interpret that as matching either the character \ or the character s
but your grep may recognize [[:space:]]

[[:space:]] or [[:blank:]] should work!

crontab -l | grep -E  "^([ [:space:]]{0,}[0-9]{1,2}[ ,/-]|[*@]).*"

"[[:space:]]" means all "whitespace" characters, which includes TABs, and other "invisible" characters, like vertical TAB, LF etc. [[:blank:]] means just space and TAB.

>> What is the purpose of having both tab and space? <<

That's because a crontab entry might well have leading tabs, not only leading spaces.

>> why are you not advising to use [\s] rather? <<

That's because it doesn't work with my grep. Try it!
pvinodpAuthor Commented:
thanks a lot for your inputs
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux OS Dev

From novice to tech pro — start learning today.