Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Shell script help for listing files

Posted on 2007-03-22
14
Medium Priority
?
322 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses

636 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