Looping within the elements of a file using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Looping within the elements of a file using awk
# 1  
Old 12-26-2011
Looping within the elements of a file using awk

Hi all, I have a file containing 5000 rows and 4 columns. I need to do a loop within the rows based on the values of column 3. my sample data is formatted like the ones below: what i need to do is to make a loop that will allow me to plot the values of x,y,values corresponding to month 1 to month n. in concept, I want a pseudo-script like this:
Code:
for line in file.xyz; do
    for i=1; i<=12; i++
    awk syntax to find rows corresponding to the same month
    plot rows in file.xyz $3==i; 
done

Code:
x y month value
123.2 12.0 1 2.3
123.2 12.1 1 2.4
123.3 12.1 1 2.0
123.4 12.2 2 1.8
123.5 12.3 2 1.8
124.3 12.4 n 1.3

bunch of thanks as always.
# 2  
Old 12-26-2011
Please elaborate. What do you mean by "plot the values of x,y,values"? Do you want the x,y,values of each month in a separate file?
# 3  
Old 12-26-2011
yes it is, storing values of x,y,values in individual files. thanks
# 4  
Old 12-26-2011
Code:
perl -ane 'open O,">>month_$F[2].txt";print O "$F[0] $F[1] $F[3]\n;close O' inputfile.txt

---------- Post updated at 14:47 ---------- Previous update was at 14:31 ----------

bash code:
  1. #! /bin/bash
  2. while read line
  3. do
  4.     mt=`echo $line | awk '{print $3}'`
  5.     &#91; ! -e month_$mt.txt ] && echo "x y values" >> month_$mt.txt
  6.     echo $line | awk '{print $1, $2, $4}' >> month_$mt.txt
  7. done < input

Last edited by balajesuri; 12-26-2011 at 05:23 AM..
This User Gave Thanks to balajesuri For This Post:
# 5  
Old 12-26-2011
thanks much. i havent tried running a perl script how do i invoke a perl script?thanks, the bash code worked perfectly,Smilie
# 6  
Old 12-26-2011
@ida1215: Copy-paste that perl one-liner on your command line. It should work, unless you don't have perl installed. It'll give the same output as bash code, sans the first header line "x y values".
This User Gave Thanks to balajesuri For This Post:
# 7  
Old 12-27-2011
thanks much.

---------- Post updated 12-27-11 at 01:30 PM ---------- Previous update was 12-26-11 at 08:53 PM ----------

hi there again,

I'm back with my questions. Its regarding my post yesterday, so instead of one single file for all the months, i have now individual yearly files containing the monthly list. how will i do the loop for this? thanks much. i was trying to manipulate this code to suit my need. what i want is to append the year to each month, output will be like the ones below. Also there are months that has no data, but i still want a code that will print an empty file even for "null" months so that every year i will have a constant 12 files. is that possible. many many thanks.

Code:
1999_1.xyz 2000_1.xyz
1999_2.xyz 2000_2.xyz
to ..      to ..
1999_12.xyz 2000_12.xyz and so on..

Code:
loc="$(ls *.xyt)"
year=( 1999 2000 2001 2002 2003 2004 2005 2006 2007 )

for j in $loc; do
      while read line; do
      mt=`echo $line | awk '{print $3}'`
      [ ! -e $year_$mt.xyz ] >> $year_$mt.xyz
      echo $line | awk '{print $1, $2, $4}' >> _$mt.xyz 
 done < $j
 done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk: count unique elements in a field and sum their occurence across the entire file

Hi, Sure it's an easy one, but it drives me insane. input ("|" separated): 1|A,B,C,A 2|A,D,D 3|A,B,B I would like to count the occurence of each capital letters in $2 across the entire file, knowing that duplicates in each record count as 1. I am trying to get this output... (5 Replies)
Discussion started by: beca123456
5 Replies

2. Shell Programming and Scripting

Compare multiple arrays elements using awk

I need your help to discover missing elements for each box. In theory each box should have 4 items: ITEM01, ITEM02, ITEM08, and ITEM10. Some boxes either have a missing item (BOX02 ITEM08) or might have da duplicate item (BOX03 ITEM02) and missing another one (BOX03 ITEM01). file01.txt ... (2 Replies)
Discussion started by: alex2005
2 Replies

3. Shell Programming and Scripting

Awk: Append new elements to an array

Hi all, I'm dealing with a bash script to merge the elements of a set of files and counting how many times each element is present. The last field is the file name. Sample files: head -5 *.tab==> 3J373_P15Ac1y2_01_LS.tab <== chr1 1956362 1956362 G A hom ... (7 Replies)
Discussion started by: lsantome
7 Replies

4. Shell Programming and Scripting

Awk:substitution of characters between two file elements

Hi, I have file1 like this: a 64 b 66 c 67and file2 like this: @1234 1123 aabbcc @5453 5543 ccbaI want to replace each letter of the third line in file2 with corresponding number in file1. So desired output is, @1234 1123 646466666767 @5453 5543 67676664I tried something like... (4 Replies)
Discussion started by: polsum
4 Replies

5. Shell Programming and Scripting

printing array elements inside AWK

i just want to dump my array and see if it contains the values i am expecting. It should print as follows, ignore=345fht ignore=rthfg56 . . . ignore=49568g Here is the code. Is this even possible to do? please help termReport.pl < $4 | dos2ux | head -2000 | awk ' BEGIN... (0 Replies)
Discussion started by: usustarr
0 Replies

6. Shell Programming and Scripting

AWK help: how to compare array elements against a variable

i have an array call ignore. it is set up ignore=34th56 ignore=re45ty ignore=rt45yu . . ignore=rthg34 n is a variable. I have another variable that i read from a different file. It is $2 and it is working the way i expect. array ignore read and print correct values. in the below if... (2 Replies)
Discussion started by: usustarr
2 Replies

7. Shell Programming and Scripting

awk - array elements as condition

Hi, can I use array elements ( all ) in conditional statements? the problem is ,the total number of elements is not known. e.g A is an array with elements - 1,2,3 now if i want to test if the 1 st field of input record is either 1,2 or 3, i can do something like this if ( $1 ~... (1 Reply)
Discussion started by: shellwell
1 Replies

8. Shell Programming and Scripting

How to extract elements using Awk

Hi, I have this typical extraction problem in AWK. I have 3 input files.. i) First one is somehow like an oracle of:- foo 12,23,24 bla 11,34 car 35 ii)Second file is basically detailing the score for each of the second field of first file. Besides, for the first column, it is the... (3 Replies)
Discussion started by: ahjiefreak
3 Replies

9. Shell Programming and Scripting

Accessing single elements of a awk array in END

How do I access one of the indices in array tst with the code below? tst=sprintf("%5.2f",Car / 12) When I scan thru the array with for ( i in tst ) { print i,tst } I get the output of: vec-7 144 But when I try this in the END print tst It looks like it's not set. What am... (6 Replies)
Discussion started by: timj123
6 Replies

10. Shell Programming and Scripting

How to transpose data elements in awk

Hi, I have an input data file :- Test4599,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,0,0,2,0,2,2,Rain Test90,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,0,0,1,0,1,1,0,Not Rain etc.... I wanted to transpose these data to:-... (2 Replies)
Discussion started by: ahjiefreak
2 Replies
Login or Register to Ask a Question