Go back in AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Go back in AWK
# 1  
Old 12-08-2009
Go back in AWK

Hello!

I need to come back one line in awk when i detect a regular expression, i'm working with this:

Code:
/<POLITIC>/,/<\/POLITIC>/

when i found the line that matches with <\/POLITIC> i need to come back one line, the opposite of getline that takes a new line and continue executing the code in the same line, i need to come back one line and continue executing the code with this new line.

Someone knows how can i do?

Thanks!
# 2  
Old 12-08-2009
What you're working with (i.e. "I'm working with this") doesn't tie in with the question you're asking.

Code:
/<POLITIC>/,/<\/POLITIC>/

This doesn't match a regular expression. It matches a range of lines between two "regular expressions".

You want to "go back" from what after matching "<\POLITIC>"? Back from "<\POLITIC>" or back from the line before "<\POLITIC>"?

More useful is if you show what your input is and what your output should be, as well as what code you've written so far.
# 3  
Old 12-08-2009
Yes, sorry, it matches a range, i didn't express correctly.

>You want to "go back" from what after matching "<\POLITIC>"? Back from "<\POLITIC>" or back from the line before "<\POLITIC>"?

I want to go back from the line before "<\POLITIC>".

When it arrives to the end of the range: "<\POLITIC>" i wanna go back one line.
# 4  
Old 12-08-2009
Well, there is no "rewind" function in awk, so:

Code:
$ cat Test
awk '
  NR == 1 { LINE = $0; next }
  /<POLITIC>/,/<\/POLITIC>/ { if ( $0 ~ /<\/POLITIC>/ ) print "POLITIC:  goes here" }
  { print LINE; LINE = $0 }
' file1


$ cat file1
1
2
<POLITIC>
blah 1
blah 2
blah 3
</POLITIC>
blah
more blah
and still
more blah


$ ./Test
1
2
<POLITIC>
blah 1
blah 2
POLITIC:  goes here
blah 3
</POLITIC>
blah
more blah
and still

# 5  
Old 12-08-2009
My approach when I need to do this is to pass the file name twice to the script. On the first pass write the line numbers you are interested in to an array. On the second pass do your processing.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. What is on Your Mind?

I'm back

Hi all, I used to post here years ago, and was a moderator, my old username: zazzybob. Anyway, after a few years away focusing on my career, I'm back and keener than ever to get involved in the unix.com community again. I'm looking forward to getting back into the swing of things, helping... (11 Replies)
Discussion started by: tokiwinter
11 Replies

2. Shell Programming and Scripting

Use AWK to move matched line back one?

Can somebody help me with this? I'm sure it's a no-brainer if you know awk... but I don't. Input: Blah Blah Me love you long time Blah Blah awk magic with 'long time' ==> Output: Blah Blah Me love you long time (0 Replies)
Discussion started by: Ryan.
0 Replies

3. IP Networking

Back-to-Back Connection using HBAs

Hi every body, Is it possible to connect two servers Back-to-Back (Point-to-Point) using HBA adapters & using Fiber. Note it is direct connection & there is no switches between the servers. I'm concern about using HBA adapters, it is possible or not. Thanks in advance. :) (3 Replies)
Discussion started by: aldowsary
3 Replies

4. Shell Programming and Scripting

get value back from awk script

I want to receive a value back into shell script from awk code block. for example - I want to use the substring value from the awk script inside the shell code's scope. do dir=`dirname $file`; echo | awk '{print substr("'"${dir}"'",'$pathlength')}'; done can someone guide me how to... (2 Replies)
Discussion started by: iyerhere
2 Replies

5. AIX

back to back printing in UNIX

Hi , Can you suggest me how to back to back printing in UNIX? Is there any way? Kindly advise. Regards Vijaya Amirtha Raj (3 Replies)
Discussion started by: amirthraj_12
3 Replies

6. Shell Programming and Scripting

Is it possible to pass variable from awk to shell back

I am passing a varaible to from Shell to awk then I am doing some maniplation for that variable inside awk. I want that maniplated variable value back to shell , Is this possible .Please let me know. (12 Replies)
Discussion started by: unishiva
12 Replies

7. HP-UX

Help about back up

Hi this is Ramana.sv new to this group, please help me about my question, i am using HP-UX11.11i with oracle 10G this server is in india and i have another server in US with same HP-UX with oracle10G, what i want is i want to rename the local database in local HP server and copy the database from... (0 Replies)
Discussion started by: mcseramana
0 Replies

8. UNIX for Advanced & Expert Users

help with back up please

Can anyone please tell me how safe is the following backup script? Does it really backing up the WHOLE system or just part of it? I do that with the system running an oracle database. Will I be able to restore the system in case of a fault? <pre> Here is the output of 'df -k' ... (11 Replies)
Discussion started by: guest100
11 Replies
Login or Register to Ask a Question