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
# 8  
Old 12-27-2011
This is my understanding of your question: You have a directory with files each containing data about a year. For e.g., 1999.xyz contains data about all the months of year 1999. 2000.xyz contains data about all the months of year 2000 and so on. That is, 1999.xyz contains data similar to sample data posted in post #1, right?

bash code:
  1. #! /bin/bash
  2.  
  3. for x in 1999.xyz 2000.xyz 2001.xyz 2002.xyz 2003.xyz 2004.xyz 2005.xyz 2006.xyz 2007.xyz
  4. do
  5.     for mt in `seq 1 12`
  6.     do
  7.         [ ! -e `basename $x .xyz`_$mt.xyz ] && echo "x y values" >> `basename $x .xyz`_$mt.xyz
  8.         while read line
  9.         do
  10.             echo $line | grep -q "^x y month"
  11.             [ $? -eq 0 ] && continue
  12.             [ $mt -eq `echo $line | awk '{print $3}'` ] && echo $line | awk '{print $1, $2, $4}' >> `basename $x .xyz`_$mt.xyz
  13.         done < $x
  14.     done
  15. done
This User Gave Thanks to balajesuri For This Post:
# 9  
Old 12-27-2011
hi balajesuri, thanks a bunch for your help, helpful as always. i just have one glitch though. If I'm listing the file, I need to have it in sequential format, i.e. 1-12 and so I need to put zeros for months from 1-9, how to incorporate this in the code.
# 10  
Old 12-27-2011
Just include the line in blue:
Code:
for mt in `seq 1 12`
    do
        mt=`printf "%02d" $mt`
        [ ! -e `basename $x .xyz`_$mt.xyz ] && echo "x y values" >> `basename $x .xyz`_$mt.xyz

And to display the sorted list try this on your command line: ls -1 *.xyz | sort -t_ -n -k1 -k2
This User Gave Thanks to balajesuri For This Post:
# 11  
Old 12-27-2011
many thanks, worked perfectly,Smilie
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