f



awk

Why in the following code?

varA=2;

strA="`cat /root/mytempdir/wordlist.txt | awk '{print $ 2}'`"
strB="`cat /root/mytempdir/wordlist.txt | awk '{print $ $varA}'`"

strA contains the second word of line 1
and strB contains all  line 1
???

Thanks



0
AL
8/22/2006 6:27:34 PM
comp.unix.shell 15484 articles. 2 followers. Post Follow

3 Replies
718 Views

Similar Articles

[PageSpeed] 16

AL wrote:
> Why in the following code?
>
> varA=2;
>
> strA="`cat /root/mytempdir/wordlist.txt | awk '{print $ 2}'`"
> strB="`cat /root/mytempdir/wordlist.txt | awk '{print $ $varA}'`"
>
> strA contains the second word of line 1
> and strB contains all  line 1
> ???

shell variables within single-quotes are not interpolated. try
enclosing the awk statements with double quotes.

  strB=`cat /root/mytempdir/wordlist.txt | awk "{print $ $varA}"`
 
--
XC

0
Xicheng
8/22/2006 6:53:12 PM
Thank you !





0
AL
8/22/2006 7:11:30 PM
Xicheng Jia wrote:

> AL wrote:
> 
>>Why in the following code?
>>
>>varA=2;
>>
>>strA="`cat /root/mytempdir/wordlist.txt | awk '{print $ 2}'`"
>>strB="`cat /root/mytempdir/wordlist.txt | awk '{print $ $varA}'`"
>>
>>strA contains the second word of line 1
>>and strB contains all  line 1
>>???
> 
> 
> shell variables within single-quotes are not interpolated.

Right, so to awk "$var" is trying to use an awk (NOT shell) variable 
varA to access a positional parameter. Since varA is an unitialised awk 
variable, it gets initialised on first use to the value zero, to $varA 
is the same as $0 which is the whole input record.

  try
> enclosing the awk statements with double quotes.
> 
>   strB=`cat /root/mytempdir/wordlist.txt | awk "{print $ $varA}"`

No, don't do that as you'd leave yourself open to obscure failures if 
your variable isn't set as expected.

Instead set an awk variable based on the value of the shell variable, e.g.:

awk -v varA="$varA" '{print $varA}'

See question 24 in the FAQ, 
http://home.comcast.net/~j.p.h/cus-faq-2.html#24, for more details.

	Ed
0
Ed
8/22/2006 7:31:11 PM
Reply:

Similar Artilces:

web hosting, shell provider, unix shells, bnc shell, ircd provider, shell hosting, bnc shells, irc shells, ircd shells, unix shell account, irc shell, irc, eggdrop shell, irc vps, irc shell, linux vps
http://www.hostrazor.net/ web hosting, shell provider, unix shells, bnc shell, ircd provider, shell hosting, bnc shells, irc shells, ircd shells, unix shell account, irc shell, irc, eggdrop shell, irc vps, irc shell, linux vps -- questo articolo e` stato inviato via web dal servizio gratuito http://www.newsland.it/news segnala gli abusi ad abuse@newsland.it ...

can I run unix shell command in the ModelSim shell?
Hi, Just wonder if I can run unix shell command in the Modelsim shell, expecially those time/date command? Thanks. clinton__bill@hotmail.com wrote: > Hi, > Just wonder if I can run unix shell command in the Modelsim shell, > expecially those time/date command? ModelSim uses Tcl as its scripting language so (almost) anything you can do from Tcl can be done in ModelSim. Tcl has a variety of shell and file interaction commands available. Of course, if you want to execute Unix commands, you need to be running on a Unix (or Cygwin) system. Note that Tcl commands are NOT direct...

