?
Solved

Shell script help for listing files

Posted on 2007-03-22
14
Medium Priority
?
321 Views
Last Modified: 2013-12-27
Hi,

I need to work on script which will give me the selective list of files in a directory into another file. I will explain , I have a script abc.sh , the shell is ksh. I have a directory /home/anand/output. The output will have files by the name file1.txt , file2.txt and file1.txt.1 and file2.txt.2. Now I want to get a list of files without the .1 and .2 extensions only. and put them into a list.txt file. So my list.txt will have file1.txtand file2.txt. Can someone help me with this. I am not being able to figure out how to not select files with the last alphabet as a numeral.

Regards,
Anand
0
Comment
Question by:anandabrata
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
14 Comments
 
LVL 16

Expert Comment

by:Hanno P.S.
ID: 18771455
You don't need a shell script, a simple Unix command will do:
# ls -l |  grep '.*\.txt$' > listing.txt
This fill list all files with name ending with ".txt" and put this list into file listing.txt
0
 
LVL 16

Expert Comment

by:Hanno P.S.
ID: 18771502
If you want to see only files _not_ ending with ".<number>", use
 # ls -l | egrep -v '\.[0-9]$'
0
 

Author Comment

by:anandabrata
ID: 18771544
Would ls -l | egrep -v '\.[0-9]$' give me the same result if used in script or do i need to do something extra.

Regards
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:anandabrata
ID: 18771574
Also my files can have extension from 1 to 99 so in that case ls -l | egrep -v '\.[0-9]$ does not work. I tried ls -l | egrep -v '\.[0-99]$ and even that does not work.

regards,
anand
0
 

Author Comment

by:anandabrata
ID: 18771666
Thanks this # ls -l |  grep '.*\.txt$' > listing.txt solves the problem. Let me try it out in a script and I will get back to you.
0
 
LVL 16

Expert Comment

by:Hanno P.S.
ID: 18771713
Try
  # ls -l | egrep -v '\.[0-9][0-9]*$'

All file _not_ ending with a number (at least one digit long)
0
 

Author Comment

by:anandabrata
ID: 18771737
Oh no!! I just figured that the files might end with CXX. Which means C01, C02. so just giving "txt" will not help. I need something with egrep.  
0
 

Author Comment

by:anandabrata
ID: 18772290
Hi,

This command ( ls -l | egrep -v '\.[0-9][0-9]*$') works great excepting that it will also list the directories in which it runs. I have another saving grace i.e all the files that I want will always start with 2112657. So in addition in the above condition , even this needs to be checked. I tried
ls | egrep -v '\.[0-9][0-9]*$' | ls | grep '2112657*' and
ls | egrep -v '\.[0-9][0-9]*$' > ls | grep '2112657*'
but they don't work.

Regards,
Anand
0
 
LVL 16

Expert Comment

by:Hanno P.S.
ID: 18772484
The RE (regular expression)
    [0-99]
is not valid! You must use
    [0-9][0-9]*
instead. Therefore:
  # ls -l  | egrep -v '\.[0-9][0-9]*$'
lists all files _not_ ending with a number (at least one digit).
0
 
LVL 16

Accepted Solution

by:
Hanno P.S. earned 2000 total points
ID: 18772524
a) The "ls" lists *all* files and the "grep ..." filters out what you want to see (or not to see using -v)

Therefore:
  ls | egrep -v '\.[0-9][0-9]*$' | ls | grep '2112657*
should read
  ls | egrep -v '\.[0-9][0-9]*$' |  grep '^211'
  (everything NOT ending with "dot" "number") AND ( starting with 211)
0
 

Author Comment

by:anandabrata
ID: 18773324
Thanks that helps a lot. I just need help with another thing, I want make a recursive call to the ls command used above. Like this
acty_loa_cnt=do a select from database which will retrun me a count value
if [ $acty_loa_cnt -eq 11 ]
      then
        ls | egrep -v '\.[0-9][0-9]*$' |  grep '^211'
        else
       sleep 1000
fi
#do the select and if condition block above again. Basically make it recursive with a sleep of 1 min. how is that possible. Btw appreciate your help.

Regards,
Anand

0
 
LVL 58

Expert Comment

by:amit_g
ID: 18773828
If these are the only lines in your script, you can call the script again

acty_loa_cnt=do a select from database which will retrun me a count value
if [ $acty_loa_cnt -eq 11 ]
      then
        ls | egrep -v '\.[0-9][0-9]*$' |  grep '^211'
        else
       sleep 1000
fi
#Call the same script again
$0

Or you could put all this in a loop

while :
do
       acty_loa_cnt=do a select from database which will retrun me a count value
       if [ $acty_loa_cnt -eq 11 ]
             then
               ls | egrep -v '\.[0-9][0-9]*$' |  grep '^211'
               else
              sleep 1000
       fi
done

The only way it would stop is via Ctrl-C or if some error occurs. If you really want to run this every minute indefinitely, you should schedule it as a cron job (without sleep command).

BTW, sleep 1000 should be sleep 60 for 1 minute sleep.

0
 
LVL 48

Expert Comment

by:Tintin
ID: 18775078
So you're saying you want a list of all .txt files (without a number on the end) and any file ending in CXX, if so then a simple

ls -l *.txt *C[0-9][0-9]

will do that.
0
 
LVL 16

Expert Comment

by:Hanno P.S.
ID: 18778095
Tintin,

I think he also has files ending in Cx as well as Cxx

BTW: If we got all required information in the beginning it would have been somewhat easier and quicker coming up with the right solution ;-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question