Sponsored Content
Top Forums Shell Programming and Scripting awk script: print line number n of another file Post 302402628 by kpg on Wednesday 10th of March 2010 08:24:14 AM
Old 03-10-2010
Thank you for your reply. I'll try to explain what I want to do:

fileA is:
000000011111000
000000329100001
000000330100001

the contents indicate line numbers (position 0 to 8) to look for in fileB
fileB is:
dataline1
dataline2
...
dataline32
dataline33
...


Now the output of my awk should be for each line in fileA
  • prettyfied line from fileA. eg print "error on line" line_num
  • prettyfied matching line (dataline line_num in fileb)

I just do not understand how to modify your code to achieve this.Smilie
Code:
1 gawk '
2 BEGIN{} 
3 NR==FNR{
4   line_num=int( substr($0,1,9) ) ; 
5   a[line_num] ;
6   next}
7  (FNR in a)
8 ' fileA fileB

at line 2 i can insert my initialization stuff.
#3. builds the array?
#4 sets the line_num variable;
#5 no idea what this does
#6 a search said "next means nothing else is done with this line from the first file"
#7 no idea

Maybe you can answer my questions,please?
Is it possible to let awk print a mixed output from fileA and matching lines in fileB

Thank you very much!
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

shell script(Preferably awk or sed) to print selected number of columns from each row

Hi Experts, The question may look very silly by seeing the title, but please have a look at it clearly. I have a text file where the first 5 columns in each row were supposed to be attributes of a sample(like sample name, number, status etc) and the next 25 columns are parameters on which... (3 Replies)
Discussion started by: ks_reddy
3 Replies

2. Shell Programming and Scripting

awk/shell script to print each line to a file

Dear People, My query is: have a file, which looks likes this: 10 20 30 40 50 1 2 3 4 5 100 200 300 400 500 what i need is: "PRINT EACH LINE TO AN UNIQUE FILE" desired output: file 1 10 20 30 40 50 file 2 1 2 3 4 5 (3 Replies)
Discussion started by: saint2006
3 Replies

3. Shell Programming and Scripting

sed script - print the line number along with the line

Hi, I want to print the line number with the pattern of the line on a same line using multi-patterns in sed. But i don't know how to do it. For example, I have a file abc def ghi I want to print 1 abc 2 def 3 ghi I know how to write it one line code, but i don't know how to put... (11 Replies)
Discussion started by: ntpntp
11 Replies

4. Shell Programming and Scripting

awk script to return the middle line number

I need an awk script that returns the 1st field of the line in the middle of a file. For example, if a file I have has 6 lines, I want it to return the 1st field on line number 3. If a file has 7 lines, I want the script to return the 1st field on line number 4. File1: 3 214 4 219 5 226 2... (8 Replies)
Discussion started by: jontjioe
8 Replies

5. Shell Programming and Scripting

Help on Sed/awk/getting line number from file

I Have file1 with below lines : #HostNameSelection=0 :NotUsed #HostNameSelection=1 :Automatic #HostNameSelection=3 :NotForced I have file2 which has similar lines but with different values I want to copy the changes from file1 to file2 ,line by line only if line begins with '#'. for... (7 Replies)
Discussion started by: mvr
7 Replies

6. Shell Programming and Scripting

awk/sed script to print each line to a separate named file

I have a large 3479 line .csv file, the content of which looks likes this: 1;0;177;170;Guadeloupe;x 2;127;171;179;Antigua and Barbuda;x 3;170;144;2;Umpqua;x 4;170;126;162;Coos Bay;x ... 1205;46;2;244;Unmak Island;x 1206;47;2;248;Yunaska Island;x 1207;0;2;240;north sea;x... (5 Replies)
Discussion started by: kalelovil
5 Replies

7. Shell Programming and Scripting

find string nth occurrence in file and print line number

Hi I have requirement to find nth occurrence in a file and capture data from with in lines (between lines) Data in File. <QUOTE> <SESSION> <ATTRIBUTE NAME='Parameter Filename' VALUE='file1.parm'/> <ATTRIBUTE NAME='Service Name' VALUE='None'/> </SESSION> <SESSION> <ATTRIBUTE... (6 Replies)
Discussion started by: tmalik79
6 Replies

8. Shell Programming and Scripting

awk script -print line when $2 > $2 of previous line

Hi all, From a while loop I am reading a sorted file where I want to print only the lines that have $1 match and $2 only when the difference from $2 from the previous line is > 30. Input would be like ... AN237 010 193019 0502 1 CSU Amoxycillin AN237 080 ... (2 Replies)
Discussion started by: gafoleyo73
2 Replies

9. Shell Programming and Scripting

Match pattern and print the line number of occurence using awk

Hi, I have a simple problem but i guess stupid enough to figure it out. i have thousands rows of data. and i need to find match patterns of two columns and print the number of rows. for example: inputfile abd abp 123 abc abc 325 ndc ndc 451 mjk lkj... (3 Replies)
Discussion started by: redse171
3 Replies

10. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies
libapache2-mod-perl2-2.0.7::docs::api::Apache2::DirectivUsermContributed Perl Documelibapache2-mod-perl2-2.0.7::docs::api::Apache2::Directive(3pm)

