help with awk with changing parameters: TY


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help with awk with changing parameters: TY
# 1  
Old 11-28-2011
help with awk with changing parameters: TY

hi, i got several pieces of code that look like:
Code:
for ff in `seq 3 $nlpN`; 
do     
    npc1[$ff]=`awk 'NR=='$ff' {print $1}' p_walls.raw`;  echo ${npc1[$ff]};
    npc2[$ff]=`awk 'NR=='$ff' {print $2}' p_walls.raw`;
    npc3[$ff]=`awk 'NR=='$ff' {print $3}' p_walls.raw`;
    npRs[$ff]=`awk 'NR=='$ff' {print $4}' p_walls.raw`; 
 echo $ff 
done

as u can see i'm invoking awk several times.. is there a faster way to do this? Smilie like invoking awk once and do the assignments with the changin parameters...

thanks a lot in advance!Smilie
Moderator's Comments:
Mod Comment Please use code tags!

Last edited by vbe; 11-28-2011 at 01:09 PM.. Reason: unecessary info
# 2  
Old 11-28-2011
When you mention `awk 'NR=='$ff' {print $1}' p_walls.raw | cut -f1 -d ' '` (assuming FS is space for awk), I don't think cut -f1 -d' ' will have any effect. Or am I wrong?

Also, please provide a sample input and desired output.
This User Gave Thanks to balajesuri For This Post:
# 3  
Old 11-28-2011
no it wont, sry, those cuts are indeed useless..i'm editing the question... but the point here is how to do a loop inside awk with the lines as parameters...

i see there are ways to loop inside awk... as for the input and output ..only the input matter atp, the input looks like:

3.76023 0.79528 0.307771 8729.82
3.76024 0.814664 0.307849 8650.2
3.76026 0.845679 0.307978 8802.97
3.76025 0.826293 0.307897 8690.43
3.76017 0.65959 0.30722 8936.07


but i cant find one for my case atm... im still digging thou Smilie

im looking for sth like
Code:
    awk ' { for ff in `seq 3 $nlpN`;  do
    /'$ff'/ { 
            npc2[$ff]=$2;
        npc3[$ff]=$3;
        npRs[$ff]=$4; 
          done}
        }' p_walls.raw;


Last edited by vbe; 11-28-2011 at 01:10 PM.. Reason: elaborating
# 4  
Old 11-28-2011
input.txt:
3.76023 0.79528 0.307771 8729.82
3.76024 0.814664 0.307849 8650.2
3.76026 0.845679 0.307978 8802.97
3.76025 0.826293 0.307897 8690.43
3.76017 0.65959 0.30722 8936.07

Code:
#! /usr/bin/perl -w
use strict;

my (@npc1, @npc2, @npc3, @npRs);
my ($ln, $index) = (0, 0);

open INPUT, "< input.txt";
for $ln ( <INPUT> ) {
    ($npc1[$index], $npc2[$index], $npc3[$index], $npRs[$index]) = split /\s+/, $ln;
    $index++;
}
close INPUT;

print "@npc1\n";
print "@npc2\n";
print "@npc3\n";
print "@npRs\n";

Output:
3.76023 3.76024 3.76026 3.76025 3.76017
0.79528 0.814664 0.845679 0.826293 0.65959
0.307771 0.307849 0.307978 0.307897 0.30722
8729.82 8650.2 8802.97 8690.43 8936.07
This User Gave Thanks to balajesuri For This Post:
# 5  
Old 11-28-2011
holy moses! that was quick!

thanks a lot, that's the sort of thing i'd usually do using matlab, but since i was using bash/sh i didnt have (or i havent found) this option.. the whole script has like several awk invokes and the estimated time to completion is 22 years! LOL, so i rly need to open files once... guess the perl is an option... i was hoping for an answer for bash/sh but it can move to perl

thanks for now..SmilieSmilieSmilie
# 6  
Old 11-28-2011
What are you actually trying to do? Transpose rows to columns?
# 7  
Old 11-28-2011
posting a larger part of the code...

#!/bin/bash
...
....
for ff in `seq 3 $nlpN`;
do
npc1[$ff]=`awk 'NR=='$ff' {print $1}' p_walls.raw| cut -f1 -d ' '`; echo ${npc1[$ff]};
npc2[$ff]=`awk 'NR=='$ff' {print $2}' p_walls.raw| cut -f1 -d ' '`;
npc3[$ff]=`awk 'NR=='$ff' {print $3}' p_walls.raw| cut -f1 -d ' '`;
npRs[$ff]=`awk 'NR=='$ff' {print $4}' p_walls.raw| cut -f1 -d ' '`;
echo $ff
done

