Awk Line/Row Position


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk Line/Row Position
# 1  
Old 04-19-2011
Awk Line/Row Position

Hi guys.

I'd just like to know if its possible to change the actual line/row position in awk while its busy processing a file. In other words, is it possible to jump from line 10, back up to line 5 and continue processing line-by-line from then onwards? or is the way around this to add all lines to an array and jump between lines within the array?

Example:
A file contains N records. Each record has a corresponding/matching record (in no particular order)

record1(pair1)
record2(pair2)
record3(pair3)
record4(pair1) #match for record1
record5(pair4)
record6(pair3) #match for record3
record7(pair2) #match for record2
...
..
.

A generic script to hopefully assist in my explaination.

Code:
BEGIN { numPairsFound = 0;
        while(getline > 0) {
           found="false";
           count=0;
           if($0 ~ "one") { #assume pair1 is unique and will never be read in again by awk
              pair1=$0; 
              while(getline > 0) {
                 if($0 ~ "two") { #matching record found
                   pair2=$0; 
                   found="true";
                   break;
                 }
              }
           }   
           if(found == "true") {
              printf("%s\n","Pair found:");
              printf("%s\n",pair1);
              printf("%s\n",pair2);
              numPairsFound++;
           }           
        }
}
END { print "Number of pairs found: " numPairsFound; }

I apologize in advance if this question has already been asked.
# 2  
Old 04-19-2011
Can you please post the output that you are expecting.
This User Gave Thanks to panyam For This Post:
# 3  
Old 04-19-2011
No, you cannot "jump" back, but you can always process the same file twice in one go.
If you provide a more descriptive sample input and a desired output, we could probably avoid guessing/jumping...
This User Gave Thanks to vgersh99 For This Post:
# 4  
Old 04-19-2011
Wow, that was quick. Thanks for your responses guys.

Sorry, I'm unable to provide you with a more descriptive sample input. vgersh99, your response was all I needed. I can now go and explore other solutions.

Thanks again.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk sed to repeat every character on same position from the upper line replacing whitespace

Hello is it possible with awk or sed to replace any white space with the previous line characters in the same position? I am asking this because the file I have doesn't always follow a pattern. For example the file I have is the result of a command to obtain windows ACLs: icacls C:\ /t... (5 Replies)
Discussion started by: nakaedu
5 Replies

2. Shell Programming and Scripting

Column to row and position data in a text file

Hi everyone.. I have a list of values in a file... a, b, c, 1, 2, 3, aaaa, bbbbb, I am interested in converting this column to a row.. "text",aaaa, bbbb a,1 (7 Replies)
Discussion started by: manihi
7 Replies

3. UNIX for Dummies Questions & Answers

How to find position of blank row in UNIX?

Hi I have file "emp.txt"like below Emp Id Name 123 aa 123 bb 223 cc 233 dd 334 ee Please help me to know that the position of the blank row. (5 Replies)
Discussion started by: rock_01
5 Replies

4. 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

5. Shell Programming and Scripting

Subtracting each row from the first row in a single column file using awk

Hi Friends, I have a single column data like below. 1 2 3 4 5 I need the output like below. 0 1 2 3 4 where each row (including first row) subtracting from first row and the result should print below like the way shown in output file. Thanks Sid (11 Replies)
Discussion started by: ks_reddy
11 Replies

6. Shell Programming and Scripting

Remove text from n position to n position sed/awk

I want to remove text from nth position to nth position couple of times in same line my line is "hello is there anyone can help me with this question" I need like this ello is there anyone can help me with question 'h' is removed and 'this' removed from the line. I want to do this... (5 Replies)
Discussion started by: elamurugu
5 Replies

7. Shell Programming and Scripting

awk to print all row to one line

HI , My unix command output look like below , bste-ngh-bt9.hecen.com EA1981, 09/01/2010 17:56:56 03/31/2011 00:00:00 I want this output changed to , all i need in one line with space ! bste-ngh-bt9.hecen.com EA1981 09/01/2010 17:56:56 03/31/2011 00:00:00 need to get all in... (17 Replies)
Discussion started by: gnanasekar_beem
17 Replies

8. Shell Programming and Scripting

awk command : row by row merging of two files

I want to write a scrpit to merge files row wise (actually concatinating) main.txt X Y Z file 1 A B C file 2 1 2 3 now i want the script to check if the file1 is empty or not, if empty then make it like A B C 1 2 3 again to check if second file is empty if not do as done... (0 Replies)
Discussion started by: shashi792
0 Replies

9. Shell Programming and Scripting

Deleting Characters at specific position in a line if the line is certain length

I've got a file that would have lines similar to: 12345678 x.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00 23456781 x.00 xx.00 xx.00 xx.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00 34567812 x.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00 45678123 x.00 xx.00 xx.00 xx.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00 xx.00... (10 Replies)
Discussion started by: Cailet
10 Replies
Login or Register to Ask a Question