NAME
Apache2::Directive - Perl API for manipulating the Apache configuration tree Synopsis use Apache2::Directive (); my $tree = Apache2::Directive::conftree(); my $documentroot = $tree->lookup('DocumentRoot'); my $vhost = $tree->lookup('VirtualHost', 'localhost:8000'); my $servername = $vhost->{'ServerName'}; use Data::Dumper; print Dumper $tree->as_hash; my $node = $tree; while ($node) { print $node->as_string; #do something with $node my $directive = $node->directive; my $args = $node->args; my $filename = $node->filename; my $line_num = $node->line_num; if (my $kid = $node->first_child) { $node = $kid; } elsif (my $next = $node->next) { $node = $next; } else { if (my $parent = $node->parent) { $node = $parent->next; } else { $node = undef; } } } Description "Apache2::Directive" provides the Perl API for manipulating the Apache configuration tree API
"Apache2::Directive" provides the following functions and/or methods: "args" Get the arguments for the current directive: $args = $node->args(); obj: $node ( "Apache2::Directive object" ) ret: $args ( string ) Arguments are separated by a whitespace in the string. since: 2.0.00 For example, in httpd.conf: PerlSwitches -M/opt/lib -M/usr/local/lib -wT And later: my $tree = Apache2::Directive::conftree(); my $node = $tree->lookup('PerlSwitches'); my $args = $node->args; $args now contains the string "-M/opt/lib -M/usr/local/lib -wT" "as_hash" Get a hash representation of the configuration tree, in a format suitable for inclusion in <Perl> sections. $config_hash = $conftree->as_hash(); obj: $conftree ( "Apache2::Directive object" ) The config tree to stringify ret: $config_hash ( HASH reference ) since: 2.0.00 For example: in httpd.conf: <Location /test> SetHandler perl-script PerlHandler Test::Module </Location> And later: my $tree = Apache2::Directive::conftree(); my $node = $tree->lookup('Location', '/test/'); my $hash = $node->as_hash; $hash now is: { 'SetHandler' => 'perl-script', 'PerlHandler' => 'Test::Module', } "as_string" Get a string representation of the configuration node, in httpd.conf format. $string = $node->as_string(); obj: $node ( "Apache2::Directive object" ) The config tree to stringify ret: $string ( string ) since: 2.0.00 For example: in httpd.conf: <Location /test> SetHandler perl-script PerlHandler Test::Module </Location> And later: my $tree = Apache2::Directive::conftree(); my $node = $tree->lookup('Location', '/test/'); my $string = $node->as_string; $string is now: SetHandler perl-script PerlHandler Test::Module "conftree" Get the root of the configuration tree: $conftree = Apache2::Directive::conftree(); obj: "Apache2::Directive" ( class name ) ret: $conftree ( "Apache2::Directive object" ) since: 2.0.00 "directive" Get the name of the directive in $node: $name = $node->directive(); obj: $node ( "Apache2::Directive object" ) ret: $name ( string ) since: 2.0.00 "filename" Get the filename the configuration node was created from: $filename = $node->filename(); obj: $node ( "Apache2::Directive object" ) ret: $filename ( string ) since: 2.0.00 For example: my $tree = Apache2::Directive::conftree(); my $node = $tree->lookup('VirtualHost', 'example.com'); my $filename = $node->filename; $filename is now the full path to the httpd.conf that VirtualHost was defined in. If the directive was added with "add_config()", the filename will be the path to the httpd.conf that trigerred that Perl code. "first_child" Get the first child node of this directive: $child_node = $node->first_child; obj: $node ( "Apache2::Directive object" ) ret: $child_node ( "Apache2::Directive object" ) Returns the first child node of $node, "undef" if there is none since: 2.0.00 "line_num" Get the line number in a filename this node was created at: $lineno = $node->line_num(); obj: $node ( "Apache2::Directive object" ) arg1: $lineno (integer) since: 2.0.00 "lookup" Get the node(s) matching a certain value. $node = $conftree->lookup($directive, $args); @nodes = $conftree->lookup($directive, $args); obj: $conftree ( "Apache2::Directive object" ) The config tree to stringify arg1: $directive ( string ) The name of the directive to search for opt arg2: "args" ( string ) Optional args to the directive to filter for ret: $string ( string / ARRAY of HASH refs ) In LIST context, it returns all matching nodes. In SCALAR context, it returns only the first matching node. If called with only $directive value, this method returns all nodes from that directive. For example: @Alias = $conftree->lookup('Alias'); returns all nodes for "Alias" directives. If called with an extra $args argument, it returns only nodes where both the directive and the args matched. For example: $VHost = $tree->lookup('VirtualHost', '_default_:8000'); since: 2.0.00 "next" Get the next directive node in the tree: $next_node = $node->next(); obj: $node ( "Apache2::Directive object" ) ret: $next_node ( "Apache2::Directive object" ) Returns the next sibling of $node, "undef" if there is none since: 2.0.00 "parent" Get the parent node of this directive: $parent_node = $node->parent(); obj: $node ( "Apache2::Directive object" ) ret: "parent_node" ( "Apache2::Directive object" ) Returns the parent of $node, "undef" if this node is the root node since: 2.0.00 See Also mod_perl 2.0 documentation. Copyright mod_perl 2.0 and its core modules are copyrighted under The Apache Software License, Version 2.0. Authors The mod_perl development team and numerous contributors. perl v5.14.2 2011-02-08 libapache2-mod-perl2-2.0.7::docs::api::Apache2::Directive(3pm)
All times are GMT -4. The time now is 05:45 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy