Sponsored Content
Top Forums Shell Programming and Scripting What is wrong with my script? Post 302774685 by jim mcnamara on Saturday 2nd of March 2013 10:45:07 PM
Old 03-02-2013
You cannot embed shell variables in an awk statement that way.
Generally
Code:
var=foo
awk -v myvar=$var '{.....}' somefile

-v [awkvariable name]=$variable

It looks like you are trying to get a substring of a variable. Some shells like bash provide that.

Code:
${string:position:length}

example:
Code:
a=123456789
len=${#a}   # length of the variable
len=$(( $len -1 4 ))
echo ${a:0:len}   # get string minus 4 characters

It is much less efficient in awk, but you can it this way (one of several) if you are not using the bash shell:
Code:
var=123456789
newvar=$(echo "$s" | awk  '{print substr($0,1,length($0)-4) }')

This User Gave Thanks to jim mcnamara For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

What is wrong with my script?

Hey guys, can someone help me with this script... #!/bin/sh dir=`pwd` for i in *.f do if then M=`wc -l < ${i} sed -e 's://.*::' < ${i} | \ (echo "//${i} -"$M ; cat - ) > $i.tmp chmod 700 $i ; mv ${i}.tmp $i ... (6 Replies)
Discussion started by: Lem2003
6 Replies

2. Shell Programming and Scripting

What is wrong with this script?

I keep getting errors messages for the "else" statement at line 81? #!/bin/ksh ######### Environment Setup ######### PATH=/gers/nurev/menu/pub/sbin:/gers/nurev/menu/pub/bin:/gers/nurev/menu/pub/mac :/gers/nurev/menu/adm/sbin:/gers/nurev/menu/adm/bin:/gers/nurev/menu/adm/mac:/ge... (8 Replies)
Discussion started by: heprox
8 Replies

3. Shell Programming and Scripting

What's wrong with this script

I am trying to create a script but it is giving me errors on Cygwin for the following script. Could someone tell me, what am I doing wrong? choice=1000 echo "choice is $choice" while ; do echo "choice is $choice" echo 'Please select your option:' echo '1. Option 1' echo '2. Option 2'... (3 Replies)
Discussion started by: amitg1980
3 Replies

4. UNIX for Dummies Questions & Answers

what is wrong with this script?

Hi, I have this example script which gives error ": unexpected operator/operand". I need the '' brackets for operator precedence. #!/bin/ksh x="abc" y="xyz" z="123" if -a then print "yes" else print "no" fi Thanks (2 Replies)
Discussion started by: rs1969
2 Replies

5. Shell Programming and Scripting

what is wrong with this script?

Hi I've made a short script but it is not working. Can some pl. help me out in this? ./123.sh #! /usr/bin/ksh # for changing to this directory cd /layered/relational/scripts When I run the above scripts, it doesn't change to the above directory. I don't what is the problem? the... (2 Replies)
Discussion started by: Mike1234
2 Replies

6. Shell Programming and Scripting

Script Gone Wrong

Hello all, so this is a script i did for an assignement, - first option greets the user according to the time after fetching his name - second options isn't implemented - third check the performance according to how many users are using the system - creates a log of names, time and ip of the... (14 Replies)
Discussion started by: ibzee33
14 Replies

7. Shell Programming and Scripting

What's wrong with my script ....

Please see below mentioned my script ... it ran once without any issue .... then after it is not coming out .... please suggest what is wrong? #!/bin/ksh ## if (( ${num_errors} > 0 )); export ACULOG=/home/varshnes/input export num_errors=10 **** Search for 'Start correcting roll up... (7 Replies)
Discussion started by: heyitsmeok
7 Replies

8. UNIX for Dummies Questions & Answers

What's wrong on this script?

I get this error on these lines when i run this script:"for i in /home;do file2=`ls -s $i` if ;then ls - s $i fi done (7 Replies)
Discussion started by: kotsos13
7 Replies

9. Shell Programming and Scripting

Can anyone tell me what's wrong with my script

Hi... I am fed up in file handing with array for comparing.... 1st I want save first 2 columns of file 1 I tried like this,, {getline< "file1";ln=$1; lt=$2}then I read second file's 1st and 2nd column..and saved like this and small calculation and initialization var1 =$1 var2 =$2... (5 Replies)
Discussion started by: Akshay Hegde
5 Replies

10. Shell Programming and Scripting

Why result is wrong here ? whether break statement is wrong ?

Hi ! all I am just trying to check range in my datafile pls tell me why its resulting wrong admin@IEEE:~/Desktop$ cat test.txt 0 28.4 5 28.4 10 28.4 15 28.5 20 28.5 25 28.6 30 28.6 35 28.7 40 28.7 45 28.7 50 28.8 55 28.8 60 28.8 65 28.1... (2 Replies)
Discussion started by: Akshay Hegde
2 Replies
CONFSTR(3)						     Linux Programmer's Manual							CONFSTR(3)

NAME
confstr - get configuration dependent string variables SYNOPSIS
#define _POSIX_C_SOURCE 2 or #define _XOPEN_SOURCE #include <unistd.h> size_t confstr(int name, char *buf, size_t len); DESCRIPTION
confstr() gets the value of configuration - dependent string variables. The name argument is the system variable to be queried. The following variables are supported: _CS_PATH A value for the PATH variable which indicates where all the POSIX.2 standard utilities can be found. If buf is not NULL, and len is not zero, confstr() copies the value of the string to buf truncated to len - 1 characters if necessary, with a null character as termination. This can be detected by comparing the return value of confstr() against len. If len is zero and buf is NULL, confstr() just returns the value as defined below. RETURN VALUE
If name does not correspond to a valid configuration variable, confstr() returns 0. EXAMPLES
The following code fragment determines the path where to find the POSIX.2 system utilities: char *pathbuf; size_t n; n = confstr(_CS_PATH,NULL,(size_t)0); if ((pathbuf = malloc(n)) == NULL) abort(); confstr(_CS_PATH, pathbuf, n); ERRORS
If the value of name is invalid, errno is set to EINVAL. CONFORMING TO
proposed POSIX.2 BUGS
POSIX.2 is not yet an approved standard; the information in this manpage is subject to change. SEE ALSO
sh(1), exec(3), system(3) GNU
1993-04-17 CONFSTR(3)
All times are GMT -4. The time now is 01:36 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy