Visit Our UNIX and Linux User Community

cut the variable from the line and use it to find the file and read the content of that file

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting cut the variable from the line and use it to find the file and read the content of that file
# 1  
Old 10-29-2011
cut the variable from the line and use it to find the file and read the content of that file


I am working on one script..I am having files in the below format

file 1 (each line is separated with : delimeter)

Now I want from file 1 that most left part of the first line will store in one variable...i.e in the below way


and there is one folder which is having number of files that will start with the second varible of each line of file1

like TEST is a folder which is having

Now I want when second variable store in b like above example I want to use that variable to find that file in TEST folder and will read the content of that file.

these SPLASH_release.txt files always keep single line like SPLASH_11_26_00_RC01

Please help me on this.

# 2  
Old 10-29-2011
Where are you stuck?


Last edited by ahamed101; 10-29-2011 at 06:13 AM..
# 3  
Old 10-29-2011
I am not able able to understand how I I can find the file with variable

like if variable stores b=SPLASH how I will search the file start with string SPLASH_Release.txt in number of files and read the content of that file

all files would be in TEST folder


I have tofind the exact filer in that folder with the valuue store in in variable b and read the content of that file.

Please help me.
# 4  
Old 10-29-2011
while IFS=":" read v1 v2 v3
    [ -f "$test_path/${v2}_Release.txt" ] && cat $test_path/${v2}_Release.txt || echo "${v2}_Release.txt not exists"
done< infile

# 5  
Old 10-30-2011
Thanks for your help

I was able to store the value of splash(2nd field) in variable .I want here in single command to search the file with that variable and store the content of that file in the variable..I don't want to create a loop and make another file.

something like that
find / - name 'Metallica*' -exec ls -l {\}\ \;

b=`find / -name core -exec cat '{}'`

find /rohit/TEST/ -name "$b" -print

Please help me on this

---------- Post updated at 06:50 AM ---------- Previous update was at 06:39 AM ----------

Can you verify whether this code will work or not.

LOCATE=`find /rohit/TEST/ -name "$b_Release.txt" -print`
echo $LOCATE
for i in `cat "$LOCATE"`
do echo $i

---------- Post updated 10-30-11 at 12:41 AM ---------- Previous update was 10-29-11 at 07:50 AM ----------

Can someone help on the above problem please?

Thanks in advance

Last edited by Franklin52; 10-31-2011 at 07:09 AM.. Reason: Code tags
# 6  
Old 10-30-2011
What is your ultimate goal? Do you want to display the contents of the file or store it in a variable?

LOCATE=$( find /rohit/TEST/ -name "${b}_Release.txt" )


PS : You have like 45 posts and still not using code tags! Please use code tags for better readability!
# 7  
Old 10-30-2011
Thanks for your help.I will try the code you gave it to me and also take care of your instruction given to me regarding tags

LOCATE=$( find /rohit/TEST/ -name "${b}_Release.txt" )

My ulitmate goal is to use 2nd filed value to search the filein folder and read the content

In above code second value is SPLASH i will use it to find the file in TEST folder and read the content of that file and store it in variable.But it should be in single command.


Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cut line from searched file if grep find neighbor columns

Hello All, While searching for the question, I found some answers but my implementation is not giving expected output. I have two files; one is sourcefile, other is named template. What I want to do is to search each line in template, when found all columns, cut the matching line from source... (4 Replies)
Discussion started by: baris35
4 Replies

2. Shell Programming and Scripting

Read and concatenate content file and file name

Hi all, i need a bash script. I have a 3 file named Milano, Torino, Firenze Into file i have: Milano Marco Luca Giorgio Michele PatrizioTorino Marco Giulio Emilio MicheleFirenze Luca Giorgio Marco Saverio EmilioThe output should be a all_city.csv file like: (3 Replies)
Discussion started by: kamose
3 Replies

3. Shell Programming and Scripting

Extract a part of variable/line content in a file

I have a variable and assigned the following values ***XYZ_201519_20150929140642_20150929140644_211_0_0_211 I need to read this variable from backward and stop read when I get first underscore (_) In this scenario I should get 211 Thanks Kris (3 Replies)
Discussion started by: mkris
3 Replies

4. Shell Programming and Scripting

Do While Loop + Read From File + assign line to a variable

Hello, I am using below code for reading from a file and assigning the values to a variable , but it is loosing the value after the loop , please suggest to retain the value of the variable after the loop , while IFS=: read -r line do set $dsc=$line echo 'printing line variable ' $line... (1 Reply)
Discussion started by: ParthThakkar
1 Replies

5. Shell Programming and Scripting

While read line ignores the '\' in file content

I need to read temp.$i file content line by line through while loop but somehow the '\' do not appear in output.. Can someone guide how to read this exact content line by line in unix : if then cat temp.$i | head -1 # the file content appears fine while... (13 Replies)
Discussion started by: Prev
13 Replies

6. Shell Programming and Scripting

How to read a file line by line and store it in a variable to execute a program ?

Hello, I am quite new in shell scripting and I would like to write a little scritp to run a program on some parameters files. all my parameters files are in the same directory, so pick them up with ls *.para >>dirafter that I have a dir file like that: param1.para param2.para etc... I... (2 Replies)
Discussion started by: shadok
2 Replies

7. Shell Programming and Scripting

how to Read a file and assigning each line to a variable?

Friends, I have a file output.txt with values as below: 092307135717 061910135717 I want to know how to read this file and then assign each value to a variable. say like var1=092307135717 var2=061910135717 So that I can use this VAR1 and Var2 in the shell script for further processing.... (3 Replies)
Discussion started by: shyamaladevi
3 Replies

8. Shell Programming and Scripting

shell script to read a line in gps receiver log file and append that line to new file

Hi, I have gps receiver log..its giving readings .like below Trying Connected to localhost. Escape character is '^]'. GPSD,R=1 $GPGSV,3,1,11,08,16,328,40,11,36,127,00,28,33,283,39,20,11,165,00*71... (3 Replies)
Discussion started by: gudivada213
3 Replies

9. Shell Programming and Scripting

Need help with awk - how to read a content of a file from every file from file list

Hi Experts. I need to list the file and the filename comes from the file ListOfFile.txt. Basicly I have a filename "ListOfFile.txt" and it contain Example of ListOfFile.txt /home/Dave/Program/Tran1.P /home/Dave/Program/Tran2.P /home/Dave/Program/Tran3.P /home/Dave/Program/Tran4.P... (7 Replies)
Discussion started by: tanit
7 Replies

10. UNIX for Dummies Questions & Answers

Read and store each line of a file to a variable

Hi all, I'm quite new to unix and hope that someone can help me on this. I'm using csh. Below is what i intend to do. 1. I stored some data in a file. 2. I intend to read the file line by line and store each line of data into a variable, so that i can used it later. Anyone have any... (4 Replies)
Discussion started by: seijihiko
4 Replies

Featured Tech Videos