Jul 31, 2003 #1 ryanc2 MIS Joined Apr 18, 2003 Messages 73 Location US I need to pull the 2 lines before a string of "^" charactors. For example: Date Version ^^^^^^^^^^^^^^^^^^^ Is it possible to grep for the ^ and retrieve the previous 2 lines? Thanks in advance.
I need to pull the 2 lines before a string of "^" charactors. For example: Date Version ^^^^^^^^^^^^^^^^^^^ Is it possible to grep for the ^ and retrieve the previous 2 lines? Thanks in advance.
Jul 31, 2003 #2 Salem Programmer Joined Apr 29, 2003 Messages 2,455 Location GB Well if you have GNU grep, then you can use this option Code: `-B NUM' `--before-context=NUM' Print NUM lines of leading context before matching lines. Upvote 0 Downvote
Well if you have GNU grep, then you can use this option Code: `-B NUM' `--before-context=NUM' Print NUM lines of leading context before matching lines.
Jul 31, 2003 Thread starter #3 ryanc2 MIS Joined Apr 18, 2003 Messages 73 Location US I'm working in SCO UNIX. I found that option searching some other forums, but it won't work for me. Upvote 0 Downvote
Aug 1, 2003 #4 Salem Programmer Joined Apr 29, 2003 Messages 2,455 Location GB Well you can do it with an AWK script Code: #!/bin/awk -f # find the match /^\^/ { print l1 print l2 } # record the previous two lines { l1 = l2 l2 = $0 } # end Save this as say 'myscript' (choose your own name), then type this command line Code: awk -f myscript file.txt file.txt is the file you want to search Upvote 0 Downvote
Well you can do it with an AWK script Code: #!/bin/awk -f # find the match /^\^/ { print l1 print l2 } # record the previous two lines { l1 = l2 l2 = $0 } # end Save this as say 'myscript' (choose your own name), then type this command line Code: awk -f myscript file.txt file.txt is the file you want to search
Aug 1, 2003 #5 Ygor Programmer Joined Feb 21, 2003 Messages 623 Location GB Or just... awk '/^\^/ {print a[(NR-2)%2] "\n" a[(NR-1)%2]} {a[NR%2]=$0}' file.txt Upvote 0 Downvote