AWK/SED line based search


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK/SED line based search
# 15  
Old 01-02-2012
"p" is just a variable I am using to control the execution of the code within the curly braces. If p=0, it will not enter, if p=1 it will enter.
So, we need to execute the code in the first iteration and hence "p" is initialized to 1 in the BEGIN block. Once it enters the block, we will turn it off i.e. p=0.
When the range limit is reached, we need to re-initialize end, increment n and decrement howmanytimes and so I set p=1 within the if loop.

HTH
--ahamed
# 16  
Old 01-02-2012
Thanks ahamed Smilie i get it now.

Just curious, is it possible to play around with NR values as in the algorithm I had mentioned? or is it not possible with AWK.
# 17  
Old 01-02-2012
Anything is possible Smilie If you can clarify what you want to do with NR, may be we can help.

--ahamed
# 18  
Old 01-02-2012
Why not split the whole file in one go? With this line of awk, you split the whole file into files with filenames like 00000000-00001000.exp for the record range below 1000, 00001000-00002000.exp for the next range and so on.

My input:
Code:
$ cat input.txt 
1	10
2	22
3	33
4	44
5	55
6	66
7	77
8	88
9	99
10	110
11	111
12	112
13	113
14	144
15	155
16	166

Command:
Code:
$ awk 'BEGIN{per=5} {step=int($1/per);fname=sprintf("%09d-%09d.exp",step*per,(step*per)+per);print >>fname}' input.txt

$ grep '' *exp
Code:
000000000-000000005.exp:1	10
000000000-000000005.exp:2	22
000000000-000000005.exp:3	33
000000000-000000005.exp:4	44
000000005-000000010.exp:5	55
000000005-000000010.exp:6	66
000000005-000000010.exp:7	77
000000005-000000010.exp:8	88
000000005-000000010.exp:9	99
000000010-000000015.exp:10	110
000000010-000000015.exp:11	111
000000010-000000015.exp:12	112
000000010-000000015.exp:13	113
000000010-000000015.exp:14	144
000000015-000000020.exp:15	155
000000015-000000020.exp:16	166

I used a per (period) of 5 since my file is very small. Change this value to 1000 for your application.

If your file grows from time to time you really need a way to start exporting where you left off the last time. That is not difficult to do.

Greetings/Groeten,
Eric

---------- Post updated at 07:03 PM ---------- Previous update was at 06:46 PM ----------

While composing my reply I left my PC for a while and did not see there was already an answer given in the meantime. Sorry, Ahamed! I hope I didn't offend you.

Still, I chose a different approach, so maybe it has some use for anyone anyway.

Greetings,
Eric
This User Gave Thanks to edehont For This Post:
# 19  
Old 01-02-2012
@eric ...

the op -- from what i understand -- needed to be able to grab chunks of the original file into manageable chunks therefore the question ...

however ...

if your solution is not needed here, it may be more appropriate to another one or it may give the op another option that he/she has not yet thought of ... so never apologize about offering another solution ... we are all here to learn ...
# 20  
Old 01-02-2012
I'm new on this forum (my second day!), but this was already the second time I gave a solution to a problem Ahamed already solved. I thought it was time to point out I was not competing or anything.

On the other hand: maybe I do want to compete, but not where it comes to producing the most brilliant solution (I would definately loose that battle...), but by providing a solution with the best explanation to give the OP knowledge to build upon instead of mere awe. Smilie

Greetings,
Eric
# 21  
Old 01-03-2012
@edehont- Thanks Smilie I will try it out soon, I am just looking at it.

@ahamed- What I had planned for was something like this-
1. get the range
2. store the values within the range to a file, save the last matching line number
3. get new range, and start looking from the stored line number.
4.repeat over the file

Thanks again!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search a multi-line shell command output and execute logic based on result

The following is a multi-line shell command example: $cargo build Compiling prawn v0.1.0 (/Users/ag/rust/prawn) error: failed to resolve: could not find `setup_panix` in `human_panic` --> src/main.rs:14:22 | 14 | human_panic::setup_panix!(); | ... (2 Replies)
Discussion started by: yogi
2 Replies

2. Shell Programming and Scripting

awk command to search based on 5 user input fields

Field1=”” Field2=”” Field3=”” Field4=”” Field5=”” USER INPUT UP TO 5 FIELDS awk -F , '{ if ( $3 == Field1 && $6 == Field2 && $8 == Field3 && $9 == Field4 && $10 == Field5) print $0 }' /tmp/rodney.outD INPUT FILE (Rodney.outD): ... (3 Replies)
Discussion started by: rmerrird
3 Replies

3. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

4. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

5. Shell Programming and Scripting

Split a line based on : using sed

Hi, i have a file say file1 having following data /abc/def:ghi/jkl/ some other text Now i want to extract only ghi/jkl/using sed, can some one please help me. Thanks Sarbjit (2 Replies)
Discussion started by: sarbjit
2 Replies

6. Shell Programming and Scripting

Printing previous line based on pattern using sed

Hi, I have a written a shell script to get the previous line based on the pattern. For example if a file has below lines: ---------------------------------------------- #UNBLOCK_As _per #As per 205.162.42.92 #BLOCK_As_per #----------------------- #input checks abc.com... (5 Replies)
Discussion started by: Anjan1
5 Replies

7. Shell Programming and Scripting

Append specific lines to a previous line based on sequential search criteria

I'll try explain this as best I can. Let me know if it is not clear. I have large text files that contain data as such: 143593502 09-08-20 09:02:13 xxxxxxxxxxx xxxxxxxxxxx 09-08-20 09:02:11 N line 1 test line 2 test line 3 test 143593503 09-08-20 09:02:13... (3 Replies)
Discussion started by: jesse
3 Replies

8. Shell Programming and Scripting

using sed to conditionally extract stanzas of a file based on a search string

Dear All, I have a file with the syntax below (composed of several <log ..... </log> stanzas) I need to search this file for a number e.g. 2348022225919, and if it is found in a stanza, copy the whole stanza/section (<log .... </log>) to another output file. The numbers to search for are... (0 Replies)
Discussion started by: aitayemi
0 Replies

9. Shell Programming and Scripting

sed search and replace in next line

Hello, I am hoping someone can provide some guidance on using context based search and replace to search for a pattern and then do a search and replace in the line that follows it. For example, I have a file that looks like this: <bold>bold text </italic> somecontent morecontent... (3 Replies)
Discussion started by: charissaf67
3 Replies

10. Shell Programming and Scripting

search file, change existing value based on input (awk help)

I have a file (status.file) of the form: valueA 3450 valueB -20 valueC -340 valueD 48 I am tailing a data.file, and need to search and modify a value in status.file...the tail is: tail -f data.file | awk '{ print $3, ($NF - $(NF-1)) }' which will produce lines that look like this: ... (3 Replies)
Discussion started by: nortonloaf
3 Replies
Login or Register to Ask a Question