The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Data manipulation/ formatting question ddurden7 UNIX for Dummies Questions & Answers 2 04-12-2009 07:42 PM
Data manipulation in perl chriss_58 Shell Programming and Scripting 1 06-13-2008 01:56 PM
data manipulation script grinder182533 UNIX for Dummies Questions & Answers 1 11-10-2006 02:48 PM
More log manipulation StevePace Shell Programming and Scripting 8 02-13-2006 03:20 AM
Help with log manipulation StevePace Shell Programming and Scripting 3 01-31-2006 08:28 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-24-2009
Cham Cham is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 6
Data manipulation with Awk

Hello guys,

I'm a new member here and I need some help with the Awk application. I'm using it through the Terminal app of OSX (I'm a Mac user).

I have a huge file with a large amount of data (rows of 3D cartesian coordinates). The data is typically like the following example (actually, the data is made of several thousands of "curves" defined by a list of points in 3D) :

Quote:
2.344 1.453 -1.345
1.234 3.134 3.123
2.566 1.345 2.344
-1.234 -2.333 -4.555
1.344 1.453 -1.345 # End of curve
3.234 4.134 6.123
2.566 4.345 3.344
-1.234 -2.333 -4.555
-7.344 1.453 -5.345 # End of curve
1.234 -3.134 3.123
6.566 5.345 8.344
-1.234 2.333 4.555 # End of curve
I need Awk to read this input file, and write out the number of lines for each "curve" (with some arbitrary symbol in front of it), followed by a list of line numbers. From the data above, Awk should output something like this :

Quote:
NumberOfLines 5
0 1 2 3 4

NumberOfLines 4
5 6 7 8

NumberOfLines 3
9 10 11
Take note that the first line is numbered as "0".
How can I do that ? Someone has an idea ?

The only data manipulation I know to do with Awk is like this example :

Quote:
cat path_to_input | awk '{ print $1 "\t" $2 "\t" $3 "\t"}' > path_to_output
Thanks, and sorry for my bad English.
  #2 (permalink)  
Old 08-24-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,301
Try this:

Code:
awk '
{s=n?s " " c++:c++; n++}
/# End/{print "NumberOfLines " n; print s "\n"; n=0; s =""}
' file
  #3 (permalink)  
Old 08-24-2009
Cham Cham is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 6
Quote:
Originally Posted by Franklin52 View Post
Try this:

Code:
awk '
{s=n?s " " c++:c++; n++}
/# End/{print "NumberOfLines " n; print s "\n"; n=0; s =""}
' file
OMG !! It worked like a charm !

THANK YOU SO MUCH !!!
  #4 (permalink)  
Old 08-24-2009
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,082
Code:
awk '/End of/{
if(flag==0){
  flag=1
  print "NumberOfLines"NR
  for(i=1;i<=NR;i++){
    printf i-1" "
  }
  pre=NR
}
else{
  num=NR-pre
  print "NumberOfLines"num
  for(i=pre+1;i<=NR;i++){
    printf i-1" "
  }
  pre=NR
}
print ""
}'
  #5 (permalink)  
Old 08-24-2009
protocomm protocomm is online now
Registered User
  
 

Join Date: Mar 2009
Location: france, montpellier
Posts: 150
Hi!!

Franklin 52, could you explain, your command line....i don't understand the part...

Code:
{s=n?s " " c++:c++;
Thanks by advance.
  #6 (permalink)  
Old 08-24-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,301
Quote:
Originally Posted by protocomm View Post
Hi!!

Franklin 52, could you explain, your command line....i don't understand the part...

Code:
{s=n?s " " c++:c++;
Thanks by advance.
I've used a conditional operator, the form is :

Code:
expr ? action1 : action2
If expr is true, return action1 else return action2

Code:
s=n?s " " c++:c++

Explanation:

if n != 0 then s = s " " c++ else s = c++
Regards

Last edited by Franklin52; 08-24-2009 at 03:21 PM.. Reason: Correction of explanation: if n != 0 instead of if n = 0
  #7 (permalink)  
Old 08-24-2009
protocomm protocomm is online now
Registered User
  
 

Join Date: Mar 2009
Location: france, montpellier
Posts: 150
Great, thanks...
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:08 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0