Unix Shell
I'm new to RUBY, but am familar with other scripting languages, PHP, Perl, Wscript, etc. I'd like to be able to run items from the shell such as ls -l > dirlisting.txt. Just an example. Can I do this? I would have just used the search on these forums, but unfortuantely when I click Search I get "The page cannot be found". Hope they can get this fixed so I won't have to ask every little question. Thanks in advance. -Greg -- Posted via http://www.ruby-forum.com/. Greg Johnson wrote: > I'd like to be able to run items from the shell > such as l...

Unix shell
Hi all, We offer free shells with web hosting and a spam-filtered email account. - 50MB storage space (more available upon request) - Domain hosting available upon request - Access via SSH, SFTP, and POP3-ssl - Instant messaging clients for IRC, AIM, MSN, ICQ, Yahoo!, Gadu-Gadu, Jabber, and Lily - FastTrack (KaZaA), SoulSeek, and BitTorrent clients - Optional trainable server-side spam detection is also included. We also have a few vhosts for fans of IRC. Instant accounts available at: http://silenceisdefeat.org/ Anyway, I hope this is an acceptable place to let you guys know. If i...

unix shells
I have compiled a program called mk, and it's internal routines work with unixlib in a taskwindow happily enough. However the wheels fall off when a recipe wants to 'fire up' a child process for calling the gcc commands. It's a little confusing, because it echos the command perfectly to it's running Taskwindow, but errors with mk fork Invalid argument Looking inside a bit further, I think the error comes from a program unix.c where there also is reference to /bin/sh Should I be trying to map this to a new Taskwindow with Set UnixFS$/bin/sh Taskwindow or some redirection? Ron M In message <534c5a1151.beeb@ron1954.woosh.co.nz> Ron <beeb@woosh.co.nz> wrote: > I have compiled a program called mk, and it's internal routines > work with unixlib in a taskwindow happily enough. > However the wheels fall off when a recipe wants to 'fire up' a > child process for calling the gcc commands. > It's a little confusing, because it echos the command perfectly > to it's running Taskwindow, but errors with > mk fork Invalid argument > > Looking inside a bit further, I think the error comes from a > program unix.c where there also is reference to /bin/sh > > Should I be trying to map this to a new Taskwindow with > Set UnixFS$/bin/sh Taskwindow or some redirection? > > Ron M I have tried changing the command gcc to the coreutils cp, in case it is a heap/wimpslot issue but look...

awk Unix
Hi! I have a UNIX (awk) query. I have pulled out of MYSQL field into a tab-delimited file (array). I want to create directories so I have created shell script: for(i=2;i<NF;i++) mkdir $i } I awk the tab delimited file: awk -f (shell script) (tab_delimited file) but nothing happens If 'mkdir' was 'print' it would print the required fields, but if it were 'echo' it would not. Does anyone there know why this should be? Thanks! Paolo (new to all this!) Paolo wrote: > Hi! > > I have a UNIX (awk) query. I have pulled out of MYSQL field into a...

US-NJ-Jersey City: UNIX Developer, UNIX, Shell scripting, SwiftAlliance Access; (45335553540)
US-NJ-Jersey City: UNIX Developer, UNIX, Shell scripting, SwiftAlliance Access; (45335553540) ============================================================================================= Position: UNIX Developer Reference: SMC01885 Location: Jersey City NJ Duration: 7M Skills: Knowledge of SwiftAlliance Access. Exp in upgrading/migrating Swift Alliance Access, Swift Alliance Gateway, SwiftNet Link. Knowledge of Compliance and Watchlist filtering applications like Mircosoft. Knowledge of B...

US-NJ-Jersey City: UNIX Developer, UNIX, Shell scripting, SwiftAlliance Access; (45335557609)
US-NJ-Jersey City: UNIX Developer, UNIX, Shell scripting, SwiftAlliance Access; (45335557609) ============================================================================================= Position: UNIX Developer Reference: SMC01885 Location: Jersey City NJ Duration: 7M Skills: Knowledge of SwiftAlliance Access. Exp in upgrading/migrating Swift Alliance Access, Swift Alliance Gateway, SwiftNet Link. Knowledge of Compliance and Watchlist filtering applications like Mircosoft. Knowledge of B...

