Variable value substitution issue with awk command issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable value substitution issue with awk command issue
# 1  
Old 03-01-2013
Code Variable value substitution issue with awk command issue

Hi All,

I am using the below script which has awk command, but it is not returing the expected result. can some pls help me to correct the command.

The below script sample.ksh should give the result if the value of last 4 digits in the variable NM matches with the variable value DAT. The script is written with awk command as shown below but the DAT variable value is not substituting in the awk command. Please help me how to fix this issue. Expected output provided below.

sample.ksh
========
Code:
#!bin/ksh
DAT="0301"
NM="xyzxyz.4560301"
echo $NM | awk '/$DAT$/  { print $1 }'

expected output:
=============
Code:
xyzxyz.4560301


Thanks in Advance
G.K.K

Last edited by Franklin52; 03-01-2013 at 03:08 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 03-01-2013
Code:
echo $NM | awk -v D="$DAT" ' $0 ~ D { print $1 }'

Also fix your shebang:
Code:
#!/bin/ksh

This User Gave Thanks to Yoda For This Post:
# 3  
Old 03-01-2013
Quote:
Originally Posted by bipinajith
Code:
echo $NM | awk -v D="$DAT" ' $0 ~ D { print $1 }'

Also fix your shebang:
Code:
#!/bin/ksh

Thanks for the REply.

But that awk command will work where ever the DT variable value exists in the string NM. But i want to check against only last 4 digits in the variable NM. If it matches then it should return the entire variable value NM. Else it should return nothing.

Example: NM="xyzxyz.0301456" , in this string last four digits value is not matching with the DT value 0301. If i use the above awk command then it will still return the result. But i want to check against only las4 4 digits.
# 4  
Old 03-01-2013
As bipinajith write, you can not use a variable directly in your awk code
You need to declare a variable. This can be done before the code with -v or after the code like this.
The D$ should march only end of line
Code:
echo $NM | awk -v D="$DAT" '$0~"D$" {print $1}'
echo $NM | awk '$0~"D$" {print $1} D="$DAT"'

If NM do contain multiple lines, you should add quotes to it:
Code:
echo "$NM" | awk '$0~"D$" {print $1} D="$DAT"'

PS after posting more than 40 post here, you should know how to use code tags to.

Last edited by Jotne; 03-01-2013 at 03:05 AM..
# 5  
Old 03-01-2013
Quote:
Originally Posted by Jotne
As bipinajith write, you can not use a variable directly in your awk code
You need to declare a variable. This can be done before the code with -v or after the code like this.
Code:
echo $NM | awk -v D="$DAT" '$0~"D$" {print $1}'
echo $NM | awk '$0~"D$" {print $1} D="$DAT"'

If NM do contain multiple lines, you should add quotes to it:
Code:
echo "$NM" | awk '$0~"D$" {print $1} D="$DAT"'

PS after posting more than 40 post here, you should know how to use code tags to.
Thanks I understood.

But how to check the variable value DAT against the last 4 digits of variable value NM using awk command.

---------- Post updated at 11:08 PM ---------- Previous update was at 11:06 PM ----------

Quote:
Originally Posted by Jotne
As bipinajith write, you can not use a variable directly in your awk code
You need to declare a variable. This can be done before the code with -v or after the code like this.
The D$ should march only end of line
Code:
echo $NM | awk -v D="$DAT" '$0~"D$" {print $1}'
echo $NM | awk '$0~"D$" {print $1} D="$DAT"'

If NM do contain multiple lines, you should add quotes to it:
Code:
echo "$NM" | awk '$0~"D$" {print $1} D="$DAT"'

PS after posting more than 40 post here, you should know how to use code tags to.
Thanks , I understood.

But here i want to check the last 4 digits only of a variable value NM. So, how to correct the syntax for below code:
echo "$NM" | awk '$0~"D$" {print $1} D="$DAT"'
# 6  
Old 03-01-2013
Code:
echo "$NM" | awk 'substr($0,lenght($0)-3,4)==D {print $1}' D="$DAT"

