Combining echo and awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combining echo and awk
# 1  
Old 05-15-2013
Combining echo and awk

i have a script that has many lines similar to:
Code:
echo $var | awk -F"--" '{print $2}'

as you can see, two commands are being run here. echo and awk.

id like to combine this into one awk statement.

i tried:

Code:
awk -F"--" "BEGIN{print $var; print $2}"

but i get error messages.
# 2  
Old 05-15-2013
You can use the -v option.

Code:
$ awk -v var="$var" 'BEGIN{print var; print $2}'

This User Gave Thanks to Scott For This Post:
# 3  
Old 05-15-2013
You can assign values to AWK variables on the command line. Consult the man page for how AWK handles arguments of the form var=value.

It is not my intention to be rude, but after nearly 500 posts and nearly 7 years of membership, not having learned the basics does not reflect well. I realize that not everyone who seeks assistance is a professional administrator or programmer, but, still, this is a very fundamental question.

From nearly a year ago, one of your threads: https://www.unix.com/shell-programmin...wrong-awk.html

Regards,
Alister

Last edited by alister; 05-15-2013 at 06:17 PM..
# 4  
Old 05-15-2013
Quote:
Originally Posted by alister
You can assign values to AWK variables on the command line. Consult the man page for how AWK handles arguments of the form var=value.

It is not my intention to be rude, but after nearly 500 posts and nearly 7 years of membership, not having learned the basics does not reflect well. I realize that not everyone who seeks assistance is a professional administrator or programmer, but, still, this is a very fundamental question.

From nearly a year ago, one of your threads: https://www.unix.com/shell-programmin...wrong-awk.html

Regards,
Alister
im not sure how that post from a year ago is related to this post. in that thread, i'm looking for something different from what i asked here.

i'm well aware of that post as i have used it in many of my scripts. before creating this thread, i checked it and my question wasn't answered.

after implementing the solution that Scott as so kindly provided, im still unable to solve the situation.

Code:
var=77-99
awk -v var="$var" 'BEGIN{print var; print $2}'

unfortunately, this does not give the desired result as i'm still unable to grab the second field.

even after running this:
Code:
awk -F- -v var="$var" 'BEGIN{print var; print $2}'

all i get back is:

Code:
77-99


Last edited by SkySmart; 05-15-2013 at 06:39 PM..
# 5  
Old 05-15-2013
It's related because it mentions a mechanism with which you can pass information to AWK without piping (which for whatever reason you wish to avoid).

In the BEGIN clause, use the split function to place the different components of var into an array. You can then reference whichever member of the array you require.

Regards,
Alister
# 6  
Old 05-15-2013
for anyone reading this who might have a similar question, the answer is:

Code:
awk 'BEGIN{split(ARGV[1],var,"-");print var[2]}' $var

# 7  
Old 05-15-2013
For anyone reading this, the moral of this thread is ... if you don't spoonfeed the hungry, they won't thank you.

You're welcome,
Alister
This User Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Combining awk code into one

the following code works perfectly for me: # AWK 1 gawk -F, '/,'${ThisMonthDOW}' '${ThisMonthMON}' :: '${ThisMonthYEA}',/ { if (NF == 10) ... (6 Replies)
Discussion started by: SkySmart
6 Replies

2. Shell Programming and Scripting

awk problem - combining awk statements

i have a datafile that has several lines that look like this: 2,dataflow,Sun Mar 17 16:50:01 2013,1363539001,2990,excelsheet,660,mortar,660,4 using the following command: awk -F, '{$3=strftime("%a %b %d %T %Y,%s",$3)}1' OFS=, $DATAFILE | egrep -v "\-OLDISSUES," | ${AWK} "/${MONTH} ${DAY}... (7 Replies)
Discussion started by: SkySmart
7 Replies

3. Shell Programming and Scripting

How to combining awk commands?

I can achieve two tasks with 2 different awk commands: 1) awk -F";;WORD" '{print $2}' file | sed '/^$/d' #to find surface_word 2) awk -F"bw:|gloss:" '// {print $2}' file | sed '/\//!d; s:/*+*: + :g; s:^+::; s: *+ *$::;' #to find segmentation of surface_word Number 1) finds surface_word... (7 Replies)
Discussion started by: Viernes
7 Replies

4. Shell Programming and Scripting

Combining AWK statements

Hello UNIX Community, I have file that contains the following data: testAwk2.csv rabbit penguin goat giraffe emu ostrich hyena elephant panda dog cat pig lizard snake antelope platypus tiger cheetah lion rhino spider I then find the character length of the... (1 Reply)
Discussion started by: vnayak
1 Replies

5. Shell Programming and Scripting

combining awk and sed

Hi experts, I have a requirement, In which I need to display the first and last line of a zip file where the line starts with "L". I've writen the code like below using sed and awk. gunzip -c 20110203.1104.gz | awk '$1 ~ "^L" {print substr($0,178,15)}' | sed -n '1p;$p' Is it possible to do it... (8 Replies)
Discussion started by: senthil.ak
8 Replies

6. Shell Programming and Scripting

Combining awk statements

I have a pretty simple script below: #!/bin/sh for i in *.cfg do temp=`awk '/^InputDirectory=/' ${i}` input_dir=`echo ${temp} | awk '{ print substr( $0, 16) }'` echo ${input_dir} done As you can see its opening each cfg file and searching for the line that has "InputDirectory="... (3 Replies)
Discussion started by: ssbsts
3 Replies

7. Shell Programming and Scripting

Combining two awk scripts

I have a file like this consisting of blocks separated by > of two number X and T > 10 0 13 5.92346 16 10.3106 19 13.9672 22 16.9838 25 19.4407 28 21.4705 31 23.1547 34 24.6813 37 26.0695 40 27.3611 43 28.631 46 29.8366 49 30.9858 52 32.0934 55 33.1458 (6 Replies)
Discussion started by: kristinu
6 Replies

8. UNIX for Dummies Questions & Answers

Combining awk tests

It would be convenient to be able to combine awk tests. For example, suppose that I do this query: awk '$1 != "Bob" || $1 != "Linda" {print $2}' datafileIs there a reasonable way to combine the conditions into a single statement? For example, in egrep, I can do: egrep -v "Bob|Linda"... (4 Replies)
Discussion started by: treesloth
4 Replies

9. Shell Programming and Scripting

combining fields in awk

I am using: ps -A -o command,%cpu to get process and cpu usage figures. I want to use awk to split up the columns it returns. If I use: awk '{print "Process: "$1"\nCPU Usage: "$NF"\n"}' the $NF will get me the value in the last column, but if there is more than one word in the... (2 Replies)
Discussion started by: json4639
2 Replies

10. Shell Programming and Scripting

help combining lines in awk

I seem to have gotten myself in over my head on this one. I need help combining lines together. I have a text file containing 24,000 lines (exactly why I need awk) due to bad formatting it has separated the lines (ideally it should be 12,000 lines total). Example of file: ... (2 Replies)
Discussion started by: blueheed
2 Replies
Login or Register to Ask a Question