Sponsored Content
Full Discussion: help with file formatting
Top Forums Shell Programming and Scripting help with file formatting Post 302354468 by radoulov on Friday 18th of September 2009 09:09:50 AM
Old 09-18-2009
Franklin52's post is important. What version of awk using my code produces the output you mentioned?

Code:
% nawk --version
awk version 20070501
% nawk -F, 'END { print r }
NR > 1 && /[A-Z]/ {
  print r; r = ""
  }
{ r = r ? r $0 : $0 FS }
' infile        
AAA,pqr,jkl,mnop,abcd
BBB,abc,pqrs,xyz,uvw,efgh,uvw,rpk
CCC,123,456,789
% gawk --version |head -1
GNU Awk 3.1.7
% gawk -F, 'END { print r }
NR > 1 && /[A-Z]/ {
  print r; r = ""
  }
{ r = r ? r $0 : $0 FS }
' infile        
AAA,pqr,jkl,mnop,abcd
BBB,abc,pqrs,xyz,uvw,efgh,uvw,rpk
CCC,123,456,789

On Solaris:

Code:
$ nawk -F, 'END { print r }
> NR > 1 && /[A-Z]/ {
>   print r; r = ""
>   }
> { r = r ? r $0 : $0 FS }
> ' infile
AAA,pqr,jkl,mnop,abcd
BBB,abc,pqrs,xyz,uvw,efgh,uvw,rpk
CCC,123,456,789
$ /usr/xpg4/bin/awk -F, 'END { print r }
NR > 1 && /[A-Z]/ {
  print r; r = ""
  }
{ r = r ? r $0 : $0 FS }
' infile
AAA,pqr,jkl,mnop,abcd
BBB,abc,pqrs,xyz,uvw,efgh,uvw,rpk
CCC,123,456,789

Input file used:

Code:
AAA
pqr,jkl,mnop,abcd

BBB
abc,pqrs,xyz,uvw,
efgh,uvw,
rpk

CCC
123,456,789



---------- Post updated at 02:56 PM ---------- Previous update was at 02:53 PM ----------

Franklin52's code assumes an awk implementation that supports multi character record separator (RS). So it will work only with GNU awk or tawk (?) I suppose.

---------- Post updated at 02:59 PM ---------- Previous update was at 02:56 PM ----------

Franklin52 , could you please post the sample data used in your examples? Is it different from the OP example?

---------- Post updated at 03:09 PM ---------- Previous update was at 02:59 PM ----------

vgersh99's solutions are old nawk specific, because setting FS to an empty string has a different meaning in the other awk implementations (not sure about mawk and tawk, though).
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Formatting a file

Hi All, I have been trying to format a file using sed. I can't seem to get the syntax right. I want to append the next line delemited by a comma or a comma and double quotes. Here is an example of the file I'm tring to format: Before formatting: 00324 03A0312 BRI-u24 0000324 01 H-12... (4 Replies)
Discussion started by: cstovall
4 Replies

2. Shell Programming and Scripting

Help with formatting of file.

