How To Retreive Files With a Special Condition?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How To Retreive Files With a Special Condition?
# 8  
Old 07-02-2018
Quote:
Originally Posted by april
Well, here is what I got when I typed in
Code:
uname -a

Code:
Linux (server name) 3.0.101-108.52-default #1 SMP Tue May 29 19:42:53 UTC 2018 (80e6815) x86_64 x86_64 x86_64 GNU/Linux

As far as the ls version, i do not know how to search for that.
Code:
ls --version

if it is a Gnu ls (which is most likey given your OS)
Quote:
The files do not start with an A. I just happened to pick that as examples. There are numbers imbedded in the file names which are the dates & times the files were received and created.
Please post examples of filenames in the directory. The above solutions are based on your example file names.

Andrew
# 9  
Old 07-02-2018
I wonder if I look at this a little differently.

If I know the name of the last .x file, does it make it easier if I look for all the file names created after that last .x file?

---------- Post updated at 02:39 PM ---------- Previous update was at 02:33 PM ----------

Examples of filenames:
Code:
mRefresh_2018-06-28_003015.x
mRefresh_2018-06-28_221609.tar
mRefresh_2018-06-29_003058.tar
mRefresh_2018-06-29_003058.x
mRefresh_2018-06-29_220654.tar
mRefresh_2018-06-30_003043.tar
mRefresh_2018-07-01_222747.tar
mRefresh_2018-07-02_003030.tar

So, based on this list, I want the name of the last 4 files.

Code:
ls --version
ls (GNU coreutils) 8.12

# 10  
Old 07-02-2018
Quote:
Originally Posted by april
. . .

The files do not start with an A. I just happened to pick that as examples.
Too bad - had my olfactory monitor not collapsed recently, I could have smelled your sample didn't meet your reality, and of course had not mapped your sample data in my proposal.


As it turns out my proposal doesn't apply, please adapt it to meet your request.
# 11  
Old 07-03-2018
Quote:
Originally Posted by april
I wonder if I look at this a little differently.

If I know the name of the last .x file, does it make it easier if I look for all the file names created after that last .x file?

---------- Post updated at 02:39 PM ---------- Previous update was at 02:33 PM ----------

Examples of filenames:
Code:
mRefresh_2018-06-28_003015.x
mRefresh_2018-06-28_221609.tar
mRefresh_2018-06-29_003058.tar
mRefresh_2018-06-29_003058.x
mRefresh_2018-06-29_220654.tar
mRefresh_2018-06-30_003043.tar
mRefresh_2018-07-01_222747.tar
mRefresh_2018-07-02_003030.tar

So, based on this list, I want the name of the last 4 files.

Code:
ls --version
ls (GNU coreutils) 8.12

Assuming (1) that there are other files in that directory but you are only interested in those that start with mRefresh; and (2) that the timestamp in the filename is the one you should be relying on:
Code:
ls -r mRefresh_* | sed '/\.x$/,$ d'

The files will be in reverse order, I'm afraid, but you can fix that if necessary by adding | tac to the pipeline.

Andrew
# 12  
Old 07-05-2018
Hello apmcd47,
Thank you for your post.

All the files in the directory start with mRefresh_
The timestamp doesn't matter to me in my script.
The order doesn't matter to me because I just need to copy them to another directory.
So, your code works for me.

Thank you very much, apmcd47 and all who replied.
# 13  
Old 07-05-2018
Another solution with shell internals, relying on the alphabetic order of the file names
Code:
out=""
for f in *
do
  if [[ $f == *.x ]]
  then
    out=""
  else
# append $f to $out (after a newline if $out is not empty)
    out="${out:+$out
}$f"
  fi
done
echo "$out"

This uses a simple shell string as an output buffer,
otherwise it works like the solution with a bash array in post#3 (by Aia, see my corrections in post#4).

Last edited by MadeInGermany; 07-05-2018 at 01:54 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If condition fails for special charecter

I have a sample server name listed in variable as below: var="server-13" I need to check if the 7th character on $var is number 1 whichenv=`echo "$var"| head -c 7 | tail -c 1` if ]; then echo "9 found" else echo "9 Not Found" fi Output: This works... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. Shell Programming and Scripting

