awk line variable help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk line variable help
# 1  
Old 07-23-2012
awk line variable help

I am using Solaris 10 and have no GNU products installed. I have the following awk code and would like to add a variable. I believe awk doesn't accept variables, but I think nawk can. I would like to replace the red higlighted part of my code with a variable. If nawk is the answer how do I change my awk code to nawk code?

Code:
awk '$2 ~/^0:/ && $6 ~/^04/ && $7 ~/^2[48]/ && $8 ~/^00|^0F|^1F/ {print x;print;getline;print}; {x=$0}

# 2  
Old 07-23-2012
I don't have access to a Sun box tonight to try this, but I believe this will work in awk, and should work in nawk for sure.

Code:
awk '$2 ~/^0:/ && match( $6, "^" v ) && $7 ~/^2[48]/ && $8 ~/^00|^0F|^1F/ {print x;print;getline;print}; {x=$0}' v="04" input-file

If you are piping data in, obviously omit the input-file name. I just wanted to show that it goes after the assignment of v.
# 3  
Old 07-23-2012
Thanks for the help. Let's say I have set my variable before I ran this line and its stored in a variable called name. Would this code work?

Code:
awk '$2 ~/^0:/ && match( $6, "^" v ) && $7 ~/^2[48]/ && $8 ~/^00|^0F|^1F/ {print x;print;getline;print}; {x=$0}' v=$name input-file

or would I need something like this:
Code:
awk '$2 ~/^0:/ && match( $6, "^" v ) && $7 ~/^2[48]/ && $8 ~/^00|^0F|^1F/ {print x;print;getline;print}; {x=$0}' v=`echo $name` input-file

Or maybe neither one would work....what's your thought?
# 4  
Old 07-23-2012
Your first one; the echo is not necessary. Best to quote it though:

Code:
awk '$2 ~/^0:/ && match( $6, "^" v ) && $7 ~/^2[48]/ && $8 ~/^00|^0F|^1F/ {print x;print;getline;print}; {x=$0}' v="$name" input-file

This User Gave Thanks to agama 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

awk variable search and line count between variable-search pattern

Input: |Running the Rsync|Sun Oct 16 22:48:01 BST 2016 |End of the Rsync|Sun Oct 16 22:49:54 BST 2016 |Running the Rsync|Sun Oct 16 22:54:01 BST 2016 |End of the Rsync|Sun Oct 16 22:55:45 BST 2016 |Running the Rsync|Sun Oct 16 23:00:02 BST 2016 |End of the Rsync|Sun Oct 16 23:01:44 BST 2016... (4 Replies)
Discussion started by: busyboy
4 Replies

2. Shell Programming and Scripting

awk - Multi-line data to be stored in variable

Greetings Experts, As part of automating the sql generation, I have the source table name, target table name, join condition stored in a file join_conditions.txt which is a delimited file (I can edit the file if for any reason). The reason I needed to store is I have built SELECT list without... (5 Replies)
Discussion started by: chill3chee
5 Replies

3. Shell Programming and Scripting

Awk: Assigning a variable to be the value of FNR at a certain line

Sorry for the probably strangely worded title but I don't really know how else to put it. Background context: Post processing LAMMPS simulation data. tl;dr: I'm making two spheres collide, every defined timestep the simulation outputs a bunch of data including total energy of the particles,... (10 Replies)
Discussion started by: ThomasP
10 Replies

4. Shell Programming and Scripting

How to read a two files, line by line in UNIX script and how to assign shell variable to awk ..?

Input are file and file1 file contains store.bal product.bal category.bal admin.bal file1 contains flip.store.bal ::FFFF:BADC:CD28,::FFFF:558E:11C5,6,8,2,1,::FFFF:81C8:CA8B,::FFFF:BADC:CD28,1,0,0,0,::FFFF:81C8:11C5,2,1,0,0,::FFFF:81DC:3111,1,0,1,0 store.bal.... (2 Replies)
Discussion started by: veeruasu
2 Replies

5. UNIX for Dummies Questions & Answers

Help with awk, where line length and field position are variable

I have several questions about using awk. I'm hoping someone could lend me a hand. (I'm also hoping that my questions make sense.) I have a file that contains pipe separated data. Each line has similar data but the number of fields and the field position on each line is variable. ... (3 Replies)
Discussion started by: Cheese64
3 Replies

6. Shell Programming and Scripting

Using AWK to get a specific line using a variable

Hi I have a text like this example.input 1 red 2 blue 3 green If I set this c=2 Then try awk 'NR==$c { print $2 }' example.input I do get nothing If I try awk 'NR==2 { print $2 }' example.input i get blue (8 Replies)
Discussion started by: Jotne
8 Replies

7. Shell Programming and Scripting

Modify line with dynamic variable in awk

Hi, I'm guessing this is probably relatively straight forward to do in awk, but I just can't get my head round it! I have a log file of the following format: 3:03:35 (lmgrd) TIMESTAMP 10/14/2011 3:20:41 (MLM) IN: "MATLAB" user1@host1.private.dns.zone 3:21:05 (MLM) IN: "MATLAB"... (2 Replies)
Discussion started by: chrissycc
2 Replies

8. Shell Programming and Scripting

AWK Script to Capture Each Line of File As Variable

Hi All, I'm working on creating a parts database. I currently have access to a manufacturer database in HTML and am working on moving all of the data into a MySQL db. I have created a sed script that strips out the HTML and unnecessary info and separates the script into one line for each field.... (3 Replies)
Discussion started by: dkr
3 Replies

9. Shell Programming and Scripting

Line/Variable Editing for Awk sed Cut

Hello, i have a file, i open the file and read the line, i want to get the first item in the csv file and also teh third+6 item and wirte it to a new csv file. only problem is that using echo it takes TOO LONG: please help a newbie. below is my code: WorkingDir=$1 FileName=`cut -d ',' -f... (2 Replies)
Discussion started by: limamichelle
2 Replies

10. Shell Programming and Scripting

print any required line by its line no using awk and its NR variable

how to print any required line by its line no using awk and its NR variable for eg: ------------ 121343 adfdafd 21213sds dafadfe432 adf.adf%adf --------------- requied o/p if give num=3 it print: 21213sds -------------------------------------- (2 Replies)
Discussion started by: RahulJoshi
2 Replies
Login or Register to Ask a Question