1827458 Members
4730 Online
109965 Solutions
New Discussion

Re: grep problem

 
SOLVED
Go to solution
Hunki
Super Advisor

grep problem


I am doing a " ls -ltr |grep -v "*.gz" " - so that I cant view gzipped files but they still show up . How to get a listing without the gzipped file.

thanks
hunki
22 REPLIES 22
Laurent Menase
Honored Contributor
Solution

Re: grep problem

ls -lrt |grep -v "\.gz$"
James R. Ferguson
Acclaimed Contributor

Re: grep problem

Hi:

# ls -ltr |grep -v -F .gz

Regards!

...JRF...
Aussan
Respected Contributor

Re: grep problem

remove the " "

ls -ltr |grep -v *.gz
The tongue weighs practically nothing, but so few people can hold it
Hunki
Super Advisor

Re: grep problem

The whole point of having the grep problem solved was to have the the "0502" passed as a variable to the script.

I have reached a point where I get the following output , what I need to know is how to pass the 0502 as a value to a variable.

## ls -ltr |grep -v -F .gz |awk '{print $9}' |awk -F"." '{print $3}'

0502
0502
Sandman!
Honored Contributor

Re: grep problem

Could you provide a sample or see if the following works for you:

# VAR=$(ls -ltr |grep -v -F .gz |awk '{print $9}' |awk -F"." '{print $3}')

pass VAR to your script named "myscript" as follows:

# ./myscript $VAR
Hunki
Super Advisor

Re: grep problem

after passing on I am getting two 0502's

echo $VAR
0502 0502

I just want one - 0502 passed as Variable.
Sandman!
Honored Contributor

Re: grep problem

Piping to sort for uniqueness should do the trick for you:

# VAR=$(ls -ltr |grep -v -F .gz |awk '{print $9}' |awk -F"." '{print $3}' | sort -u)

~cheers
Steven E. Protter
Exalted Contributor

Re: grep problem

Shalom hunki,

Two awk's is likely resulting in double output.

VAR=$(ls -ltr |grep -v -F .gz |awk '{print $9}' |awk -F"." '{print $3}')

Try it with 1.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
James R. Ferguson
Acclaimed Contributor

Re: grep problem

Hi:

I think you can optimize something like this:

# ls -ltr |awk '$NF!~/.gz/ {n=split($NF,a,/\./);if (n==3) {print a[2]}}'

Hence if you had a file named "/tmp/myfile.123.log" you would be returned "123".

Anytime you 'grep' and pipe to 'awk' you are adding an extra process that 'awk' can accomodate!

Regards!

...JRF...
Hunki
Super Advisor

Re: grep problem


One more problem :

When I am doing a "mv filename.$VAR filename" it says

mv: filename not found

where VAR is the 0502.

there is a file by the name filename.0502 that exists in the directory but mv does not work when I supply the variable. Also when I do a echo $VAR , I do get the 0502 value for it.

Please suggest whats the problem.
Sandman!
Honored Contributor

Re: grep problem

Does filename have abcd.xyz.0502 format when you specify it as an argument to the mv(1) command?
Hunki
Super Advisor

Re: grep problem

Yes . infact filename.rar.0502
Sandman!
Honored Contributor

Re: grep problem

Most likely you have invisible characters in your filename; post the output of:

# ls -1 *05* | cat -ve
Hunki
Super Advisor

Re: grep problem

Yes it does have non printing characters :

filename.rar.0502$
Sandman!
Honored Contributor

Re: grep problem

Please post the code snippet to troubleshoot this issue.

~thanks
Hunki
Super Advisor

Re: grep problem

Here you go ...
James R. Ferguson
Acclaimed Contributor

Re: grep problem

Hi Hunki:

You need to source (read) your profile by doing:

. $HOME/.profile

In this fashion, you will propagate any environmental variables form your profile into your script.

By the way, when using 'cat -ev' the "$" character is a newline character.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: grep problem

Since you are using a real shell, why don't you remove the grep -v and replace by:
$ ls -ltr !(*.gz)

>Sandman: Piping to sort for uniqueness should do the trick for you:

It seems this is correct. There seems to be N files that have X.y.date, there X varies. So the initial script will return the same one twice.

>JRF: You need to source (read) your profile by doing:

While this is true, why would you ever need to source your .profile except if you are in a remsh or crontab script?
Hunki
Super Advisor

Re: grep problem

Hi,

But my original problem got left out ...

mv: filename not found

how to sort that out , please suggest.

thanks
Hunki
Super Advisor

Re: grep problem

Posting the script. after changes but the mv problem exists.

thanks.
Hunki
Super Advisor

Re: grep problem

Fixed it by replacing ls -ltr to ls -1 in the script.
Sandman!
Honored Contributor

Re: grep problem

>remove the grep -v and replace by:
>$ ls -ltr !(*.gz)

above works only if the current directory didn't have any subdirs underneath and reduce overhead of a long pipeline by removing dups in the first awk construct...

# ls -ltr | grep -v -F .gz | awk '{n=split($9,z,".")}END{print z[n]}'

~cheers