Put lines of a file in an array with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Put lines of a file in an array with awk
# 1  
Old 04-28-2010
Put lines of a file in an array with awk

Hello,

Is there any way in awk to put every line of a file in an array and so we can like this print the line we want.

For example, if we have this file
Code:
aaa eee
bbb fff
ccc ggg
ddd hhh

So we can print to the output the 3rd line only
Code:
ccc ggg

If it is possible, please put the code and thx in advance.

Regards,

Last edited by zaxxon; 04-28-2010 at 11:49 AM.. Reason: use code tags please, ty
# 2  
Old 04-28-2010
What did you try so far? Why an array if you know which line you want - for further usage?

If you are after a pattern, you can use a simple grep; if you are after the line number a short sed can do the trick.
# 3  
Old 04-28-2010
The following AWK function reads a file into an array :
Code:
function read_file_into_array(file, array     ,status, record, count ) {
   count  = 0;
   while (1) {
      status = getline record < file
      if (status == -1) {
         print "Failed to read file " file;
         exit 1;
      }
      if (status == 0) break;
      array[++count] = record;
   }
   close(file);
   return count
}

Example (AWK code):
Code:
   records_count = read_file_into_array("input.txt", File);
   print "Records count=" records_count;
   for (i=1; i<=records_count; i++)
      printf "[%d] %s\n", i, File[i];

Input File :
Code:
Line 1
Line 2
Line 3

Output :
Code:
Records count=3
[1] Line 1
[2] Line 2
[3] Line 3

Jean-Pierre.
# 4  
Old 04-28-2010
Here I gave an example of a 3 lines file.

But I want to do is that I have a file of let's say 500000 lines and at the moment I want to display one line corresponding to its number.

So the awk shelk asks you for the line number which will display it only.

Regs,

---------- Post updated at 10:42 AM ---------- Previous update was at 10:17 AM ----------

Hello Jean Pierre,

Thx a lot for your reply.

I tried your code, but I have this error and I don't see the problem ...

Code:
function read_file_into_array(file, array     ,status, record, count ) {
   count  = 0;
   while (1) {
      status = getline record < file
      if (status == -1) {
         print "Failed to read file " file;
         exit 1;
      }
      if (status == 0) break;
      array[++count] = record;
   }
   close(file);
   return count
}

Error:
awk: syntax error near line 1
awk: bailing out near line 1

Regs,
# 5  
Old 04-28-2010
if you just want to print line_number in file
Code:
head -$line_number $file | tail -1

# 6  
Old 04-28-2010
Rany, have you tried nawk instead of awk ?

A full example :
Code:
$ cat rany.awk
function read_file_into_array(file, array     ,status, record, count ) {
   count  = 0;
   while (1) {
      status = getline record < file
      if (status == -1) {
         print "Failed to read file " file;
         exit 1;
      }
      if (status == 0) break;
      array[++count] = record;
   }
   close(file);
   return count
}
BEGIN {
   records_count = read_file_into_array("input.txt", File)
   print "The file contains",records_count,"records.\n"
}
{
   printf "Record #%d [%s]\n\n", $1, File[$1];
}
$ cat input.txt
This is record 1
Line 2
Last line
$ awk -f rany.awk
The file contains 3 records.

2
Record #2 [Line 2]

1
Record #1 [This is record 1]

3
Record #3 [Last line]

^D
$

This is too much if you only want to print a specific line.
You can use the technique posted by frans or
Code:
sed -n "$line_number{p;q;}" input.txt

Jean-Pierre.
# 7  
Old 04-28-2010
To print one line of a file:
Code:
awk NR==$line_number file

With arrays in awk:
Code:
awk '{array[NR]=$0}
END{
  print array[i]
  # more stuff with array[]
}' i=$line_number file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to put the command to remove duplicate lines in my awk script?

