Using NR with two variables at the beginning of awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using NR with two variables at the beginning of awk
# 1  
Old 06-20-2009
Error Using NR with two variables at the beginning of awk

hi My requirement is this:
I have a file having around 100000 records pipe delimited. Now I want to compare record 1 with record 2 and similarly record3 with record 4, this goes on.. For this purpose i put a script as follows:

#!bin/ksh

ct_line=1
nxt_line=`expr ${ct_line} + 1`

awk -F "|" -v i=${ct_line} -v j=${nxt_line} '{print i j} NR==i {
ct_dt=$1
ct_name=$2
}
NR==j {
nxt_dt=$1
nxt_name=$2
}
{print ct_dt nxt_dt}
i+=1
j=i+1
' <infile.txt >outfile.txt

Expected output:
1 2
20090501 20090502

Actual Output:
1 2
20090501

Its not all processing NR==j.. Why??

If there is any otehr way to do it in awk??
I mean, I need to read two lines at a time and assign their values to respective variables and compare..?/

Its an urgent requirement.. plz help
# 2  
Old 06-20-2009
As a basis for what it is you're trying to do...
Code:
awk -F "|" '
  (NR-1) % 2 == 0 { CT_DT = $1; CT_NAME = $2; next }
  (NR-1) % 2 == 1 { print CT_DT, $1; print CT_NAME, $2 }
' infile.txt > outfile.txt


Last edited by Scott; 06-20-2009 at 11:28 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

2. Shell Programming and Scripting

awk - Why can't value of awk variables be passed to external functions ?

I wrote a very simple script to understand how to call user-defined functions from within awk after reading this post. function my_func_local { echo "In func $1" } export -f my_func_local echo $1 | awk -F"/" '{for (k=1;k<=NF;k++) { if ($k == "a" ) { system("my_local_func $k") } else{... (19 Replies)
Discussion started by: sreyan32
19 Replies

3. Shell Programming and Scripting

Beginning awk question

I have a report that I'd like to print columns headers on. The code appears as follows: gawk '{for (i=1; i<=NF; i++) if ($i < 0) $i = -$i; print }'|gawk '{ printf "%-78s%-30s%-30s%-30s\n", $1,$2, $3, $4 }' |awk '{for (i=1; i<=NF; i++) if ($i < 0) $i = -$i; print }' report_1020I am trying to make... (3 Replies)
Discussion started by: newbie2010
3 Replies

4. Shell Programming and Scripting

One of the awk column spilling on the new line from beginning

I am having a problem where the Compute field/column is spilling over to the new line. Here's the piece of code and the o/p:- Code Sniplet:- for id in `qstat -u "*" -q "$Queue" -s r |sed -n '3,$ p'|awk -F" " '{print $1}'` do NODELIST=`cat /scratch/$id.hostlist.*|awk -F" " '{print $1,$2}'|tr '... (5 Replies)
Discussion started by: grish
5 Replies

5. Shell Programming and Scripting

AWK - variables

I have a data file like this: 49960 1157 32390 1227 1268 31 8 21 12 115 18493 67 250 2 2 237704 369658 52 21 312 38 27746 3174 19 160 9 555 6337 6071 43 33 I want to separate the field to three groups, $1 and $2 are the first group, $3 and $4 are the second group, $5 and $6... (1 Reply)
Discussion started by: xshang
1 Replies

6. Shell Programming and Scripting

ksh passing to awk multiple dyanamic variables awk -v

Using ksh to call a function which has awk script embedded. It parses a long two element list file, filled with text numbers (I want column 2, beginning no sooner than line 45, that's the only known thing) . It's unknown where to start or end the data collection, dynamic variables will be used. ... (1 Reply)
Discussion started by: highnthemnts
1 Replies

7. Shell Programming and Scripting

Place variables at the beginning of each line

Hello all, I am very new to the shell scripting and I hope someone can help me with this. I have thousands of files with certain format of information and I need to do this for all my files. For each file, grab the numbers in the first and second rows and place them in the position 1 and 2... (8 Replies)
Discussion started by: GoldenFire
8 Replies

8. Shell Programming and Scripting

Removing from beginning to first : using awk

A have a file npt02-sr40-syn-dc0p014-32x24drw.log:0. Best Value = 0.00144914 npt02-sr40-syn-dc0p014-32x24drw.log:1. Best Value = 0.00115706 npt02-sr40-syn-dc0p014-32x24drw.log:2. Best Value = 0.00094345 npt02-sr40-syn-dc0p014-32x24drw.log:3. Best Value = 0.000925552... (11 Replies)
Discussion started by: kristinu
11 Replies

9. Shell Programming and Scripting

Using AWK variables.

Hi all, I am new to the forum and Shell Script programming. The problem is: I need to do a script to search all system processes and show me hierarchical way the number of bytes occupied by each of the regions of the memory map of each process. Today I got to show me the number of regions in... (3 Replies)
Discussion started by: cougar_rea
3 Replies

10. Shell Programming and Scripting

Swapping lines beginning with certain words using sed/awk

I have a large file which reads like this: fixed-address 192.168.6.6 { hardware ethernet 00:22:64:5b:db:b1; host X; } fixed-address 192.168.6.7 { hardware ethernet 00:22:64:5b:db:b3; host Y; } fixed-address 192.168.6.8 { hardware ethernet 00:22:64:5b:db:b4; host A; }... (4 Replies)
Discussion started by: ksk
4 Replies
Login or Register to Ask a Question