US-NJ-Jersey City: UNIX Developer, UNIX, Shell scripting, SwiftAlliance Access; (45335857612)
US-NJ-Jersey City: UNIX Developer, UNIX, Shell scripting, SwiftAlliance Access; (45335857612) ============================================================================================= Position: UNIX Developer Reference: SMC01885 Location: Jersey City NJ Duration: 7M Skills: Knowledge of SwiftAlliance Access. Exp in upgrading/migrating Swift Alliance Access, Swift Alliance Gateway, SwiftNet Link. Knowledge of Compliance and Watchlist filtering applications like Mircosoft. Knowledge of Banking, payment and messaging systems. Exp with Unix, shell Scripting, MQ Series, Oracle, Crystal Reports and WebSphere. Strong analytical skills. Ability to analyze a problem. own the problem and follow thru to resolution. Production support experience with critical systems. Good written and oral communication skills. Scope: Knowledge of Banking, payment and messaging systems. Experience with Unix, shell Scripting, MQ Series, Oracle, Crystal Reports and WebSphere. Strong analytical skills. Ability to analyze a problem. own the problem and follow thru to resolution. Production support experience with critical systems. Good written and oral communication skills. Please send your...

US-NJ-Jersey City: UNIX Developer, UNIX, Shell scripting, SwiftAlliance Access; (45335832409)
US-NJ-Jersey City: UNIX Developer, UNIX, Shell scripting, SwiftAlliance Access; (45335832409) ============================================================================================= Position: UNIX Developer Reference: SMC01885 Location: Jersey City NJ Duration: 7M Skills: Knowledge of SwiftAlliance Access. Exp in upgrading/migrating Swift Alliance Access, Swift Alliance Gateway, SwiftNet Link. Knowledge of Compliance and Watchlist filtering applications like Mircosoft. Knowledge of B...

US-NJ-Jersey City: UNIX Developer, UNIX, Shell scripting, SwiftAlliance Access; (45335814411)
US-NJ-Jersey City: UNIX Developer, UNIX, Shell scripting, SwiftAlliance Access; (45335814411) ============================================================================================= Position: UNIX Developer Reference: SMC01885 Location: Jersey City NJ Duration: 7M Skills: Knowledge of SwiftAlliance Access. Exp in upgrading/migrating Swift Alliance Access, Swift Alliance Gateway, SwiftNet Link. Knowledge of Compliance and Watchlist filtering applications like Mircosoft. Knowledge of B...

US-NJ-Jersey City: UNIX Developer, UNIX, Shell scripting, SwiftAlliance Access; (45335557609)
US-NJ-Jersey City: UNIX Developer, UNIX, Shell scripting, SwiftAlliance Access; (45335557609) ============================================================================================= Position: UNIX Developer Reference: SMC01885 Location: Jersey City NJ Duration: 7M Skills: Knowledge of SwiftAlliance Access. Exp in upgrading/migrating Swift Alliance Access, Swift Alliance Gateway, SwiftNet Link. Knowledge of Compliance and Watchlist filtering applications like Mircosoft. Knowledge of Banking, payment and messaging systems. Exp with Unix, shell Scripting, MQ Series, Oracle, Crystal Reports and WebSphere. Strong analytical skills. Ability to analyze a problem. own the problem and follow thru to resolution. Production support experience with critical systems. Good written and oral communication skills. Scope: Knowledge of Banking, payment and messaging systems. Experience with Unix, shell Scripting, MQ Series, Oracle, Crystal Reports and WebSphere. Strong analytical skills. Ability to analyze a problem. own the problem and follow thru to resolution. Production support experience with critical systems. Good written and oral communication skills. Please send your...

