Print filename inside loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print filename inside loop
# 1  
Old 06-24-2009
Print filename inside loop

Im want to print filename inside loop .. the code im using :-


Filename_1=abc_20090623_2.csv.lk
Filename_2=def_20090623_2.csv.lk

i want to extract filename till .csv eg
Filename_1=abc_20090623_2
Filename_2=def_20090623_2

How can i do this inside the for loop

for((k=1;k<=$p;k++))
do
echo "$(eval echo \$FILENAME_$k)" .. working
filename=`echo "$(eval echo \$FILENAME_$k)"`... nt workn
echo $filename;
length=`expr "echo $filename : '.*'"` ... nt wrking
echo $length
substring=`echo "$(eval echo \$FILENAME_$k)" | cut -c1-($length-4)`
echo $substring
done
# 2  
Old 06-24-2009
Quote:
Originally Posted by r_t_1601
Im want to print filename inside loop .. the code im using :-


Filename_1=abc_20090623_2.csv.lk
Filename_2=def_20090623_2.csv.lk

i want to extract filename till .csv eg
Filename_1=abc_20090623_2
Filename_2=def_20090623_2

How can i do this inside the for loop

for((k=1;k<=$p;k++))
do
echo "$(eval echo \$FILENAME_$k)" .. working
filename=`echo "$(eval echo \$FILENAME_$k)"`... nt workn
echo $filename;
length=`expr "echo $filename : '.*'"` ... nt wrking
echo $length
substring=`echo "$(eval echo \$FILENAME_$k)" | cut -c1-($length-4)`
echo $substring
done

filename=`echo "$(eval echo \$FILENAME_$k)"`... nt workn

will work if you add:-
filename=`echo `eval echo \\$FILENAME_$k``...


now for your problem we can use shell variable manipulation:

cat test.txt :
Filename_1=abc_20090623_2.csv.lk
Filename_2=def_20090623_2.csv.lk


code :-

Code:
while read line
do
echo ${line%%.csv*}
done < test.txt

output
Filename_1=abc_20090623_2
Filename_2=def_20090623_2

BR
# 3  
Old 06-24-2009
`echo`eval echo \\$FILENAME_$k`` is not wrking
# 4  
Old 06-24-2009
Quote:
Originally Posted by r_t_1601
`echo`eval echo \\$FILENAME_$k`` is not wrking

examples:-

This code will work:-

Code:
filename_4=test.txt
for i in 4
do
echo $(eval  echo "\${filename_${i}}" )
done


and this also will work:-

Code:
for i in 4
do
echo `eval  echo "\\${filename_${i}}"`
done

for variables

Code:
file=`eval  echo "\\${filename_${i}}"`

or

file=$(eval  echo "\${filename_${i}}")

BR
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

List filename of files only inside a directory (non-recurrsive) on AIX

I wish to list only files along with the absolute path in a given directory on my AiX 6.1 system. Below is the best I could do. ls -p "/app/scripts"/* This gives a a list of all filename along with folder names with absolute path non-recurrsive (without listing files in sub-directories)... (1 Reply)
Discussion started by: mohtashims
1 Replies

2. Shell Programming and Scripting

Use while loop to read file and use ${file} for both filename input into awk and as string to print

I have files named with different prefixes. From each I want to extract the first line containing a specific string, and then print that line along with the prefix. I've tried to do this with a while loop, but instead of printing the prefix I print the first line of the file twice. Files:... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

3. UNIX for Dummies Questions & Answers

Write a while loop inside for loop?

I'm taking a unix class and need to countdown to 0 from whatever number the user inputs. I know how to do this with a while or until loop but using the for loop is throwing me off.... I know I can use an if-then statement in my for loop but can I include a while loop in my for loop? (3 Replies)
Discussion started by: xxhieixx
3 Replies

4. Shell Programming and Scripting

For loop inside awk to read and print contents of files

Hello, I have a set of files Xfile0001 - Xfile0021, and the content of this files (one at a time) needs to be printed between some line (lines start with word "Generated") that I am extracting from another file called file7.txt and all the output goes into output.txt. First I tried creating a for... (5 Replies)
Discussion started by: jaldo0805
5 Replies

5. Shell Programming and Scripting

Replace Filename and text inside of directory

I have a directory that has directories that contain Dir-20111114-xyz and I want to change them to Dir-20111121-xyz. Inside of Dir-20111114-xyz, I have a config.xml file that also contains the date that I need changed from 20111114 to 20111121 I have used sed to replace inside of file not... (4 Replies)
Discussion started by: icculus99
4 Replies

6. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

7. UNIX for Dummies Questions & Answers

How to get redirected filename inside unix script

Hi All, I am having a script which calculate checks the input feed and perform some function. When i am executing this script i am redirecting this to a output file. I want to know the redirected output file name inside my scripts. Is there is any way to get that . like the same way we... (4 Replies)
Discussion started by: arunkumar_mca
4 Replies

8. Shell Programming and Scripting

Howto Print File Path or Print the Filename

I'm trying to clean up my samba share and need to print the found file or print the path of the image it tried to searched for. So far I have this but can't seem to get the logic right. Can anyone help point me in the right direction? for FILE in `cat list`; do if ; then ... (1 Reply)
Discussion started by: overkill
1 Replies

9. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

10. Shell Programming and Scripting

print the filename

#!/bin/ksh for files in `ls *.gz` do gunzip -c $files | awk -v s=$files -F\" '{print s","$6}' done I have tried FILENAME parameter but it did not work please help. (1 Reply)
Discussion started by: harshakirans
1 Replies
Login or Register to Ask a Question