Storing cutted filenames into variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Storing cutted filenames into variable
# 1  
Old 03-11-2011
Power Storing cutted filenames into variable

Hi,
I'd like to write a script which works on various files file1.jpg, file2.jpg ..... These files are splitted and their names are something like file[1-9]_[1-9].XXX. I'd like to merge them and convert them at some moment again in file[1-9].XXX also
file1_1.jpg file1_2.jpg ... >file1.pdf
file2_1.txt file2_2.txt ... >file2.pdf

I'd use for loop and I need there to specify the variable - the first part of the filename. I'd write
for file in *[#-#]_*.jpg
do for i in {#-#}
do convert .....
done
done

I don't know how many files I'd use for the conversion in advance because it varies, I even't don't know the names file1, file2 and their amount. I'd need to get it know from the folder, somehow count the amount of file1_xxx files and the amount of file[1-9] files.
How can I do that?
Thank you very much!
# 2  
Old 03-11-2011
This would give you a count of the number of files for each different file extension you've got:

Code:
ls | perl -p -e 's/.+\.(.+?)/$1/' | sort | uniq -c

As for the loop, you can always use an incremental counter and test if the file exists. Say with .jpg files:

Code:
#!/usr/bin/ksh

NUM=1
while [[ -e a1_$NUM.jpg ]];do
  echo "a1_$NUM.jpg exists"
  let  NUM=$NUM+1
done

Don't know if this is what you needed.
# 3  
Old 03-11-2011
:)

GREAT!
Thank you very much!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Passing variable as input & storing output in other variable

I have a below syntax its working fine... var12=$(ps -ef | grep apache | awk '{print $2,$4}') Im getting expected output as below: printf "%b\n" "${VAR12}" dell 123 dell 456 dell 457 Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Discussion started by: sam@sam
5 Replies

2. Shell Programming and Scripting

Storing a variable using sed

I would like to use sed to store a variable. The code is : echo $HEADERREC CUTVAR=$(echo "$HEADERREC"|sed 's/SDV/STR') echo $CUTVAR Output I am getting now: 0012PVGRSCDVSDV 005 00000000000000000000 2014 0.00 sed: Function s/SDV/STR cannot be parsed. Desired... (5 Replies)
Discussion started by: MIA651
5 Replies

3. Shell Programming and Scripting

Storing filenames in an array in shell script

hi, i am writing a shell script in which i read a line in a variable. FNAME="s1.txt s2.txt s3.txt s4.txt s5.txt" i want to create a array and store single file names in a array.. so the array should contain arr="s1.txt" arr="s2.txt" arr="s3.txt" arr="s4.txt" arr="s5.txt" how to... (3 Replies)
Discussion started by: Little
3 Replies

4. Shell Programming and Scripting

Storing o/p of a command to a variable

Hi, I have a ftp script there I want to store the o/p of the below command: sftp -b <batch file> user@password cat <batch file> get /remote/file/path/remote_file_name.csv*.gz /local/path Now the problem is that when I fire this command. Then it gives o/p as: File... (7 Replies)
Discussion started by: dips_ag
7 Replies

5. Shell Programming and Scripting

About storing the value of wc -l into a variable and then using this value in while

Hi all, I m new to this forum. I ma facing onei issue. I have something like this: length= wc -l < b2| awk '{print $1}' where b2 is filename having detauls like: cat b2 abc1 abc4 xyc3 sbdghf4 but when I do echo "$length" it displays nothing Also I am using awk to overcome... (4 Replies)
Discussion started by: student2009
4 Replies

6. Shell Programming and Scripting

Storing value in a variable

Hi Everyone, I have a code which requires to be stored in different variables and I am achiving it like this. HOST=`echo $RMP | cut -f2 -d:` NAME=`echo $RMP | cut -f3 -d:` DIR=`echo $RMP | cut -f4 -d:` TYPE=`echo $RMP | cut -f5 -d:` Is there any other way of storing value... (2 Replies)
Discussion started by: gehlnar
2 Replies

7. Shell Programming and Scripting

storing a command in a variable

how would i go about storing this command in a variable echo "$LINE" | awk -F"|" '{print $1"|"$2"|"$3"}' i have tried FOO = ${command up there} but receive the error FOO: not found aswell as a couple of other attempt but no luck (2 Replies)
Discussion started by: nookie
2 Replies

8. Shell Programming and Scripting

Storing a variable?

I'm writing a bash shell script to backup several mysql databases. This script will run on a daily basis and send a copy to a remote FTP repository. The filenames are in the format DATE.backup.sql. How do I store the DATE variable so I can delete/move/etc the file on the FTP server the next time... (4 Replies)
Discussion started by: hoover90
4 Replies

9. UNIX for Dummies Questions & Answers

Storing the output into a variable

Hi unix gurus, I am trying to store the result of a command into a variable. But it is not getting stored. x='hello' y=echo $x | wc -c but it is giving the output as 0(zero) Pls help me its very urgent (7 Replies)
Discussion started by: ravi raj kumar
7 Replies

10. Shell Programming and Scripting

Storing values in variable

Hi All, Here is the description of the problem. I am scripting for database access using k-shell on solaris box dbaccess <databasename> - << EOF 2>/dev/null | awk 'BEGIN {FS=" "}\ {printf "%s", $1}' | grep -v "^$" | \ read cnt1 OUTPUT TO PIPE cat WITHOUT HEADINGS select count(*) from... (1 Reply)
Discussion started by: matrixmadhan
1 Replies
Login or Register to Ask a Question