Sponsored Content
Top Forums Shell Programming and Scripting sed newbie scripting assistance Post 302686019 by agama on Monday 13th of August 2012 09:55:01 PM
Old 08-13-2012
It's not trying to load the whole file in memory. It caches one copy of the first bits of a new line (things up to "fruit") and the list of 'items' that follow. When the first bits change, the record, with the summary of items, is written and the caching/list starts anew (list = ""). So, the only significant amount of "stuff" that is ever held in memory is the list of items.


Now, if that list is huge, then the programme could be amended to write them out as it finds them. My assumption was that the list wasn't going to be more than 1 or 2 K.

---------- Post updated at 21:55 ---------- Previous update was at 21:42 ----------

Turns out that printing the list as we go is a simpler programme; just didn't see it that way the other night.


Code:
awk '
    {
        x = $0;
        sub( "fruit.*", "", x );
        gsub( ".*fruit ", "", $0 );
         if( x != last )       # if first bits are different, print newline (if needed) and the current line
            printf( "%s%sfruit %s", NR > 1 ? "\n" : "", x, $0 );
         else         # first bits are the same, print just what is after fruit
            printf( "%s%s", NR > 1 ? ", " : "", $0 );
        last = x;
    }
     END { printf( "\n" );  }     # must have final newline
' input-file

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Scripting Newbie

Seems simple but I am having difficulty with this one: I am trying to write a single command line argument (which will be a path) - the program should print out the owner of the path. I can not get anything I write to run. Please help. (5 Replies)
Discussion started by: Kymmers7
5 Replies

2. Shell Programming and Scripting

scripting newbie... some help please?

hi all, i am just getting in to bash scripting, so don't be too harsh... i've created this little backup script, and it's just awfull... ugly, doesn't work like I want it to, the works. anyways, i was hoping some of you might help me improve it and learn a little in the process. what i... (13 Replies)
Discussion started by: jmd9qs
13 Replies

3. Shell Programming and Scripting

Scripting neophyte needs file manipulation assistance

I need to write two shell scripts for an rsync backup solution. The first script will copy all backed up files into a folder named after the original folder, plus a date stamp (so e.g. if the original folder name is 'foo' and is backed up on the 10th of September, then the backup folder will be... (0 Replies)
Discussion started by: LambdaCalculus
0 Replies

4. Shell Programming and Scripting

Assistance in Perl scripting

PFA file "color.txt". Note : There is no newline character in the file. I have manually inserted the newline char to make it easy to understand. I am expecting out in the form as specified in second file "out.txt" I need a perl script to perform the task. Thanks in advance. (2 Replies)
Discussion started by: deo_kaustubh
2 Replies

5. Shell Programming and Scripting

Need assistance with sed

Hi All, I need your assistance, I would like to replace all lines beginning with the word "begin" with the below text: Device | IPMB0-A | IPMB0-B Board Address |Sent SentErr %Errr |Sent SentErr ... (9 Replies)
Discussion started by: Dendany83
9 Replies

6. UNIX for Advanced & Expert Users

Need assistance with sed

Hi All, I need your assistance, I would like to replace all lines beginning with the word "begin" with the below text: Device | IPMB0-A | IPMB0-B Board Address |Sent SentErr %Errr |Sent SentErr ... (10 Replies)
Discussion started by: Dendany83
10 Replies

7. Shell Programming and Scripting

Using ii for irc chat - scripting assistance?

I am using ii for irc on my pogoplug... hxxp://hg.suckless.org/ii/file/d163c8917af7/FAQ If you look at the bottom of there, it states 31 What other fancy stuff can I do with ii? 32 ---------------------------------------- 33 It is very easy to write irc bots in ii: 34... (3 Replies)
Discussion started by: spartan2006
3 Replies

8. Shell Programming and Scripting

Dhcp.config file scripting assistance

Hello everyone! I am brand new at this forum thing and wanted to thank all of you for your time and help in advance for helping me troubleshoot my issue. I am fairly new to shell scripting and scoured the entire internet to find a solution for my issue to no avail and hope you're able to help. ... (2 Replies)
Discussion started by: sedrocks
2 Replies

9. Shell Programming and Scripting

Noob to scripting needs some assistance

Hello, I am in a Unix class and have been out of town. I have been tasked to generate a couple of scripts and ahve never done it before. I have a virtual machine running Ubuntu. The task is below Prompt the system administrator for all valid input parameters Generate a menu to ask which... (1 Reply)
Discussion started by: jkeeton81
1 Replies

10. Shell Programming and Scripting

sed assistance

Hello everyone. I am trying to replace sprintf(buffer, "{\"id\":1,\"method\":\"mining.update_block\",\"params\":}\n", coinid, blockhash); with sprintf(buffer, "{\"id\":1,\"method\":\"mining.update_block\",\"params\":}\n", coinid, blockhash); this is the code I was trying but is... (9 Replies)
Discussion started by: crombiecrunch
9 Replies
EACH(3) 								 1								   EACH(3)

each - Return the current key and value pair from an array and advance the array cursor

SYNOPSIS
array each (array &$array) DESCRIPTION
Return the current key and value pair from an array and advance the array cursor. After each(3) has executed, the array cursor will be left on the next element of the array, or past the last element if it hits the end of the array. You have to use reset(3) if you want to traverse the array again using each. PARAMETERS
o $array - The input array. RETURN VALUES
Returns the current key and value pair from the array $array. This pair is returned in a four-element array, with the keys 0, 1, key, and value. Elements 0 and key contain the key name of the array element, and 1 and value contain the data. If the internal pointer for the array points past the end of the array contents, each(3) returns FALSE. EXAMPLES
Example #1 each(3) examples <?php $foo = array("bob", "fred", "jussi", "jouni", "egon", "marliese"); $bar = each($foo); print_r($bar); ?> $bar now contains the following key/value pairs: Array ( [1] => bob [value] => bob [0] => 0 [key] => 0 ) <?php $foo = array("Robert" => "Bob", "Seppo" => "Sepi"); $bar = each($foo); print_r($bar); ?> $bar now contains the following key/value pairs: Array ( [1] => Bob [value] => Bob [0] => Robert [key] => Robert ) each(3) is typically used in conjunction with list(3) to traverse an array, here's an example: Example #2 Traversing an array with each(3) <?php $fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry'); reset($fruit); while (list($key, $val) = each($fruit)) { echo "$key => $val "; } ?> The above example will output: a => apple b => banana c => cranberry Caution Because assigning an array to another variable resets the original array's pointer, our example above would cause an endless loop had we assigned $fruit to another variable inside the loop. Warning each(3) will also accept objects, but may return unexpected results. It's therefore not recommended to iterate though object proper- ties with each(3). SEE ALSO
key(3), list(3), current(3), reset(3), next(3), prev(3), foreach, Object Iteration. PHP Documentation Group EACH(3)
All times are GMT -4. The time now is 09:09 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy