Help with txt formatting using AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with txt formatting using AWK
# 8  
Old 08-04-2009
Pls, go through this , as i am sure you will understand the usage.


Code:
http://www.grymoire.com/Unix/Awk.html

# 9  
Old 08-05-2009
basically, i prefer to use Perl for this kind of issue. As perl has hash.

Code:
while(<DATA>){
	my @tmp=split;
	push @{$hash{$tmp[0]}->{$tmp[1]}}, $tmp[2]."-".$tmp[3];
}
foreach my $key(keys %hash){
	print $key,"/";
	foreach my $k(keys %{$hash{$key}}){
		print $k,":", join ",", @{$hash{$key}->{$k}};
		print "/";
	}
	print "\n\n";
}
__DATA__
L73-10 342 0 1480
L73-10 342 100 1480
L73-10 342 250 1656
L73-10 342 500 1746
L73-10 342 750 1910
L73-10 350 0 1480
L73-10 350 100 1480
L73-10 350 250 1656
L73-11 300 0 1480
L73-11 300 100 1480
L73-11 300 250 1656
L73-11 300 500 1746

# 10  
Old 08-06-2009
Cheers Panyam,
I have used the linked txt previously. Although it seems pretty comprehensive I have found it quite hard to learn from. Perhaps it just me, with no previous knowledge of scripts and little computer aptitude, but I find it much easier to learn from an example. Although the link gives examples I find them hard to follow. I think I can "read" your solution but I am confused. Am I right in thinking that if the record number is zero for column 1 or 2 then
Code:
{ a[$1]=a[$1]","$3"-"$4;t=$1;s=$2 }

applies or else
Code:
{ a[$1]=a[$1]"/"$2":"$3"-"$4;t=$1
;s=$2 }

This would not give the output it does though. Have I misunderstood the record number condition?

Thanks again!
# 11  
Old 08-07-2009
Quote:
Originally Posted by eknryan
Cheers Panyam,
I have used the linked txt previously. Although it seems pretty comprehensive I have found it quite hard to learn from. Perhaps it just me, with no previous knowledge of scripts and little computer aptitude, but I find it much easier to learn from an example. Although the link gives examples I find them hard to follow. I think I can "read" your solution but I am confused. Am I right in thinking that if the record number is zero for column 1 or 2 then
Code:
{ a[$1]=a[$1]","$3"-"$4;t=$1;s=$2 }

applies or else
Code:
{ a[$1]=a[$1]"/"$2":"$3"-"$4;t=$1
;s=$2 }

This would not give the output it does though. Have I misunderstood the record number condition?

Thanks again!
I was checking first for current header is equal to previous header or not , then the else part is if not ( i mean a L73-10 / L73-11 )

a[$1]=a[$1]","$3"-"$4;t=$1;s=$2 is ..nothing but maintaining an array of elements for a header with same second element value.

in the else part
i am appending the elements if the second element is not matched ( of course for the same header).

I am bit poor in explaining the things Smilie
# 12  
Old 08-10-2009
Thanks Panyam. I think I get it but Ive tried to modify the script to add a further condition but seem to be getting nowhere. I am trying to output as previously however within each series of $1 and $2 values if
Code:
NR=i ; $4=v and NR=i+1 ; $4<v

then I do not want to print anything where NR>=i.

So if input was
Code:
L73-10 342 0 1480
L73-10 342 100 1480
L73-10 342 250 1656
L73-10 342 500 1500
L73-10 342 750 1910
L73-10 350 0 1480
L73-10 350 100 1480
L73-10 350 250 1656
L73-11 300 0 1480
L73-11 300 100 1480
L73-11 300 250 1656
L73-11 300 500 1546


Only a formatted version of the following would be output

Code:
L73-10 342 0 1480
L73-10 342 100 1480
L73-10 342 250 1656
L73-10 350 0 1480
L73-10 350 100 1480
L73-10 350 250 1656
L73-11 300 0 1480
L73-11 300 100 1480
L73-11 300 250 1656

Can anybody help me out?

Thanks again
Ryan

---------- Post updated at 10:38 AM ---------- Previous update was at 06:28 AM ----------

I know this wont exclude all lines beyond a reversed $4 value but can anyone tell me what is wrong with this?

Code:
BEGIN { s=0;t=0;NR=i;vel=$4 } NR==FNR { if($2==s&&$1==t) { a[$1]=a[$1]","$3"-"$4;t=$1;s=$2 } else { a[$1]=a[$1]"/"$2":"$3"-"$4;t=$1;s=$2 }}
END {if(NR==i+1&&vel>$4) {for ( i in a ) { print i a[i]"/"} }}

Also, is there any way of adding standard $3 $4 values say $3=6000 and $4=4500 to the end of every unique $1$2 set?

I hope ive explained this reasonably. Im a geophysicist with no experience of programming. I seem to spend hours trying variation after variation of code and have been trawing unix.com and google but seem to get lost all too often. Any help really would be appreciated!
Thanks
Ryan

Last edited by eknryan; 08-10-2009 at 10:37 AM.. Reason: Poorly explained problem
# 13  
Old 08-11-2009
Further modifying my code a bit :
Code:
awk ' BEGIN { s=0;t=0;v=0;flag1=0;} NR==FNR { if($2==s&&$1==t) { if(v > $4) { flag1=1 } if (flag1 != 1) { v=$4;a[$1]=
a[$1]","$3"-"$4;t=$1;s=$2 } } else { a[$1]=a[$1]"/"$2":"$3"-"$4;t=$1
;s=$2;flag1=0;v=$4 } }
END { for ( i in a ) { print i a[i]"/"} }'  file_name.txt

Gives the solution.
# 14  
Old 08-11-2009
Thanks very much Panyam!! Ive further modified your code to add dummy values for $3 and $4. I can do something myself!!
Cheers
Ryan
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk, sed, shell all words in INPUT.txt find in column1 of TABLE.txt and replce with column2 in

Hi dears i have text file like this: INPUT.txt 001_1_173 j nuh ]az 001_1_174 j ]esma. nuh ]/.xori . . . and have another text like this TABLE.txt j j nuh word1... (6 Replies)
Discussion started by: alii
6 Replies

2. Shell Programming and Scripting

Desired output.txt for reading txt file using awk?

Dear all, I have a huge txt file (DATA.txt) with the following content . From this txt file, I want the following output using some shell script. Any help is greatly appreciated. Greetings, emily DATA.txt (snippet of the huge text file) 407202849... (2 Replies)
Discussion started by: emily
2 Replies

3. UNIX for Dummies Questions & Answers

Removing PATTERN from txt without removing lines and general text formatting

Hi Everybody! First post! Totally noobie. I'm using the terminal to read a poorly formatted book. The text file contains, in the middle of paragraphs, hyphenation to split words that are supposed to be on multiple pages. It looks ve -- ry much like this. I was hoping to use grep -v " -- "... (5 Replies)
Discussion started by: AxeHandle
5 Replies

4. UNIX for Dummies Questions & Answers

awk formatting

Hi all, I'm writing a simple awk code: awk 'BEGIN {FS="|"};{print "Type\tNumber\ttypes\tTotal";};{print $1, "\t", $2, "\t", $3, "\t", $4, "\t";}' db_query.txt it gives me the result: Type Number types Total XXX 498.0 5100.0 5274.661 Type Number types Total... (7 Replies)
Discussion started by: messi777
7 Replies

5. Shell Programming and Scripting

awk append fileA.txt to growing file B.txt

This is appending a column. My question is fairly simple. I have a program generating data in a form like so: 1 20 2 22 3 23 4 12 5 43 For ever iteration I'm generating this data. I have the basic idea with cut -f 2 fileA.txt | paste -d >> FileB.txt ???? I want FileB.txt to grow, and... (4 Replies)
Discussion started by: theawknewbie
4 Replies

6. Shell Programming and Scripting

formatting awk

when i try this awk its giving out put as below. awk '!(/^$/||/--/||/selected/||/^ *$/){print $1}' tmp.txt output ===== 1 2010-08-03-12.31.26.126000 how excluede the 1st line ? i mean i want output only 2nd line i.e 2010-08-03-12.31.26.126000; (5 Replies)
Discussion started by: rocking77
5 Replies

7. Shell Programming and Scripting

AWK formatting help.

Dear all I require help with AWK regarding this situation Input is : fn1 12345 fn1 23456 fn3 231513 fn1 22325 fn3 123125 Desired output is fn1 12345 23456 22325 fn3 231513 123125 (5 Replies)
Discussion started by: Peasant
5 Replies

8. Shell Programming and Scripting

AWK CSV to TXT format, TXT file not in a correct column format

HI guys, I have created a script to read 1 column in a csv file and then place it in text file. However, when i checked out the text file, it is not in a column format... Example: CSV file contains name,age aa,11 bb,22 cc,33 After using awk to get first column TXT file... (1 Reply)
Discussion started by: mdap
1 Replies

9. UNIX for Dummies Questions & Answers

echo "ABC" > file1.txt file2.txt file3.txt

Hi Guru's, I need to create 3 files with the contents "ABC" using single command. Iam using: echo "ABC" > file1.txt file2.txt file3.txt the above command is not working. pls help me... With Regards / Ganapati (4 Replies)
Discussion started by: ganapati
4 Replies

10. Shell Programming and Scripting

Formatting using awk

Let's say I write a simple script that contains the following: date | awk '{print $1}' date | awk '{print $2}' Of course, when I run the script the output will look similar to: Tue Mar What if I want my ouput to be on one line as follows: Tue Mar What changes would I need to... (2 Replies)
Discussion started by: cdunavent
2 Replies
Login or Register to Ask a Question