Columns to Rows - Transpose - Special Condition

Hi Friends, Hope all is well. I have an input file like this a gene1 10 b gene1 2 c gene2 20 c gene3 10 d gene4 5 e gene5 6 Steps to reach output. 1. Print unique values of column1 as column of the matrix, which will be a b c (5 Replies)
Discussion started by: jacobs.smith
5 Replies

3. Shell Programming and Scripting

If condition matching with special chars

Hi, I have file #cat drivers.txt fcs0 fcs1 vscsi1 vscsi2 In this i need to check the availabality of "fcs" or "vscsi" alone not vscsi0,fcs1 I tried with "if condition" but it is not working. cat drivers.txt| while read ADAP do echo "Checking for $ADAP" if ;then echo "FC... (9 Replies)
Discussion started by: ksgnathan
9 Replies

4. Shell Programming and Scripting

Print every 5 lines with special condition

Hi Friends, I have an input file like this chr1 100 200 chr1 200 300 chr1 300 400 chr1 400 500 chr1 500 600 chr1 600 700 chr1 700 800 chr1 800 900 chr1 900 920 chr1 940 960 I would like to get the first line's second column and the fifth line's 3rd column as one single line. This... (13 Replies)
Discussion started by: jacobs.smith
13 Replies

5. Shell Programming and Scripting

to add special tag to a column based on column condition

Hi All, I have following html code <TR><TD>9</TD><TD>AR_TVR_TBS </TD><TD>85000</TD><TD>39938</TD><TD>54212</TD><TD>46</TD></TR> <TR><TD>10</TD><TD>ASCV_SMY_TBS </TD><TD>69880</TD><TD>33316</TD><TD>45698</TD><TD>47</TD></TR> <TR><TD>11</TD><TD>ARC_TBS ... (9 Replies)
Discussion started by: ckwan
9 Replies

6. UNIX for Dummies Questions & Answers

Strings with Special chars in IF condition

I was trying to run a code to check if a fax number is empty or not. for that, I've written the following code which is throwing an error. #!/bin/ksh fax= "999-999-9999" if ; then fax_no="000-000-0000" else fax_no=$fax fi echo $fax_no And I get the... (7 Replies)
Discussion started by: hooaamai
7 Replies

7. Shell Programming and Scripting

How to compare 2 files & get only few columns based on a condition related to both files?

Hiiiii friends I have 2 files which contains huge data & few lines of it are as shown below File1: b.dat(which has 21 columns) SSR 1976 8 12 13 10 44.00 39.0700 70.7800 7.0 0 0.00 0 2.78 0.00 0.00 0 0.00 2.78 0 NULL ISC 1976 8 12 22 32 37.39 36.2942 70.7338... (6 Replies)
Discussion started by: reva
6 Replies

8. Shell Programming and Scripting

How to insert a character in line in Special condition?

Hi, I have a log file generated by a tool which has the following look : /tmp/releases/directory/datefilename1_release_date.zip /tmp/releases/directory/datefilename2_release_date.zip /tmp/releases/directory/datefilename3_release_date.zip... (8 Replies)
Discussion started by: bhaskar_m
8 Replies

9. Shell Programming and Scripting

How to filer a line in special condition?

Hi, I hava log file which has the following output : Created RELEASE in dist/filename_release_1_0_20090210.zip Created RELEASE in dist/filename_release_1_1_20090210.zip Created RELEASE in dist/filename_release_1_3_20090210.zip Created RELEASE in... (6 Replies)
Discussion started by: bhaskar_m
6 Replies

10. UNIX for Dummies Questions & Answers

Trying to retreive and compare value

Hi All, I'm have a file test.txt that looks like this 1939399393 03094994949 948383 I have to check whether the first character in the first line is 1 or not. I have tried the following option but it seems to fail head -1 test.txt | grep ^1 but it seems to display the entire... (3 Replies)
Discussion started by: omahablues
3 Replies
Login or Register to Ask a Question