Saving file content in arrays using AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Saving file content in arrays using AWK
# 8  
Old 02-09-2011
Can you post some lines of your dumpfile?
# 9  
Old 02-09-2011
Code:
bash-2.05# more dumpfile | grep 9230534001
923053400100
923053400102
923053400103
923053400104
923053400105
923053400106
923053400107
923053400108
923053400110
923053400112
923053400114
923053400115
923053400116
923053400117

# 10  
Old 02-09-2011
Sorry, I forgot a next statement on the first line. The code should be:
Code:
awk -F, 'NR==FNR {arr1[++c]=$1; arr2[c]=$2; next}	# Fill arrays
{
  for(i=1;i<=c;i++) {		# Loop through arrays
    if($0 >= arr1[i] && $0 <= arr2[i]) {
      print
      break
    }
  }
}' inp.txt dumpfile > out.txt

This is what I get:
Code:
$ cat inp.txt
923053400100,923053449989
923072600118,923072622197
923076470027,923076493135
$
$ cat dumpfile
1231231331
12123133
923053400100
923053400102
923053400103
923053400104
923053400105
923053400106
923053400107
923053400108
923053400110
923053400112
923053400114
923053400115
923053400116
923053400117
12131313
211231231
99999999999
$
$ awk -F, 'NR==FNR {arr1[++c]=$1; arr2[c]=$2; next}	# Fill arrays
{
  for(i=1;i<=c;i++) {		# Loop through arrays
    if($0 >= arr1[i] && $0 <= arr2[i]) {
      print
      break
    }
  }
}' inp.txt dumpfile
923053400100
923053400102
923053400103
923053400104
923053400105
923053400106
923053400107
923053400108
923053400110
923053400112
923053400114
923053400115
923053400116
923053400117
$

This User Gave Thanks to Franklin52 For This Post:
# 11  
Old 02-10-2011
wow that was genious thanks alot. can u tell me why did u use arr1[++c] instead of arr1[c] as u did for arr2[c]; and the use of next statement inside the awk body(i mean what will it do)
# 12  
Old 02-10-2011
Quote:
Originally Posted by atikan
wow that was genious thanks alot. can u tell me why did u use arr1[++c] instead of arr1[c] as u did for arr2[c]; and the use of next statement inside the awk body(i mean what will it do)
c is used as an index of the array a and ++c increases the value of c with 1.

arr1[1] keeps the 1e field and arr2[1] the 2nd field of the 1e line
arr1[2] keeps the 1e field and arr2[1] the 2nd field of the 2e line and so forth...

The condition NR==FNR is true if we read the first file. With the next command we read the next line of the 1e file because the commands after the next command is for processing the 2e file.

If this is new to you, you should have a read of:

Awk - A Tutorial and Introduction - by Bruce Barnett
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk command to get file content until 2 occurrence of pattern match

AWK command to get file content until 3 occurrence of pattern match, INPUT FILE: JMS_BODY_FIELD:JMSText = <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <custOptIn xmlns="http://com/walm/ta/cu/ccs/xml2"> <person>Romi</person> <appName>SAP</appName> </custOptIn> ... (4 Replies)
Discussion started by: prince1987
4 Replies

2. Shell Programming and Scripting

awk : split file and rename and save in path according to content

Hello, I'm using Windows 7 ; sed, awk and gnuwin32 are installed. I have a big text file I need to manipulate. In short, I will have to split it in thousands of short files, then rename and save in a folder which name is based upon filename. Here is a snippet of my big input.txt file (this... (4 Replies)
Discussion started by: sellig
4 Replies

3. Shell Programming and Scripting

Change a file content format using awk

Hi, i have a file input.txt Continent North America Country USA Capital Washington D.C. Country Canada Capital Ottawa Continent South America Country Argentina Capital Buenos Aires Country Brazil Capital Brasília Coutry Colombia Capital Bogotá and i want to get an output.txt ... (3 Replies)
Discussion started by: fastlane3000
3 Replies

4. Shell Programming and Scripting

awk saving field of first file into array

Hello guys, I just start trying out AWK and encounter a problem, I try to think a bit but seems my way is incorrect. I have two input file, with the first file has only one field, the second file has 3 fields, I suppose to do stuffs to them by writing an awk program, kinda sort them out. Since I... (15 Replies)
Discussion started by: RozenKristal
15 Replies

5. Shell Programming and Scripting

[ask]awk in csh to extract content from file

Please suggest a method (in c shell or any other shell) to implement following: -To read file1.txt (sample file1 given below) -To save name field in a variable <name> -To save parameter field in a variable <parameter> for ex. let a line in file1.txt be : bill height weight the extracted... (12 Replies)
Discussion started by: animesharma
12 Replies

6. Programming

question about int arrays and file pointer arrays

if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies

7. Shell Programming and Scripting

Read a file content with awk and sed

Hello , I have huge file with below content. I need to read the numeric values with in the paranthesis after = sign. Please help me with awk and sed script for it. 11.10.2009 04:02:47 Customer login not found: identifier=(0748502889) prefix=(TEL) serviceCode=(). 11.10.2009 04:03:12... (13 Replies)
Discussion started by: rmv
13 Replies

8. Shell Programming and Scripting

Need help with awk - how to read a content of a file from every file from file list

Hi Experts. I need to list the file and the filename comes from the file ListOfFile.txt. Basicly I have a filename "ListOfFile.txt" and it contain Example of ListOfFile.txt /home/Dave/Program/Tran1.P /home/Dave/Program/Tran2.P /home/Dave/Program/Tran3.P /home/Dave/Program/Tran4.P... (7 Replies)
Discussion started by: tanit
7 Replies

9. Shell Programming and Scripting

saving values in file in an array in awk

hi i am trying to save values in a file in an array in awk..the file is as follows: 0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0, so far i have this: awk 'BEGIN {RS="\n";FS=","} { for(i=1;i<=NR;i++) { for(j=1;j<=NF;j++) { a=$j; } } (4 Replies)
Discussion started by: npatwardhan
4 Replies

10. Shell Programming and Scripting

Content extract of a file using awk

Hi Everyone, I have a file with the below content: File1.txt ====== ### ###==> the below table was created for testing1 purpose; ### create table 123 ( field1 date, field2 char(10) primary key(field1) ); ### ###==> the below table was created... (5 Replies)
Discussion started by: nr_shan
5 Replies
Login or Register to Ask a Question