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
loop through the directory for files and sort by date and process the first file dsdev_123 AIX 1 01-30-2008 05:31 PM
while loop inside while loop panknil Shell Programming and Scripting 0 01-07-2008 12:49 PM
sort -k Indalecio Shell Programming and Scripting 4 03-16-2007 04:00 AM
sort eck331 UNIX for Dummies Questions & Answers 2 06-04-2006 01:49 AM
how to get the similar function in while loop or for loop trynew Shell Programming and Scripting 3 06-17-2002 12:09 PM

Closed Thread
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 04-28-2008
bombcan bombcan is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 18
Need some sort of loop

Here is my dilemma.

I have a file that looks something like this

name: Type1 group: Group1
name: Type1 group: Group2
name: Type1 group: Group3
name: Type2 group: Group3
name: Type2 group: Group2
name: Type2 group: Group4
name: Type3 group: Group5
name: Type3 group: Group2
name: Type3 group: Group1
name: Type3 group: Group4

Now I need a loop to create another file that would look like this

data_2_1_1 = new Option("Group1","Group1");
data_2_1_2 = new Option("Group2","Group2");
data_2_1_3 = new Option("Group3","Group3");
data_2_2_1 = new Option("Group3","Group3");
data_2_2_2 = new Option("Group2","Group2");
data_2_2_3 = new Option("Group4","Group4");
data_2_3_1 = new Option("Group5","Group5");
data_2_3_2 = new Option("Group2","Group2");
data_2_3_3 = new Option("Group1","Group1");
data_2_3_4 = new Option("Group4","Group4");

Notice how the second and third digit increments
The second will increment when the type changes and the third one will
increment with as many GROUPS that the TYPE belongs to.

Thanks for all the help!
  #2 (permalink)  
Old 04-28-2008
bombcan bombcan is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 18
Forgot to mention that this is an example only and the actual name: and group: are different
  #3 (permalink)  
Old 04-28-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,879
Use nawk or /usr/xpg4/bin/awk on Solaris.

Code:
awk '{
printf "data_2_%d_%d = new Option(\"%s\",\"%s\");\n",
!__[$2]++?++c:c, ++_[$2], $NF, $NF
}' file

Last edited by radoulov; 04-28-2008 at 11:33 AM.. Reason: corrected(misread the question)
  #4 (permalink)  
Old 04-28-2008
bombcan bombcan is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 18
Nice stuff thanks a million
  #5 (permalink)  
Old 04-28-2008
bombcan bombcan is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 18
One more question to finish this off if I would want to
have the same file however with

data_2_1 = new Option("Type1","Type1");
data_2_2 = new Option("Type2","Type2");


and so on .....

Thanks again
  #6 (permalink)  
Old 04-28-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,879
Like this?

Code:
awk '!_[$2]++{
printf "data_2_%d = new Option(\"%s\",\"%s\");\n", ++c, $2, $2
}' file

Last edited by radoulov; 04-28-2008 at 12:20 PM.. Reason: corrected
Closed Thread

Bookmarks

Tags
solaris

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 01:51 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