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?
# 1  
Old 06-29-2018
How To Retreive Files With a Special Condition?

Everyday I have to get a list of files in a directory with a special condition and feed this list to a for loop to be processed. Since I do not use Unix all the time, it is tricky for me to get that list of files. So, the question is whether there are commands that will give me the file names with a specific condition. See explanation and example below.

This is what I have to do.
  1. Do ls -l to show all the files
  2. Look for the last file that has a .x at the end
  3. Get all the file names after the last .x files
  4. Type in for x in and then copy-paste each file name separated by a space after the word in

Here is an example:

Type in ls -l and get the following list of files

Code:
A1.b.x
A2.d.e.f
A3.g.h.i
A4.j.x
A5.k.l.m
A6.n.o.p

The last file with a .x suffix is A4.j.x

So, the files after the last .x file are:

Code:
A5.k.l.m
A6.n.o.p

The for loop command will look like the following. I have to manually copy-paste the file names in.

for x in A5.k.l.m A6.n.o.p Is there a way to get the file name list with a command and then feed/pipe it to the for command?

I apologize if this is not clear. Please let me know if more clarification is needed.

Thank you.




Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 06-29-2018 at 01:07 PM.. Reason: Added CODE tags, removed all kinds of formatting.
# 2  
Old 06-29-2018
How about
Code:
for x in $(ls -r A[0-9]* | awk '/x$/ {exit} 1'); do echo $x; done
A6.n.o.p
A5.k.l.m

# 3  
Old 06-30-2018
Example dir to process
Code:
$ ls -1 april/
A1.b.x
A2.d.e.f
A3.g.h.i
A4.j.x
A5.k.l.m
A6.n.o.p

Code:
#!/bin/bash
# script name: after_last_x.sh

# Expects the name of the directory to process
# Script lives outside to avoid being included
cd "$1"

# Place holder for files found
fnames=()

# Start processing the list
for f in $(echo *); do
    # Clear the place holder if the marked file is found
    [[ $f =~ x$ ]] && fnames=() && continue
    # Add to place holder after last marked file
    fnames+=("$f")
done
# Display result
printf "%s\n" ${fnames[@]}


Example output:


Code:
$ bash after_last_x.sh april/
A5.k.l.m
A6.n.o.p

# 4  
Old 06-30-2018
The simple
Code:
for f in *; do

correctly handles filenames with special characters.
Use quotes for correctly printing an array of strings:
Code:
printf "%s\n" "${fnames[@]}"

Compare with
Code:
for f in "${fnames[@]}"; do echo "$f"; done
for f in "$@"; do echo "$f"; done

The @ lets the shell split on array elements (and parameters) despite the quotes.
# 5  
Old 07-02-2018
Hello RudiC,
I tried to run the command you posted

Code:
for x in $(ls -r A[0-9]* | awk '/x$/ {exit} 1'); do echo $x; done

But I got the following error:
Code:
/bin/ls: cannot access A[0-9]*: No such file or directory

Thank you for helping.
# 6  
Old 07-02-2018
What OS and ls version? Are you in the right directory? Do any files starting with "A" and a number exist?
# 7  
Old 07-02-2018
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.

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.
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