I create a CGI in bash/html. My awk script looks like : echo "<table>" for fn in /var/www/cgi-bin/LPAR_MAP/*; do echo "<td>" echo "<PRE>" awk -F',|;' -v test="$test" ' NR==1 { split(FILENAME ,a,""); } $0 ~ test { if(!header++){ ... (12 Replies)
Discussion started by: Tim2424
12 Replies

2. Shell Programming and Scripting

Put the lines from file A to end of lines in file B

I really can't figure this one out. I have 2 files, one file is a list of hostnames and the other is a list of their corresponding IPs: fileA: example.com another.org thirdie.net fileB: 1.1.1.1 2.2.2.2 3.3.3.3 I want to create a fileC that looks like: example.com 1.1.1.1... (2 Replies)
Discussion started by: zstar
2 Replies

3. Shell Programming and Scripting

Read file and get the data then put it in array

Hi, I have a file called "readfile" it contains below parameters #cat readfile word=/abc=225,/abc/cba=150 three=12 four=45 five=/xyz/yza likewise multiple line. From the above file, I have to read "word" output should be like, /abc /abc/cba these values need to be put in... (3 Replies)
Discussion started by: munna_dude
3 Replies

4. Shell Programming and Scripting

How to load an array with desired lines with awk

Hi everyone, Please some help over here. I've written the below script using ranges (/Initial_pattern/,/Final_Pattern/)to extract only those lines of interest for me: awk ' /^Category/,/^$/{print $1}; /Titles/,/^$/{print $1}; /Authors/,/^$/{print $1}' inputfile To process this input:... (8 Replies)
Discussion started by: cgkmal
8 Replies

5. Shell Programming and Scripting

perl, put one array into many array when field is equal to sth

Hi Everyone, #!/usr/bin/perl use strict; use warnings; my @test=("a;b;qqq;c;d","a;b;ggg;c;d","a;b;qqq;c;d"); would like to split the @test array into two array: @test1=(("a;b;qqq;c;d","a;b;qqq;c;d"); and @test2=("a;b;ggg;c;d"); means search for 3rd filed. Thanks find the... (0 Replies)
Discussion started by: jimmy_y
0 Replies

6. Shell Programming and Scripting

Need to parse file "x" lines at a time ... awk array?

I have files that store multiple data points for the same device "vertically" and include multiple devices. It repeats a consistant pattern of lines where for each line: Column 1 is a common number for the entire file and all devices in that file Column 2 is a unique device number Column 3 is... (7 Replies)
Discussion started by: STN
7 Replies

7. Shell Programming and Scripting

awk -- sort out the out put file

Hi, using awk /sed, I need to sort out the input file. here is the example input file=== Name= shashi | country= india | region = asia | dept= ww Name= jon | region = asia | dept= xx Name=sam | dept= ww Name= anthony | country=england | dept= xx Name= sumo | country= china output... (3 Replies)
Discussion started by: hegdeshashi
3 Replies

8. UNIX for Dummies Questions & Answers

need to extract few lines from a file and put it in another file

Hi all, I have a file which contains comments,dotted lines(-------),actual records etc.I need to fetch only the actual records and put into a new file neglecting/deleting other rows.I have a header record too which contains the column names for the actual records.i need to discard this too.how... (5 Replies)
Discussion started by: ganesh_248
5 Replies

9. Shell Programming and Scripting

Put two lines into one from file

Hello, I have a script that generates the following file named /tmp/rfkill: rf55 pts/13 Jul 10 06:38 (10.72.11.44) 15782 pts/13 5:07 b rf56 pts/15 Jul 10 06:53 (10.72.11.9) 18552 pts/15 0:28 b rf55 pts/39 Jul 10 09:12 (10.72.11.44) 19354 pts/39... (4 Replies)
Discussion started by: raidzero
4 Replies

10. Shell Programming and Scripting

put the contents of this file into a variable with multiple lines

I have a file that contains the following lines the brown quick fox jumped over the white laze dog 0123456789 I wanted to put the contents of this file into a variable so I used this code: VAR_LIST=`cat $2` where $2 is the file name passed as an argument to the script If I... (3 Replies)
Discussion started by: Nomaad
3 Replies
Login or Register to Ask a Question