How to read the variable from awk output?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read the variable from awk output?
# 1  
Old 01-13-2014
Question [Solved] How to read the variable from awk output?

I am reading an xml file with date tag as <Date>Default</Date> using the below command.


Code:
Dt=$(awk -F'[<>]' '/<Date>/{print $3}' /home/test/try.xml

and getting the value from the xml file stored in this variable "Dt"

echo $Dt gives me a value. Dt=Default.

Now according to my requirement, If in the date tag the xml file has "Default" written i need this part of code to copy previos day files(eg-today=20130113, prev_day=20130112)files from one location to another as below or if the date tag finds value as some date as 20130925(<Date>20130925</Date>), it should copy files from one location to another for that date

Code:
if [ $Dt -eq Default ];
then #Use yesterday as copy_date

    copy_date=$prev_day
#here prev_day is a Function to get yesterday's date(which i have prepared and is working fine)
    echo $copy_date
    
else #Use Dt as copy_date

    copy_date=$Dt
    echo $copy_date
fi

cp /home/file1/A$copy_date.*  /var/tmp/file2

Below is the error I am facing.
Code:
awk -F[<>] /<Date>/{print $3} /home/test/try.xml
+ Dt=Default
+ echo Default
Default
+ [ Default -eq Default ]
Rawfilescopy.sh: 41: [: Illegal number: Default
+ copy_date=Default
+ echo Default
Default
+ cp /home/file1/ADefault.* /var/tmp/file2
cp: cannot stat `/home/file1/ADefault.*': No such file or directory

How can i achieve this part to copy prev_day=Default=20130112 as A$prev_day.* or A20130112.* and not ADefault.*???

Last edited by Scott; 01-13-2014 at 06:32 AM.. Reason: Code tags, please....
# 2  
Old 01-13-2014
-eq is a numeric comparison. Use = for strings:

Code:
if [ "$Dt" = Default ];

This User Gave Thanks to CarloM For This Post:
# 3  
Old 01-13-2014
Thanks CarloM. It did work.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Output read variable

Hi all, how read varaible and ouput in colum, e.g. $ echo $VAR1 opc op odi games gopher vcsa abrt I like $ echo $VAR1 opc op odi games gopher vcsa abrt (3 Replies)
Discussion started by: aav1307
3 Replies

2. Shell Programming and Scripting

awk - read from a file and write conditional output

I have a file, which has '|' as separator; I need to read each line from that file and produce output to another file. While reading, I have certain condition on few specific columns (like column3 ='good'); only those lines will be processed. (3 Replies)
Discussion started by: mady135
3 Replies

3. Shell Programming and Scripting

awk question : system output to awk variable.

Hi Experts, I am trying to get system output to capture inside awk , but not working: Please advise if this is possible : I am trying something like this but not working, the output is coming wrong: echo "" | awk '{d=system ("date") ; print "Current date is:" , d }' Thanks, (5 Replies)
Discussion started by: rveri
5 Replies

4. UNIX for Advanced & Expert Users

ls output into a read command as a variable

I'm working on a short BASH script on my Ubuntu box that will run powerpoint scripts with MS Powerpoint Viewer 2007 via WINE. I can run the presentation when I run it manually but what i'd like to do is have the script look for the newest file then run it. #! /bin/sh # Start the newest... (2 Replies)
Discussion started by: binary-ninja
2 Replies

5. Shell Programming and Scripting

read the variable with awk

final.txt file contains SY10020021 SY10023077 3199 4 803.815 11884 4 1825.22 2.2707 say set FIRSTLINE = SY10020021 set SECONDLINE=SY10023077 cat final.txt | awk '{if($1==${FIRSTLINE} & $2==${SECONDLINE}){print $9}else{print "ll"}}'..............this should give me value... (1 Reply)
Discussion started by: Indra2011
1 Replies

6. Shell Programming and Scripting

using awk for setting variable but change the output of this variable within awk

Hi all, Hope someone can help me out here. I have this BASH script (see below) My problem lies with the variable path. The output of the command find will give me several fields. The 9th field is the path. I want to captured that and the I want to filter this to a specific level. The... (6 Replies)
Discussion started by: Cowardly
6 Replies

7. UNIX for Dummies Questions & Answers

use AWK to read an Environment variable

Dear Unix gurus, Perhaps I'm missing something,.....I'm having the most infernal difficulty getting AWK to read in an environment variable. For example, value=5 awk '{print ""$value""}' should return the number "5". This is not the case. Can somebody help?:confused: cheers (7 Replies)
Discussion started by: tintin72
7 Replies

8. Shell Programming and Scripting

Using 'defaults read' and storing the output in a variable

Hi all, I'm creating a script which uses 'defaults read' to retrieve details from an Info.plist like this; defaults read "/Path/Contents/Info" CFBundleShortVersionString This works fine in Terminal and returns the expected values. Is it possible to use this command in a script, and... (0 Replies)
Discussion started by: davewg
0 Replies

9. Shell Programming and Scripting

use awk to read variable length csv

Any help to read the contents of a variable length csv ....??(using awk) The csv mite look like this : anjali,ram,rahul,mohini,sam,.... and so on ... I need to pick up each name.. Thanks in advance SD (3 Replies)
Discussion started by: shweta_d
3 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