Getting value into a variable from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting value into a variable from file
# 1  
Old 03-27-2013
Getting value into a variable from file

Hi Guys ,
Need help with an issue i am facing . I have a text file test.txt with the following content

Code:
 
File: test.txt
------------
4

From this file , i need to take value of 4 into a variable . I am doing it like this

Code:
 
COUNT =`cat test.txt`
echo "$COUNT"

But the variable is showing as null . Can you please help me in initializing the variable .

Thanks in Advance
# 2  
Old 03-27-2013
Would this work for you:
Code:
$ COUNT=`tail -1 test.txt`
$ echo $COUNT
4

This User Gave Thanks to hanson44 For This Post:
# 3  
Old 03-27-2013
Yes It helped ..
Thanks a lot Smilie

---------- Post updated at 01:23 PM ---------- Previous update was at 01:18 PM ----------

Have a small doubt .
While using an iterator , i need to take the fields as seen as below .
But the Iterator is not been displayed . Can you please help with this .

Code:
ITERATOR=0
COUNT=`cat test.txt`
echo "$COUNT"
 
while [ $ITERATOR -lt $COUNT ]
do
   echo $ITERATOR
   echo "$COUNT"
   awk 'NR=="$ITERATOR"{print;exit}' EMAIL_REPORT.CSV >temp_mail.txt
   TO=`cat temp_mail.txt` 
ITERATOR=`expr $ITERATOR + 1`
done


By using set -x , i checked the data been sent .

Code:
+ awk NR=="$ITERATOR"{print;exit} EMAIL_REPORT.CSV

Can you please help with this .
# 4  
Old 03-27-2013
Use
Code:
awk -v iter=$ITERATOR 'NR==iter{print;exit}' EMAIL_REPORT.CSV >temp_mail.txt

or
Code:
sed -n "$ITERATORp'  EMAIL_REPORT.CSV >temp_mail.txt

This User Gave Thanks to PikK45 For This Post:
# 5  
Old 03-27-2013
Thanks a lot . The 2nd one worked prefectly for me
# 6  
Old 03-27-2013
Not sure I understand your logics. You seem to want to put lines 1,2,3 from file EMAIL_REPORT.CSV into individual versions of temp_mail.txt. The first iteration of your loop is in vain, as NR will never assume 0, the value of your iterator. Still awk will read through your entire file!
Why don't you try sth like (on a recent bash/ksh shell)
Code:
 while ((++ITERATOR <= COUNT)); do read LINE; echo $LINE >temp_mail.txt; ...  done < EMAIL_REPORT.CSV

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find matching file in bash with variable file names but consisent prefixs

As part of a bash the below line strips off a numerical prefix from directory 1 to search for in directory 2. for file in /home/cmccabe/Desktop/comparison/missing/*.txt do file1=${file##*/} # Strip off directory getprefix=${file1%%_*.txt} ... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

Calling a variable of variable from a file

Hi All, I have file which have looks like below abc=${def} def=${efg} efg= "this is the actual value" based on "abc" value I have to call "efg" value , Am using below lines but it is not working #!/bin/bash source file.txt echo $abc Please wrap all code, files, input &... (5 Replies)
Discussion started by: Prashanth.K
5 Replies

3. Shell Programming and Scripting

To print value for a $variable inside a $variable or file

Hi guys, I have a file "abc.dat" in below format: FILE_PATH||||$F_PATH TABLE_LIST||||a|b|c SYST_NM||||${SRC_SYST} Now I am trying to read the above file and want to print the value for above dollar variables F_PATH and SRC_SYST. The problem is it's reading the dollar variables as... (5 Replies)
Discussion started by: abcabc1103
5 Replies

4. Shell Programming and Scripting

Two question: remove from the other variable or file to get another variable or file

Question 1: A="a b c d e f g 1 2 3 4 5" B="c 3" get C="a b d e f g 1 2 4 5" Question 2: cat file1 a b c (6 Replies)
Discussion started by: yanglei_fage
6 Replies

5. Shell Programming and Scripting

Modify a file by another file: add new line and variable after string is found

hello, I have problem with writing/adjusting a shell script. I searched forum and unfortunately couldn't write scipt based on the information I found. I never wtire such so it's hard for me and I do need to modify one script immediately. case looks like: 1. 'file' that needs to be modified... (3 Replies)
Discussion started by: bipbip
3 Replies

6. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

7. Shell Programming and Scripting

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

Hi, I am working on one script..I am having files in the below format file 1 (each line is separated with : delimeter) SPLASH:SPLASH:SVN CIB/MCH:MCH:SVN Now I want from file 1 that most left part of the first line will store in... (6 Replies)
Discussion started by: rohit22hamirpur
6 Replies

8. Shell Programming and Scripting

variable within a variable from file

script below sets variable "var". Then sets "var2" (which contains "var" whithin it, see test.config file) from config file. test.sh ---------------------------- #!/bin/sh var="varvalue" while read line do var2=${line} done <./test.config echo "var value is: $var" echo... (2 Replies)
Discussion started by: bcnjc
2 Replies

9. Shell Programming and Scripting

Split variable length and variable format CSV file

Dear all, I have basic knowledge of Unix script and her I am trying to process variable length and variable format CSV file. The file length will depend on the numbers of Earnings/Deductions/Direct Deposits. And The format will depend on whether it is Earnings/Deductions or Direct Deposits... (2 Replies)
Discussion started by: chechun
2 Replies

10. Shell Programming and Scripting

Reading variable from file variable values

Hi, Here is the output of lpstat. I would like to read value of Queue which is(abxxxxb1)and status that is DOWN in first line. i dont care what is in second line. any one can help me.thanks Queue Dev Status Job Files User PP % Blks Cp Rnk ------- ----- ---------... (5 Replies)
Discussion started by: sagii
5 Replies
Login or Register to Ask a Question