...
...
...


you see... i got LOTS of awks.. and the input files..they got like 70k and 30k lines to be read... so i got 70k*30k passes..

the awks make this scrip run in 22years with a decent pc... so i figure if i could use sth like balaje pointed.. sth like

fopen
...
fclose;

using perl i guess ill need to work with 2 shells and i need all variables on the memory so speed thing up Smilie

so far using perl is the way... not too happy about moving.. and the script is not finished yet..got add more conditionals and debugg/cleanup..etc

---------- Post updated at 10:48 AM ---------- Previous update was at 08:46 AM ----------

i guess i may have found an answer in another forumSmilie:
Code:
for ff in `seq 3 $nlpN`
do
    data=`awk 'NR=='$ff' { print $1, $2, $3, $4; exit }' p_walls.raw`
    npc1[$ff]=`echo "$data" | cut -f1 -d ' '`
    echo ${npc1[$ff]}
    npc2[$ff]=`echo "$data"`
    npc3[$ff]=`echo "$data"`
    npRs[$ff]=`echo "$data"`
    echo $ff
done

any comments

ty again!Smilie

Last edited by vbe; 11-28-2011 at 01:10 PM.. Reason: removed parts of the code that were useless here
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pass Parameters to awk command

I need to pass values at runtime for the below awk command where l is the length and partial.txt is the file name. awk -v l=285 '{s="%-"l"s\n";printf(s,$0);}' partial.txt > temp1.txt; (5 Replies)
Discussion started by: Amrutha24
5 Replies

2. HP-UX

Changing Dynamic Tunable parameters in the kernel

To fix an "issue" we're having I need to update SHMMAX from 1GB to 2Gb, it's a dynamic parameter so am just wondering how flexible it really is. As Oracle is running on the server do I need to shut that down to change the parameter or will it just take the change on the fly? Also how would I... (6 Replies)
Discussion started by: Turlock
6 Replies

3. Shell Programming and Scripting

Search parameters on file with AWK

Hello everyone!! I need help with this please: I have a file with this content: 56977964333 730030201857822 1 2 4 56976969284 730030201412442 1 2... (3 Replies)
Discussion started by: bobbasystem
3 Replies

4. Shell Programming and Scripting

Help with explanation of awk parameters

Hello, Would someone be able to tell me exactly how this command works please? awk '!x++' As usual any help much appreciated (8 Replies)
Discussion started by: Grueben
8 Replies

5. Shell Programming and Scripting

passing parameters using awk

Hi, The below script is working fine awk1.sh ======= awk BEGIN { FS="|" } FNR==NR { f1=$2; next } $1 in f1 && $2 =="xx" && $1 == "DAILY_JOB" {print $3} awk -f awk1.sh a.txt b.txt--Its working fine . When passing parameters its not working .Any help it should be appereciated. ... (4 Replies)
Discussion started by: akil
4 Replies

6. Shell Programming and Scripting

awk parameters check

Is there a way to preform check if the parameters that was send with the command awk -f `file_name.awk` `input_file` before even it gets to the BEGIN section (i have tested a try to check in the BEGIN it doesn't let ,you must make it on the section that after the BEGIN) and then decide if the... (1 Reply)
Discussion started by: tal
1 Replies

7. Shell Programming and Scripting

AWK alias with parameters

Hello, How can I make an alias of an AWK one liner that prints specific lines from a file like below? # from a command like this: awk 'NR == 100, NR == 150' file.cfg The alias I want to make should work like this(I guess): <alias_command> <file.cfg><start_line><end_line> So the... (1 Reply)
Discussion started by: majormark
1 Replies

8. Shell Programming and Scripting

How to pass parameters to an awk file?

I have an awk file where I need to pass a filename and a value as a parameter from a sh script. I need to know how to pass those values in the sh script and how to use the same in the awk file. Thanks in advance!!! Geetha (3 Replies)
Discussion started by: iamgeethuj
3 Replies

9. Shell Programming and Scripting

awk - printing parameters

Hello I've got a script that takes 2 parameters. id and f. id is a user id and f is a file. I want to print this id on the first line and then the file. how can this be done? thanks (3 Replies)
Discussion started by: jacma
3 Replies

10. UNIX for Dummies Questions & Answers

Changing TCP parameters in solaris10

Hi I have to change some TCP settings in Solaris10 machine running on AMD. Is there any tool or GUI to do this. I want to see if there is any easy way to put them in the startup scripts. Thanks (1 Reply)
Discussion started by: sssow
1 Replies
Login or Register to Ask a Question