Using current line in a command in AWK


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using current line in a command in AWK
# 1  
Old 01-16-2011
Data Using current line in a command in AWK

Hi, Im trying to get current line in the AGREP command I use in AWK. My script looks like this:

Code:
list.txt
car
bus

checklist.txt
cer
buss

cat list.txt | awk -v mycmd="$(agrep -2 -i $0 checklist.txt)" '{print $mycmd}'

It doesnt work. How can I get the current line in the $0 variable? Or is there any different solution? I need to go through each line in list.txt, search & compare it in checklist.txt and get a result.

Thank u for ur responses.
# 2  
Old 01-16-2011
Not just any awk uses variables. Use gawk or nawk if available. Otherwise use your inline function embedded as you have above; you'll just need to escape it, or wrap it within an eval statement.
# 3  
Old 01-16-2011
Pls could you give me an example how to get the var in some unix command in nawk or gawk?
# 4  
Old 01-16-2011
As per the awk manpage:
OPTIONS
Gawk accepts the following options:
-v var=val
--assign var=val
Assign the value val to the variable var, before execution of the program begins. Such variable values are available to the BEGIN block of an AWK program.
For example:
Code:
trogdor $ awk -vVAR=uservalue 'BEGIN {print VAR;}' /dev/null

will print to stdout:
Code:
uservalue

Note that:
Code:
trogdor $ awk 'BEGIN {print VAR;}' VAR=anothervalue /dev/null

will print to stdout:
Code:
(an empty line)

(I could not get an empty line to show up in a code block), as VAR has not yet been assigned.
However:
Code:
trogdor $ echo | awk '{print VAR;}' VAR=anotheruservalue

will print to stdout:
Code:
anotheruservalue

# 5  
Old 01-16-2011
Hi, thank you all for ur responses. Im not sure if i get.

I need to get a line (from list.txt) in the variable var, that AWK processes and use agrap on it. The first line is represented as $0.
# 6  
Old 01-16-2011
Can you provide a somewhat longer example of your input files, and what you expect the output to be.
# 7  
Old 01-16-2011
Input:

Code:
cat list.txt

output:
computer
mobile
users

cat list2.txt 

output:
computers
mobil
dogs

What want is to take words one by one from the list.txt, let search each word in the list2.txt and find similar word.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

awk command in hp UNIX subtract 30 days automatically from current date without date illegal option

current date command runs well awk -v t="$(date +%Y-%m-%d)" -F "'" '$1 < t' myname.dat subtract 30 days fails awk -v t="$(date --date="-30days" +%Y-%m-%d)" -F "'" '$1 < t' myname.dat awk command in hp unix subtract 30 days automatically from current date without date illegal option error... (20 Replies)
Discussion started by: kmarcus
20 Replies

2. Shell Programming and Scripting

Copy Column Value Of Next Line Into Current Line

Hello All, I am looking for help to achieve the following: Here is the data set 1757890237|42|55570025|1468796400|0 1757890237|32|55570025|1471474800|0 1757890237|54|55570025|1474153200|1476745200 1757890237|34|55570026|1468796400|0 1757890237|44|55570026|1471474800|0... (7 Replies)
Discussion started by: angshuman
7 Replies

3. Shell Programming and Scripting

Append Next line with current Line bassed on condition

Hi, I have an XML file and I am tring to extract some data form it, after lot of data cleaning process, I ended up with an issue, and need your urgent support. my current input data in below format: <Node>xxxxxx <Node>yyyyy</Node> <Node>zzzzzz <Node>12345</node> I need... (9 Replies)
Discussion started by: rramkrishnas
9 Replies

4. Shell Programming and Scripting

Pass awk field to a command line executed within awk

Hi, I am trying to pass awk field to a command line executed within awk (need to convert a timestamp into formatted date). All my attempts failed this far. Here's an example. It works fine with timestamp hard-codded into the command echo "1381653229 something" |awk 'BEGIN{cmd="date -d... (4 Replies)
Discussion started by: tuxer
4 Replies

5. Shell Programming and Scripting

Sed Comparing Parenthesized Values In Previous Line To Current Line

I am trying to delete lines in archived Apache httpd logs Each line has the pattern: <ip-address> - - <date-time> <document-request-URL> <http-response> <size-of-req'd-doc> <referring-document-URL> This pattern is shown in the example of 6 lines from the log in the code box below. These 6... (1 Reply)
Discussion started by: Proteomist
1 Replies

6. UNIX for Dummies Questions & Answers

Awk to print data from current and previous line

Hi guys, I have found your forum super useful. However, right now I am stuck on a seemingly "simple" thing in AWK. I have two columns of data, the first column in Age (in million years) and the second column is Convergence Rate (in mm/yr). I am trying to process my data so I can use it to... (2 Replies)
Discussion started by: awk_noob_456
2 Replies

7. Shell Programming and Scripting

Vi Editor - How to paste the line concatenated with current line

I generally use yy to copy a line and then p to paste the line at end of current line. But is there a way to paste the copied line in concatenation with the current line with out going to next line. (3 Replies)
Discussion started by: paragkalra
3 Replies

8. Shell Programming and Scripting

awk print the next line on the current line

Ok I have a file with hundreds of lines, four columns, space delimited, TESTB.TXT for example TESTB.TXT --- AA ZZ 12 34 BB YY 56 78 CC XX 91 23 DD VV 45 67 --- I want a new file that has 7 columns, the first four are identical, and the next 3 are the last three of the next line...so... (5 Replies)
Discussion started by: ajp7701
5 Replies

9. Shell Programming and Scripting

assign a command line argument and a unix command to awk variables

Hi , I have a piece of code ...wherein I need to assign the following ... 1) A command line argument to a variable e.g origCount=ARGV 2) A unix command to a variable e.g result=`wc -l testFile.txt` in my awk shell script When I do this : print "origCount" origCount --> I get the... (0 Replies)
Discussion started by: sweta_doshi
0 Replies

10. Shell Programming and Scripting

Using awk and current date command

I am running a command to extract data from a database which has different text and dates (ex. 01/03/07, 05/29/08, 06/05/08). Once the file is created I need to grep for all the text with a current date then redirect the sorted data to another file then email the file out. Here is what I have right... (2 Replies)
Discussion started by: wereyou
2 Replies
Login or Register to Ask a Question