Reading data from file and assigning to variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading data from file and assigning to variable
# 1  
Old 12-14-2009
Reading data from file and assigning to variable

I was trying to store the number of lines in a file and store it in a file.after that i want to store the information in a file to a variable which is further used in the if loop to check certain condition.

Code:
#!/bin/bash
cat <file> | wc -l > count.txt
x="$count.txt";
i=10;
if [ "$x" -lt "$i" ]; then
cat file1 > test.txt
else
cat file2 > test.txt
fi

but above script is not working for both conditions.

Please help me

Last edited by pludi; 12-14-2009 at 02:31 PM.. Reason: code tags, please...
# 2  
Old 12-14-2009
I don't really understand what you want to do but there are already some mistakes in your script
Code:
#!/bin/bash
cat <file> | wc -l > count.txt # the '<' and '>' characters are reserved for redirecting output
# syntax should be
wc -l file > count.txt
x="$count.txt"; # Is there a variable named count.txt ? (not valid name for a variable). Should be:
x=$(cat count.txt)
 i=10;if [ "$x" -lt "$i" ]; then
cat file1 > test.txt # is file1 a file? if so, yous should just copy like:
cp file1 test.txt
else
cat file2 > test.txt
fi

But i really don't understand why you want to store the count in a file (except if you want to re-use it in the same or another script). Tell us what you need finally.
The whole thing shorter :
Code:
#!/bin/bash
x=$(wc -l < file)
i=10
if [ "$x" -lt "$i" ]
then cp file1 test.txt
else cp file2 test.txt
fi


Last edited by frans; 12-14-2009 at 06:08 PM.. Reason: Correction, thanks to Scrutinizer
# 3  
Old 12-14-2009
Code:
x=$(wc -l < file)

# 4  
Old 12-15-2009
i was tried to use the command
x=$(wc -l < file)
in the script but when iam excuting getting error as
syntax error at line 2: `(' unexpected

Code :
#!/bin/bash
x=$(wc -l < file)
i=10
if [ "$x" -lt "$i" ]
then cp file1 test.txt
else cp file2 test.txt
fi
# 5  
Old 12-15-2009
Cab you test the script with -x option :
Code:
bash -x path_to_script

# 6  
Old 12-15-2009
original script :
#!/bin/bash
x= $( wc -l < ATPBSC2log.txt-APT )
i=10;
if [ "$x" -lt "$i" ];
then cp /tmp/ATPBSC2log.txt-APT test.txt
else cp /tmp/ATPBSC2log.txt-APT test1.txt
fi

after excuting "bash -x script" output is :
++ wc -l
+ x=
+ 0
s.sh: line 2: 0: command not found
+ i=10
+ '[' '' -lt 10 ']'
s.sh: line 4: [: : integer expression expected
+ cp /tmp/ATPBSC2log.txt-APT test1.txt
# 7  
Old 12-15-2009
There shouldn't be any space around the assignement '=' in line 2. Write
Code:
x=$( wc -l < ATPBSC2log.txt-APT )

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl : reading data from dumper variable

Hi team, # PERL I have Dumper variable in perl and containing the below data and trying to fetch value and name from the reference variable. $VAR1 = { 'retainSysIds' => 'true', 'variables' => , 'name' => , ... (4 Replies)
Discussion started by: giridhar276
4 Replies

2. Shell Programming and Scripting

Assigning file to a variable

Hi, I have a list of files in a directory. Each file has a .txt and a .log extension i.e. file.txt & file.log, file1.txt & file1.log etc. The file with the .log extension may not always exist alongside the file with the .txt extension. I need to copy the .txt file if there is a corresponding... (6 Replies)
Discussion started by: brunlea
6 Replies

3. Shell Programming and Scripting

assigning variable in txt file

Hi all, One of my txt file has common format like . And I need to manually assign variable to "/a/b/c/file1/txt" , which has common text before "Calculated summary file:". I wonder if I can use some command to do that for me, that it read the file and check for that comonn text and assign... (2 Replies)
Discussion started by: emily
2 Replies

4. Shell Programming and Scripting

Reading from a file and assigning to an array in perl

I wrote a simply perl that searched a file for a particualr value and if it found it, rite it and the next three lines to a file. Now I have been asked to check those next three lines for a different value and only write those lines if it finds the second value. I was thinking the best way to... (1 Reply)
Discussion started by: billprice13
1 Replies

5. Shell Programming and Scripting

Bash: Reading a file and assigning variables from file

I have a file that has four values on each line and I'd like to give each column a variable name and then use those values in each step of a loop. In bash, I believe you could use a while loop to do this or possibly a cat command, but I am super new to programming and I'm having trouble decoding... (2 Replies)
Discussion started by: ccorder22
2 Replies

6. Shell Programming and Scripting

Assigning a value as a variable from a text file

I have a txt file output.txt Freq = 1900 L = 159I want to assign the values to a variable so that i can further use it in some other script. like F=1900 Len=159 etc i tried doing something with awk but dosent work F=$(awk 'BEGIN {}/Freq/ {split ($2,a);depth=a};printf "%d\t,... (2 Replies)
Discussion started by: shashi792
2 Replies

7. UNIX for Dummies Questions & Answers

Searching a text file and assigning it to a variable

Hi Gurus, I am new to unix.I have a requirement as below I have text file like a.txt which contains a.txt hi hello process update status Ok to Proceed no issues good data arrangement My requirement here is i need to read the file and check for the words "OK to Proceed" and if... (2 Replies)
Discussion started by: pssandeep
2 Replies

8. UNIX for Dummies Questions & Answers

Assigning value in a text file to a variable

Hi, I need to place a number located in a text file in a variable so I can perform if/then comparison. How would I go about doing this? Using A=awk '{print $2}' maintenance_date.tmp does not seem to work. Thanks (1 Reply)
Discussion started by: mojoman
1 Replies

9. UNIX for Dummies Questions & Answers

Reading from a file and assigning values

HI I have something like this in a file ABC = 1 DEF = 2 GHI = 3 JKL = 4 MNO = 5 QRS = 6 TUV = 7 I need to assign ABC to V_abc (that is to a variable) GHI to V_ghi (that is to another variable) TUV to say V_tuv ... (6 Replies)
Discussion started by: ssuresh1999
6 Replies

10. Shell Programming and Scripting

Reading file and assigning that to Variable

I am missing something here, I have a file which contains only one line and that is either a number or character string. I am trying to read the file and assign that value to a variable and here it seems I am missing something and not getting the expected results... Here is the code : #!/bin/ksh... (2 Replies)
Discussion started by: Vaddadi
2 Replies
Login or Register to Ask a Question