shell variable in awk
Hi, I am writing a bash script #! /bin/bash pattern=$1 cat file.c | awk '{ if($0 ~ $pattern) print $0; }' I want to know how could i use the variable pattern in the awk script, I tried to use '$pattern' "$pattern" $pattern pattern $'$pattern' $(pattern) in the awk script, but awk is not understanding it, I have checked in our group for similar problem, but i didn't get any satisfactory answer Rakesh UV Rakesh UV wrote: > Hi, > I am writing a bash script > ...

interactive awk shell
Hi, This is a need I perceived recently but I can't find mention of anything similar despite lots of Googling. I was working over and over with the same tab-separated data file and was tired of typing awk -F"\t" and the filename every time. I thought it would be handy to be able to use awk in a more interactive way. After several feature iterations over the last couple days, I think I have a stable version. The only requirement mktemp; specifically, I use a version that supports the -t flag. If you know of similar scripts or have ever written a wrapper like this yourself, I'd be interested to know more about it. --- #!/bin/sh # $Id: iash,v 1.19 2013/11/22 03:50:14 mike Exp $ # # Interactive AWK Shell BASENAME="${0##*/}" CMDPROMPT="$BASENAME> " if [ -n "$2" ] then FIELDSEP="$1" DATAFILE="$2" elif [ -n "$1" ] then FIELDSEP="\t" DATAFILE="$1" else echo >&2 "Usage: $BASENAME [fs] <file>" exit 1 fi if [ ! -f "$DATAFILE" ] then echo >&2 "File not found: $DATAFILE" exit 1 fi printhelp () { echo "Enter 'bye', 'exit', 'q', 'quit', or 'x' to leave." echo "Enter 'clear' or 'cls' to clear the screen." echo "Enter 'hist' or 'history' to view ses...

awk from shell script
Hello, I'm writting a shell script that takes one commandline parameter, I want to use that parameter within an awk action statement. awk '{print $1}' file That won't work , so I thoughed about doing this, test=$1 awk '{print $test}' file This is just a stupid example to illustrate what I'm trying to do ... Jonas Geiregat wrote: > I'm writting a shell script that takes one commandline parameter, > I want to use that parameter within an awk action statement. > awk '{print $1}' file Have you tried awk "{print $1}" file Jonas Geiregat <jonas.geiregat@gmail.com> wrote: > Hello, > > I'm writting a shell script that takes one commandline parameter, > I want to use that parameter within an awk action statement. > awk '{print $1}' file > That won't work , so I thoughed about doing this, > test=$1 > awk '{print $test}' file > > This is just a stupid example to illustrate what I'm trying to do ... awk -v test=$1 '{print $test}' awk '{print $test}' test=$1 -- William Park <opengeometry@yahoo.ca>, Toronto, Canada Slackware Linux -- because I can type. Jonas Geiregat wrote: > Hello, > > I'm writting a shell script that takes one commandline parameter, > I want to use that parameter within an awk action statement. > awk '{print $1}' file > That won't work , so I thoughed about doing this, >...

Shell variables in awk
I need help with an Awk statement using variables. I've used a similar format in another script and it works fine. file "/tmp/temp1 (1) Configuration 1 - SYM001,SYM005,SYM010,SYM014,SYM017,SYM019 (2) Configuration 2 - SYM007,SYM505,SYM502,SYM004,SYM400 (3) Configuration 3 - SYM102,SYM104,SYM106,SYM108,SYM463,SYM473 .. .. .. =================================================== awk ' $5 ~ 102 {print substr($1, 2, length($1)-2)}' /tmp/temp1 This returns "3" which is what I want =================================================== SYMNUM="102" awk -v sym=&q...

Awk in shell scripts
I have created a shell script that uses functions ie name() { what to do } Main program Name What I want to do is add in an awk script in the same way but have run in to the problem that awk scripts are already in brackets { Awk stuff } so my method above will not work. Any ideas? On Sat, 12 Feb 2005 at 15:27 GMT, Derek Roberts wrote: > I have created a shell script that uses functions ie > > name() > { > what to do > } > > > Main program > > Name > > What I want to do is add in an awk script in the same way but have run > in to the pro...

Shell(AWK) Sheet
(If you want in pdf file than pl get it from the : http://groups.google.co.in/group/tech_group?hl=en ) . Print column1, column5 and column7 of a data file or output of any columns list $awk '{print $1, $5, $7}' data_file $cat file_name |awk '{print $1 $5 $7}' $ls -al |awk '{print $1, $5, $7}' -- Prints file_permissions,size and date Syntax of running an awk program Awk 'program' input file(s) List all files names whose file size greater than zero. $ls -al |awk '$5 > 0 {print $9}' List all files whose file size equal to 512bytes. $ls -al |awk '$...

unix shell script
Hi all, I am in need of a shell script which will cat all the audit files into one log and then email. Thanks. Muddu "muddu" <mudassar.dba@gmail.com> wrote in message news:1156371761.252866.267670@b28g2000cwb.googlegroups.com... > Hi all, > > I am in need of a shell script which will cat all the audit files into > one log and then email. > "cat *.aud" will merge all files together. You can redirect it to a file and/or email. Thank you very much for your hint. Regards. Muddu Bob Jones wrote: > "muddu" <mudassar.dba@gmail.com> wrote in message > news:1156371761.252866.267670@b28g2000cwb.googlegroups.com... > > Hi all, > > > > I am in need of a shell script which will cat all the audit files into > > one log and then email. > > > > "cat *.aud" will merge all files together. You can redirect it to a file > and/or email. ...

Make, shell and awk
I have a Makefile containing the following (among others): --- SHELL=/bin/tcsh spec/veccmdfiles.tex: $(wildcard veccmd/*.veccmd) ls -1 veccmd/*.veccmd | awk '{caption=$1;gsub("_", "\\_", caption);;print caption " blabla " $1}' --- The problem is, that when making the target above, I get an error indicating, that the shell (tcsh) and NOT AWK has processed the $1 variable. I.e. $1 is an empty string. E.g. -- $ make spec/veccmdfiles.tex ls -1 veccmd/*.veccmd | awk '{caption=;gsub("_", "\\_", caption);... awk: cmd. line:1: {caption=;g...

Awk and shell variables
Hi everybody, i'm making a shell script to extract fields from a file. The fact is that i'm using the awk command to do that (i think its the one for) but i need to use awk variables in my shell script. My shell script call awk at the beginning. After the end i must use results from my awk script but i don't know how. I know how to use shell variables for awk but not the contrary. Here is my script : !/bin/sh JOURMAX=0 MOISMAX=0 ls | awk 'BEGIN { FS="\n" } { for (i=0;i<NF;i++) { printf "chaine $0 : %s\n",$0 ...

awk instead of shell?
Recently, I started programming in awk, and I like it. Many of my programs are written in shell so far, some of them fairly complex. I like shell for its ability to directly interact with the filesystem via the tools cp, cd, rm, mv, etc. However, shell can be very slow (and awk, on the contrary, seems very fast to me) and also, shell misses some idioms, like associative arrays. My idea is to try to use awk wherever I used to use shell in the past. Maybe it is a bad idea, but I'd like to give it a try. Are there any resources on this topic? Most importantly, I need a clean way to ite...

in awk or shell we trust
Hi all i made an image of an audio cd dd if=-/dev/disk1 of=/home/ Afterhours Then i tried to mount it and view the inside files but no way. So is there a awk command or shell to view what's inside an image? Thanks in advance On 13 Nov., 08:19, franzi <hazz...@gmail.com> wrote: > Hi all i made an image of an audio cd dd if=-/dev/disk1 of=/home/ Is that '-' as part of the input file path intentional? > Afterhours > Then i tried to mount it and view the inside files but no way. If you create an output file you need not mount the output file, it should be readable. ...

Unix without shell
Another thread (on find ... -exec ...) got me thinking about "pure Unix", i.e. Unix without a shell. I realize that, with the exception of the occasional find ... -exec ..., and maybe at login time, I don't know of any other instance in which I interact with Unix without some shell's intervention. In fact I don't have a very clear idea of where the shell ends and Unix begins. I want to remedy this situation. I'm sure it's a royal pain to deal with Unix without the services of a shell, but I would like to experience it first hand. Is it possible? I suppose...

Linux/unix Shell
Hi, Is there any linux/unix shell that also works in windows..actually i used telnet or Putty to login to my unix development machine and get the console which is single is there a similiar utility like putty/telent but give me multiple shell/consoles. if anybody knows please let me know about that. rgrds, Munish emanshu wrote: > Hi, > > Is there any linux/unix shell that also works in windows..actually i > used telnet or Putty to login to my unix development machine and get > the console which is single is there a similiar utility like > putty/telent but give me multiple shell/consoles. > if anybody knows please let me know about that. > rgrds, > Munish > have you tried opening multiple instances of either of these or hyperterminal? -- JosephKK Gegen dummheit kampfen Die Gotter Selbst, vergebens. --Shiller On 19 Jan 2006 04:35:26 -0800, emanshu <munish.nr@gmail.com> wrote: > Hi, > > Is there any linux/unix shell that also works in windows..actually i > used telnet or Putty to login to my unix development machine and get > the console which is single is there a similiar utility like > putty/telent but give me multiple shell/consoles. > if anybody knows please let me know about that. > rgrds, > Munish > Try screen. -- <apt> it has been said that redhat is the thing Marc Ewing wears on his head. -----BEGIN PGP SIGNED MESSAGE----- Hash:...

Web resources about - awk - comp.unix.shell

AWK - Wikipedia, the free encyclopedia
... Brian Kernighan . The acronym is pronounced the same as the name of the bird, auk (which acts as an emblem of the language such as on The AWK ...

The A-Z of Programming Languages: AWK
Computer scientist and compiler expert Alfred V. Aho is a man at the forefront of computer science research. He has been involved in the development ...

Ten Companies To Own For The Next Decade (AEP, AWK, CSCO, DG, XOM, KMB, GE, RSG, TEVA, DIS)
Picking stocks for a decade is no easy task. It might as well be for a lifetime because there probably will be two more bear markets and two ...

Awk Ward
Even the liberal Joe Scarborough cringes ( via ) at Romney’s ineptitude with crowds:

Bonanza Creek Energy, Inc. (NYSE:BCEI), American Water Works (NYSE:AWK) - Benzinga's Top Downgrades ...
Analysts at Raymond James downgraded Dollar Tree, Inc. (NASDAQ: DLTR) from Strong Buy to Market Perform. Dollar Tree shares closed at $82.03 ...

AWK:New York Stock Quote - American Water Works Co Inc - Bloomberg
Stock analysis for American Water Works Co Inc (AWK:New York) including stock price, stock chart, company news, key statistics, fundamentals ...

GNU Awk: This is Not Your Father's Awk
Awk's features have advanced considerably in the last decade such as the addition of a debugger and a profiler all without removing any of ...

What Is Awk? What Makes It A More Useful Utility Than Other Shell Utilities?
awk is a programming language optimized for text processing. It takes less code than sed or perl, where the input is field-based. bash and other ...

New York Fashion Weavz: Fashion's Night Awk
It is two nights ago—Wednesday, September 4, 2013—around 8:15 p.m. At this precise moment, you are enjoying yourself, conservatively, one thousand ...

利用 AWK 与 SORT 抓取字段最大值
档案中 第一栏为 Key, 第二栏为 Value, 要透过 Bash shell script 来计算列出 Key 与 最大值.

Resources last updated: 3/25/2016 7:00:52 PM