PS no need to quote message directly above you.
# 7  
Old 03-01-2013
All solutions above rely on the fact that DAT's length is 4. Why not use that fact and run
Code:
$ echo "$NM" | awk '$0 ~ D"$"' D="$DAT"
xyzxyz.4560301

which is close to Jotne's first proposal with some typos corrected.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue using empty variable in grep command

Hi, I'm writing a shell script and trying to grep a variable value, it works fine as long as there is a value in /tmp/list.out which is captured in $DSK but sometimes the file tends to be empty and that is where I'm having an issue while using grep which returns nothing. I know I can use something... (15 Replies)
Discussion started by: mbak
15 Replies

2. Shell Programming and Scripting

Issue when printing filename through cygwin using a variable with awk

Hi, If i were to do this an print out the file, it will show as it is in the command $ awk '/Privilege Use/ {P=0} /Object Access/ {P=1} P' AdvancedAudit.txt Object Access File System No Auditing Registry No Auditing Kernel... (1 Reply)
Discussion started by: alvinoo
1 Replies

3. Shell Programming and Scripting

awk command issue

Hi All, I have one file with below content Post1:uri Post2:urieop Post3:urtei I am trying to read each word seprated by delimiter with below command Value1=$(awk -F":" '{print $1}' $HSFILE) Value2=$(awk -F":" '{print $2}' $HSFILE) echo $Value1 echo $Value2 It is... (5 Replies)
Discussion started by: sharsour
5 Replies

4. Shell Programming and Scripting

Issue in using variable within sed command

Hi All, I am trying to use a variable within the sed command but I am not able to get the output. When I am using the following command (without variable) its working fine: sed -n '/2011\/12\/10 18:11:11./,$p' < Log.txt > Delta_Log.txt But when I am putting the value 2011\/12\/10... (4 Replies)
Discussion started by: acoomer
4 Replies

5. Shell Programming and Scripting

Issue with AWK using a variable

Hi, I am doing an AWK in ksh as below with the string to search to be read from variable but for some reason there is no output. It works when I hard code it. awk 'substr($0,22,6)=="${VAR}"' XXX.txt' >YYY.txt On reading other posts I tried below option, 'substr($0,22,6)=="/"${VAR}/""' ... (3 Replies)
Discussion started by: junnujayz
3 Replies

6. Shell Programming and Scripting

AWK variable substitute issue

dear, I have below file called folderlist.txt # ParentFolder environment_flag SubFolders triss 1 checksum bookstructure 1 fx 1 checksum_GMDB I have a script which which will create the folders under... (3 Replies)
Discussion started by: manas_ranjan
3 Replies

7. Shell Programming and Scripting

AWK Variable assignment issue Ksh script

Hi There, I am writing a ksh script which assigns variable values from file "A" and passes that variables to file "B". While passing the parameters an additional "$" sign is being assigned to awk -v option. Could any one help me with this please. #!/bin/ksh head -1... (3 Replies)
Discussion started by: Jeevanm
3 Replies

8. Shell Programming and Scripting

Issue in substitution

Hi , I have have file which has following structure 01aaaa88888000-9999 01ssss77777000-0991 01ssss7777700000991 02ssss7777700000991 The record 01 is corrupt as value from 12th field to 19th should be positive or start with - however it is 000-9999 it should be -0009999 i need to... (4 Replies)
Discussion started by: test_user
4 Replies

9. Solaris

Variable Substitution Issue

#!/bin/ksh VAR_ONE=HELLO TEMP=ONE echo $VAR_${TEMP} ## Output is: ONE Hi, I want the output to echo HELLO and not ONE as the above script does. I know I am missing something with dollar substitution. Can anyone help me out ? Thanks. Cal (4 Replies)
Discussion started by: calredd
4 Replies

10. UNIX for Dummies Questions & Answers

Variable for -name causing issue in Find command

Hi there, I'm trying to find files that are greater then 30 days old, zip them and move to a different directory. I'm encountering an issue passing a variable (FilesToFind) to name within the find command. Here's the code I'm running: #! /usr/bin/sh FileDir=/home/ariba... (2 Replies)
Discussion started by: ParNone
2 Replies
Login or Register to Ask a Question