I have a file with following file format - DMCRH|||83000171|||14022008||0430|||8956612.23|J|||3571235|1378452|23468|6894|9234| DMCRH|||83000215|||15092007||0480|||121.33|J|||LineID003|RefNumSP003|RefNumMem003|0004|0003| What i need done is - 1. Cut the firt four digits of the date (eg 1402... (3 Replies)
Discussion started by: divz
3 Replies

3. UNIX for Dummies Questions & Answers

Formatting a file.

I want to format a file to limit record length = 100, in each line. Any idea how i can do this? (1 Reply)
Discussion started by: abhilasha
1 Replies

4. Shell Programming and Scripting

File Formatting

Hi, Need to delete all the records prior to pattern (INSERT/UPDATE/DELETE). If ' is available, then need to retain it. Input ====================== l_s := ' INSERT INTO TEST' l_P PD := ' UPDATE INTO TEST' l_D := ' DELETE INTO TEST' This is test Input ======================... (1 Reply)
Discussion started by: saurabhbaisakhi
1 Replies

5. Shell Programming and Scripting

File Formatting

Hi, Need to delete all the records prior to pattern (INSERT/UPDATE/DELETE). If ' is available, then need to retain it. Input ====================== l_s := ' INSERT INTO TEST' l_P PD := ' UPDATE INTO TEST' l_D := ' DELETE INTO TEST' This is test Output ======================... (1 Reply)
Discussion started by: saurabhbaisakhi
1 Replies

6. Shell Programming and Scripting

File formatting

I need to count the number of lines between two sets of pattern in a file and delete those lines from that file e.g From jyotiv@yahoo.com test test2 test3 test4 test5 test6 From Jyotiv@yahoo.com So count lines from test to test6 and delete it from the start of file till next From... (1 Reply)
Discussion started by: jyotiv
1 Replies

7. Shell Programming and Scripting

File formatting

Hi, I have a file which contains data in this format # User@Host: abc @ Id: 0000000 # Query_time: 0.000070 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0 SET timestamp=00000000; SELECT @@version, @@version_comment; # User@Host: abcd @ Id: 00000000 # Query_time: 0.000228 ... (6 Replies)
Discussion started by: arijitsaha
6 Replies

8. Shell Programming and Scripting

File Formatting

Hi, I have requirement to format the file.My input file tab(\t) saperated. File format is:- 93 WARNING Sat Mar 17 20:31:59 2012 Sequential_File_0,0: Missing record delimiter "\r\n", saw EOF instead 94 WARNING Sat Mar 17 20:31:59 2012 Sequential_File_0,0: Import... (4 Replies)
Discussion started by: prasson_ibm
4 Replies

9. Shell Programming and Scripting

Formatting file data to another file (control character related)

I have to write a program to read data from files and then format into another file. However, I face a strange problem related to control character that I can't understand and solve. The source file is compose of many lines with such format: T_NAME|P_NAME|P_CODE|DOCUMENT_PATH|REG_DATE ... (3 Replies)
Discussion started by: hk6279
3 Replies

10. UNIX for Dummies Questions & Answers

Formatting data in a raw file by using another mapping file

Hi All, i have a requirement where i need to format the input RAW file ( which is CSV) by using another mapping file(also CSV file). basically i am getting feed file with dynamic headers by using mapping file (in that target field is mapped with source filed) i have to convert the raw file into... (6 Replies)
Discussion started by: ravi4informatic
6 Replies
AnyEvent::ForkObject(3pm)				User Contributed Perl Documentation				 AnyEvent::ForkObject(3pm)

NAME
AnyEvent::ForkObject - Async access on objects. SYNOPSIS
use AnyEvent::ForkObject; use DBI; my $fo = new AnyEvent::ForkObject; $fo->do( module => 'DBI', method => 'connect', args => [ 'dbi:mysql...' ], cb => sub { my ($status, $dbh) = @_; $dbh->selectrow_array('SELECT ?', undef, 1 + 1, sub { my ($status, $result) = @_; print "$result "; # prints 2 }); } ); use AnyEvent::Tools qw(async_repeat); $dbh->prepare('SELECT * FROM tbl', sub { my ($status, $sth) = @_; $sth->execute(sub { my ($status, $rv) = @_; # fetch 30 rows async_repeat 30 => sub { my ($guard) = @_; $sth->fetchrow_hashref(sub { my ($status, $row) = @_; undef $guard; # do something with $row }); }; }); }); DESCRIPTION
There are a lot of modules that provide object interface. Using the module You can use them in async mode. METHODS
new Constructor. Creates an instance that contains fork jail. do Creates an object inside jail. It receives the following named arguments: require Do require inside jail. If the argument is exists, module, method and wantarray arguments will be ignored. module Module name. For example 'DBI'. method Constructor name. Default value is 'new'. wantarray Context for method. Default is 0 (SCALAR). cb Done callback. The first argument is a status: die The method has thrown exception. The next argument contains $@. fatal A fatal error was occured (for example fork jail was killed). ok Method has done. The following arguments contain all data that were returned by the method. If "method" returns blessed object, it will provide all its methods in modified form. Each method will receive one or two additional arguments: result callback A callback that will be called after method has done. wantarray Context flag for method. Default value is 0 (SCALAR). All objects provide additional method fo_attr to access their field. Example: # set attribute $dbh->fo_attr(RaiseError => 1, sub { my ($status, $attr) = @_; ... }); # get attribute $dbh->fo_attr('RaiseError', sub { my ($status, $attr) = @_; ... }); AUTHOR
Dmitry E. Oboukhov, <unera@debian.org> COPYRIGHT AND LICENSE
Copyright (C) 2011 by Dmitry E. Oboukhov This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.1 or, at your option, any later version of Perl 5 you may have available. VCS
The project is placed in my git repo: http://git.uvw.ru/?p=anyevent-forkobject;a=summary <http://git.uvw.ru/?p=anyevent- forkobject;a=summary> perl v5.12.4 2011-07-29 AnyEvent::ForkObject(3pm)
All times are GMT -4. The time now is 08:10 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy