Sponsored Content
Top Forums Shell Programming and Scripting Questions related to if in awk context and if without awk context Post 303029710 by Don Cragun on Thursday 31st of January 2019 05:47:45 AM
Old 01-31-2019
I explained the reasons why the awk code you supplied won't work. You can choose to ignore my comments and continue to wonder why your code doesn't work. Trying to use shell variables in awk and assume that shell variable expansions will work in awk will never work the way you have used them because awk is not bash. After defining a variable in bash to be the name of a file with:
Code:
read input

and then using:
Code:
awk -v input="$input" 'BEGIN { print "input contains: \"" input "\""}'

shows you how you can turn a shell variable into an awk variable that can be used inside an awk script. Note that in shell:
Code:
echo $VAR

prints the value of the string stored in the variable named VAR (assuming that the string assigned to VAR does not contain any <backslash> characters, does not start with a <hyphen> possibly following leading <space> and/or <tab> characters). (If those constraints are broken, the output produced by echo varies from shell to shell and operating system to operating system.)

You get roughly the same output inside awk with the awk statement:
Code:
print VAR

if and only if VAR is also an awk variable containing the same string as the shell variable VAR.

Using $1 in a shell script refers to the contents of the 1st command line argument passed to your shell script. Using $1 in an awk script refers to the contents of the 1st field on the current record you are processing from the current file you are reading with awk.

If the shell variable input contains the string 5 and the awk variable input input contains the string 5 then in shell code $input expands to the string 5, but in awk code $input expands to the string that is the contents of field number 5 in the current input line in the current input file.

In a directory where I have hundreds of files and the first file in the directory (sorted alphanumerically) is named 1999_08-09.sum, the shell command
Code:
echo$(ls)

produces the output:
Code:
bash: echo1999_08-09.sum: command not found

because the shell didn't find a utility named echo1999_08-09.sum after concatenating the output produced by the command substitution $(ls) with the string echo.

The same code in awk (when there is no variable in awk named echo and no variable in awk named ls with your version of awk expands to $(0) which awk treats as the contents of the current input line from the current input file. This obviously has absolutely nothing to do with the name of a file stored in a shell variable.

You can't mix random shell statements and random awk statements and assume that that mix will magically be interpreted the way you want it to be interpreted.

The shell command language and the awk command language are not the same no matter how much you want that to be true. If you want to use shell variables in an awk script you have to create an awk variable that contains the contents of that shell variable. If you want to run shell commands inside an awk script, you have to learn the awk commands that can be used to do that (and the awk you have shown us doesn't make any attempt to do so).

BSD, Linux, and UNIX systems provide you with hundreds of tools you can use to do all sorts of wondrous things. But you have to first learn that those tools only fit together in certain ways. And, there are certain tools that are extremely good at doing one thing and extremely poor at doing other things. And, using an awk if statement to determine if a string assigned to a shell variable names an existing file is an extreme case of using the wrong tool to try to do the job.

I wish you luck in your adventures, but I sincerely hope you'll take a closer look at the (entirely) bash script I gave you that seems to do what I think you were trying to do. Trying to use awk to determine whether or not there is a file of a certain name in the current working directory is enormously more difficult than learning to use the test utility that is available in all shells based on Bourne shell syntax (e.g., bash, dash, ksh, sh, and zsh).

You could also use [[ expression ]] instead of test expression or [ expression ], in some versions of bash and some versions of ksh, but I strongly suggest that you learn the basics before trying to use shell specific features that work well in some cases but not in others.

Last edited by Don Cragun; 01-31-2019 at 06:52 AM..
This User Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Context Switching

I know that this is a relative question but can someone give me an idea of what would be considered a high number of context switches? I am running vmstat and show a cs value of between 5000 and 6000 on a 4 processor system. How can I deduce if this number is high or not? Also, the timeslice... (2 Replies)
Discussion started by: keelba
2 Replies

2. Shell Programming and Scripting

keep context in awk

here is a data file. ------------------------------------- KSH, CSH, BASH, PERL, PHP, SED, AWK KSH, CSH, BASH, PERL, PHP, BASH, PERL, PHP, SED, AWK CSH, BASH, PERL, PHP, SED, KSH, CSH, BASH, PERL, PHP, SED, AWK ------------------------------------- My desired output is... (2 Replies)
Discussion started by: VTAWKVT
2 Replies

3. UNIX for Advanced & Expert Users

context lost problem

there are several same servers(process) on more than one server(machine) providing the same service. we store session/context within the server(process), if the same client login, he will be directed to the very server service for him last time. But, if a server(machine or process) down, the... (1 Reply)
Discussion started by: zhongyj
1 Replies

4. Shell Programming and Scripting

grep help after context

Hi, There's a file with below contents which I have to read based on the input parameter provided by the user. FILE_ID=1 FILE_FTP_ID=ftp.server1.com FILE_FTP_USER=user1 FILE_FTP_PASS=pass1 FILE_ID=2 FILE_FTP_ID=ftp.server2.com FILE_FTP_USER=user2 FILE_FTP_PASS=pass2 FILE_ID=3... (6 Replies)
Discussion started by: dips_ag
6 Replies

5. UNIX for Dummies Questions & Answers

Split based on Context

Hello, I have a file that I want to be able to split at specific positions. For example in the file below I want to be able to split at every occurence of 2 at the start of a line, into multiple files. 2 abc PQRST abcRSTG 2 cde FGKL abcLKGRG ABCLKgrg 2 lmn OPT lmopqrst uvwxyz ... (1 Reply)
Discussion started by: Gussifinknottle
1 Replies

6. UNIX for Dummies Questions & Answers

Context-switching question

Hi all, I've got this question that i need to solve: "Type `vmstat -s; vmstat -n 1 5; vmstat -n 1 5; vmstat -s` to your Ruby interpreter. Then terminate your Ruby session. Run the Unix com- mand vmstat -s; vmstat -n 1 5; vmstat -s in the same terminal window you had been using for Ruby. Did... (1 Reply)
Discussion started by: snowboarder
1 Replies

7. Slackware

Context dependent symlinks

Ive got multiple PCs, sharing an NFS mounted home dir. For certain apps I would like to keep the config files host specific. Easy solution is to create symlinks to local folders for configs. Ideally I would still want the .config files to reside in the user home folder. Is it possible to... (2 Replies)
Discussion started by: agentrnge
2 Replies

8. Linux

involuntary context switching

In a kernel based on 2.6.27: In the schedule() routine they have a local variable switch_count: /* * schedule() is the main scheduler function. */ asmlinkage void __sched schedule(void) { struct task_struct *prev, *next; unsigned long *switch_count; struct rq... (2 Replies)
Discussion started by: chriskot
2 Replies

9. UNIX for Advanced & Expert Users

Interupt Context Switching

If suppose a middle level interrupt is being serviced and a high priority interrupts comes in then in that case what all process will take place. The interrupt context switch will happen. But where will the interrupt context be saved? Is there something called as part process data area? (4 Replies)
Discussion started by: rupeshkp728
4 Replies

10. Shell Programming and Scripting

Context for use of [.symbol.] awk notation

Hi Just wondering ... do you have an example of context that would demonstrates how usefull the awk notation can efficiently be used ? Thx :rolleyes: (6 Replies)
Discussion started by: ctsgnb
6 Replies
All times are GMT -4. The time now is 11:26 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy