Read a file name from a text file and save it in a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read a file name from a text file and save it in a variable
# 1  
Old 02-21-2013
Bug Read a file name from a text file and save it in a variable

i have a text file consists of different file names like:

line 1: lib/libIMb.so message broker file
line 2: lil/imbdfg.lil message broker file

i need to extract libIMb.so and imbdfg.lil files from those lines and save them in a variable. so that i can search for those files in different directories.
# 2  
Old 02-21-2013
Use Parameter Substitution in Korn shell or POSIX shell:
Code:
#!/bin/ksh

while read line
do
        file=${line#*/}
        file=${file%% *}
        echo $file
done < filename

Or use sed
Code:
sed 's/.*\///; s/ .*//' filename

This User Gave Thanks to Yoda For This Post:
# 3  
Old 02-21-2013
You could try this:

Code:
set -- $(awk 'NR<3 { sub(".*/","",$1); print $1 }' names.txt)

$1 is file1 $2 is file2
This User Gave Thanks to Chubler_XL For This Post:
# 4  
Old 02-21-2013
Bug

Thank you for the replies. But i am not getting the required output when i try these. May be i am not clear. sorry for that.
I have a text file readme.txt, which consists of all these filenames in between the lines.

lib/libImbCmdLib.so Message File
lib/libImbRdl.so Message File
lib/libJDBCType4JNISinglePhaseTrxn.so Message File
lib/libJMSJNILocalTrxn.so Message File
lib/libMQLibrary.so Message File

from the above lines i need to extract the string with the filename like libImbCmdLib.so
libImbRdl.so
libJDBCType4JNISinglePhaseTrxn.so and save in a variable.

By reading that variable i need to find this filename in a directory1 and if match found need to copy that in directory2. This has to repeat for all the filenames in the textfile. Any logic and script will be appreciated. Thanks.
# 5  
Old 02-21-2013
Code:
sed -e 's:^.*/::' -e 's/ .*//' readme.txt | while read file
do
    find directory1 -name $file -exec cp {} directory2/
done

This User Gave Thanks to Chubler_XL For This Post:
# 6  
Old 02-21-2013
Thanks for the reply. But i m getting error:

find: missing argument to '-exec'
find: missing argument to '-exec'
find: missing argument to '-exec'
find: missing argument to '-exec'
# 7  
Old 02-21-2013
Sorry missed \; on end of find statement:

Code:
sed -e 's:^.*/::' -e 's/ .*//' readme.txt | while read file
do
    find directory1 -name $file -exec cp {} directory2/ \;
done

This User Gave Thanks to Chubler_XL 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

Read a file and save every word in a variable to use

Hello there so i have a txt file containing word like "one two three four plus four five six". I want to save every word in the file into a variable, and then use that variable to generate real numbers and apply the arithmetic value on them. example: the txt files becomes 123 + 456 and... (10 Replies)
Discussion started by: azaiiez
10 Replies

2. Programming

Extract text from file and save as individual file

Dear All I am using the following shell script to extract the columns from the file. for filename in *.rpt do awk -F"\t" ' BEGIN {OFS="|"} FNR > 0 {print $1,$2,$3,$5,FILENAME} ' $filename >> output.txt done However, the script works fine. Instead of saving the single... (4 Replies)
Discussion started by: bala06
4 Replies

3. Shell Programming and Scripting

Perl: How to read text from file and process $variable in that data too.

In the hello.htm have the sentenses: Hello $name How are you? The perl script: $name = "David"; open(HEADER,"hello.htm"); while(<HEADER>) { $html .= $_; } close(HEADER); print "$html";I making something about template. But it can't process the $name variable. (4 Replies)
Discussion started by: natong
4 Replies

4. UNIX for Advanced & Expert Users

How to read a text file and assign the values in the same to a variable in loop

Hi, I have a text file with multiple lines, each having data in the below format <DOB>,<ADDRESS> I have to write a script which reads each line in the text file in loop, assign the values to these variables and do some further processing in it. Using the following code prints the values... (12 Replies)
Discussion started by: manishab00
12 Replies

5. Fedora

How to read a text file and assign the values in the same to a variable in loop

Hi, I have a text file with multiple lines, each having data in the below format <DOB>,<ADDRESS> I have to write a script which reads each line in the text file in loop, assign the values to these variables and do some further processing in it. Using the following code prints the... (1 Reply)
Discussion started by: manishab00
1 Replies

6. Shell Programming and Scripting

Read file and for each line replace two variables, add strings and save output in another file

Hi All, I have a file, let's call it "info.tmp" that contains data like this .. ABC123456 PCX333445 BCD789833 I need to read "info.tmp" and for each line add strings in a way that the final output is put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)' where XXX... (5 Replies)
Discussion started by: Andy_ARG
5 Replies

7. Shell Programming and Scripting

Perl:Read single value from text file and assign to variable

Hello All, A part of my very basic perl code requires me to read a single value from a text file. The file output is the following: Reading image ... done IMAGEREGION=0x0x0-256x162x256 VOXELDIMENSION=0.9375000000x1.2000000477x0.9375000000 VOXELNUMBER=10527001... (7 Replies)
Discussion started by: ncl
7 Replies

8. Shell Programming and Scripting

Data fetched from text file and save in a csv file

Hi i have wriiten a script which fetches the data from text file, and saves in the output in a text file itself, but i want that the output should save in different columns. I have the output like: For Channel:response_time__24.txt 1547 data points 0.339 0.299 0.448 0.581 7.380 ... (1 Reply)
Discussion started by: rohitkalia
1 Replies

9. Shell Programming and Scripting

Read popup message and save it in file

Hi, I am trying to automate one of the application using IE:Auotmation in perl My web application has few text fields and 2 buttons "Save Changes" and "Discard Changes".I have written code to enter values to the text fields fetching from input file and click the button "Save Changes".As soon as... (0 Replies)
Discussion started by: jyo123.jyothi
0 Replies

10. Shell Programming and Scripting

Read file from within AWK and save $1 to a variable

Hi I am very new to NAWK programming so this question is probably going to sound really stupid: I have a NAWK script which contains a DO loop. During each loop it runs a FORTRAN program which in turn generates two output files , each one containing 2 integer variables. I would appreciate it... (8 Replies)
Discussion started by: robbiegregg
8 Replies
Login or Register to Ask a Question