Return the list of file name prefix with first 6 character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Return the list of file name prefix with first 6 character
# 1  
Old 04-15-2014
Return the list of file name prefix with first 6 character

Good day people,

Kindly advice what is the operator/command that I should try out to if I am about to return a list of prefix of my filename with first 6 character.

I understand I could use sed to retrieve the first 6 charter itself.
but i wonder if there is any aix command allow me to loop through the directory and put the prefix into a list instead of writing a function that will do a tedious looping.

My intention is to get a list of year-month from my directory.
Assuming in my directory, I am having following files:

Code:
20140101.txt
20140102.txt
20131201.txt
20131104.txt

and an array variable is returning

Code:
var[0] = 201401
var[1] = 201312
var[2] = 201311

Appreciate for your advice,

Moderator's Comments:
Mod Comment Please use code tags and also do not post generic things in the AIX forum, thanks.

Last edited by zaxxon; 04-17-2014 at 03:51 AM..
# 2  
Old 04-15-2014
You could try dumping the filenames directly into an array. I'm assuming that you're using the korn shell:

Code:
set -A FILES $(ls 201*.txt)

Then to display them minus the .txt extension:

Code:
echo ${FILES[0]%.*}

# 3  
Old 04-15-2014
Like this?

Code:
cd /path/to/files
ARRAY=( $(ls|sort) )
C=0 	# counter
while [ $C -lt "${#ARRAY[@]}" ];do
	echo "* $C -- ${ARRAY[$C]}"
	((C++))
done

hth
# 4  
Old 04-16-2014
If available:
Code:
var=( $(ls -1 *.txt|colrm 6) )

# 5  
Old 04-17-2014
I think RudiC meant colrm 7 instead of colrm 6, but even with that, it doesn't get rid of duplicates in the resulting array.

The following script shows what happens when setting the array using colrm 6, colrm 7 | sort -nru, and for systems where colrm is not available, sed | sort -nru:
Code:
#!/bin/ksh
function pvar {
	i=0
	while [ $i -lt ${#var[@]} ]
	do	printf "var[%d]=%s\n" $i "${var[i]}"
		((i++))
	done
}
printf "Using colrm 6:\n"
var=( $(ls *.txt | colrm 6) )
pvar

printf "\nUsing colrm 7 & sort -nru:\n"
var=( $(ls *.txt | colrm 7 | sort -nru) )
pvar

printf "\nUsing sed & sort -nru:\n"
var=( $(ls *.txt | sed 's/\(......\).*/\1/' | sort -nru) )
pvar

and, on a system with both colrm and sed when run in a directory containing the four text files shown in the 1st message in this thread, produces the output:
Code:
Using colrm 6:
var[0]=20131
var[1]=20131
var[2]=20140
var[3]=20140

Using colrm 7 & sort -nru:
var[0]=201401
var[1]=201312
var[2]=201311

Using sed & sort -nru:
var[0]=201401
var[1]=201312
var[2]=201311

Array variables aren't defined by the standards, but this script should with with any ksh or bash. It was tested with both ksh and bash on OS X Version 10.9.2.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract Uniq prefix from a start and end prefix

Dear All, assume i have a file with content: <Start>6000</Start> <Stop>7599</Stop> the output is: 6000 7000 7100 7200 7300 7400 7599 how should we use any awk, sed, perl can do this task, means to extract the uniq prefixes from the start and stop prefix. Thanks Jimmy (3 Replies)
Discussion started by: jimmy_y
3 Replies

2. Shell Programming and Scripting

Common prefix of a list of strings

Is there a simple way to find the longest common prefix of a space-separated list of strings, optionally by field? For example, given input: "aaa_b_cc aaa_b_cc_ddd aaa_b_cc aaa_b_cd"with no field separator, output: aaa_b_cwith _ field separator, output: aaa_bI have an awk solution which... (1 Reply)
Discussion started by: CarloM
1 Replies

3. Shell Programming and Scripting

AWK adding prefix/suffix to list of strings

75 103 131 133 138 183 197 221 232 234 248 256 286 342 368 389 463 499 524 538 (5 Replies)
Discussion started by: chrisjorg
5 Replies

4. UNIX for Dummies Questions & Answers

Grep to return lines not containing a character

Hello , this is my first topic cause I need your little help:( I got .txt file, and I want to find lines without letter 'a', so im writing: grep "" list.txt (list.txt is the file of course) and i have no idea why it's not working because it shows lines with a. (1 Reply)
Discussion started by: bbqtoss
1 Replies

5. Shell Programming and Scripting

Return part of string after X numbers of a character

My input strings look something like this: /dev/vs/dsk/group/vol I want to print just "group" out of the line... which is always found between the 4th and 5th "/" in the string. I figure I have to use awk, sed, or some combination to do this, but I've searched the forums and can't find... (2 Replies)
Discussion started by: ltricarico
2 Replies

6. UNIX for Dummies Questions & Answers

how to search and list file with wildcard character

hi, I want to search all files in the current working direcotry and to print in comma (,) seperated output. But I have two patterns to search for. Files will be in ABC20100508.DAT format. Search should happen on the format (ABC????????.DAT) along with date(20100508). I can do a ls... (2 Replies)
Discussion started by: anandapani
2 Replies

7. Shell Programming and Scripting

return a list of unique values of a column from csv format file

Hi all, I have a huge csv file with the following format of data, Num SNPs, 549997 Total SNPs,555352 Num Samples, 157 SNP, SampleID, Allele1, Allele2 A001,AB1,A,A A002,AB1,A,A A003,AB1,A,A ... ... ... I would like to write out a list of unique SNP (column 1). Could you... (3 Replies)
Discussion started by: phoeberunner
3 Replies

8. Shell Programming and Scripting

sed and character return problem

Hi, I have a problem with sed. It doesn't recognize the "\n" character. It substitudes an "n", instead of introducing a new line. This doesn't happend with print $ print "test \n \n" (it deos introduce two lines after hello) (AIX) $ sed s/bc/\n/g test.1 >test.2 $ cat test.1 bcdefg... (3 Replies)
Discussion started by: Santiago
3 Replies

9. Shell Programming and Scripting

Substituting carriage return followed by newline character - HELP

-------------------------------------------------------------------------------- Hi All I have a field being returned from the DB that when opened in Vi shows a ^M before the rest of the field is displayed on the next line. I need it so that the only newline character is the end of the... (14 Replies)
Discussion started by: djkane
14 Replies

10. Shell Programming and Scripting

Test return character.

Hi, in my korn shell I have this code: typeset -uL1 rc read rc?"Insert Y=Yes (default) or N=No >>" If I press enter without value I wish to set rc=Y. This is my default. This test: if ] then .... Do not work. I hope in your help. Thanks in advance. Regards, Giovanni (3 Replies)
Discussion started by: gio123bg
3 Replies
Login or Register to Ask a Question