Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Delete & Pad spaces (2 places) in single line help.
Right off the bat; fix one issue here @ work & they think I move
mountains =P
Secondly.  Windows based using GAWK or AWK95.EXE.

Need:

Search a file for spaces....delete ONE from 1st instance in line, pad
2 spaces to 2nd instance of SAME line

EG:

"5290Companies  Name            12345678920070814RTRN001000000000
0000                       "

should be

"5290Companies  Name           12345678920070814RTRN001000000000
0000                       "

del. 1
^                                                        add 2 ^


Report this thread to moderator Post Follow-up to this message
Old Post
i_robot73@hotmail.com
08-15-07 11:58 PM


Re: Delete & Pad spaces (2 places) in single line help.
On Wed, 15 Aug 2007 08:02:27 -0700, i_robot73 wrote:

> Right off the bat; fix one issue here @ work & they think I move
> mountains =P
> Secondly.  Windows based using GAWK or AWK95.EXE.
>
> Need:
>
> Search a file for spaces....delete ONE from 1st instance in line, pad
> 2 spaces to 2nd instance of SAME line
>
> EG:
>
> "5290Companies  Name            12345678920070814RTRN001000000000
> 0000                       "
>
> should be
>
> "5290Companies  Name           12345678920070814RTRN001000000000
> 0000                       "
>
>                                del. 1
> ^                                                        add 2 ^


It appears that you want to delete the last space before the last field
and add two at the end of the line

sub( " " $(NF), $NF )
print $0 "  "

should do that in gawk (it does in my test).

--
T.E.D. (tdavis@umr.edu)



Report this thread to moderator Post Follow-up to this message
Old Post
Ted Davis
08-15-07 11:58 PM


Re: Delete & Pad spaces (2 places) in single line help.
Sorry, damn line wraps & crap....

In the same record:
1st space-padding, delete a space before the '123456789<date>'
2nd space-padding, add two spaces

I was able to do it with 2 runs through AWK using a temporary file,
but want to see if I can't cut it down to something 'smaller' code-
wise.

current code:

AWK "{gsub(\"U            2\",\"U           2\");print}"
orig.file>temp.file
AWK "{gsub(\"N001000000000   0\",\"N001000000000     0\");print}"
temp.file>final.file



On Aug 15, 4:46 pm, Ted Davis <tda...@umr.edu> wrote:
> On Wed, 15 Aug 2007 08:02:27 -0700, i_robot73 wrote: 
> 
> 
> 
> 
> 
> 
> 
>
> It appears that you want to delete the last space before the last field
> and add two at the end of the line
>
>   sub( " " $(NF), $NF )
>   print $0 "  "
>
> should do that in gawk (it does in my test).
>
> --
> T.E.D. (tda...@umr.edu)



Report this thread to moderator Post Follow-up to this message
Old Post
i_robot73@hotmail.com
08-16-07 11:57 PM


Re: Delete & Pad spaces (2 places) in single line help.
I was able to do it in one line...but prob. not the BEST way (one big
line, so watch the wraps)

AWK "{sub(\"U            2\",\"U           2\");print}" ".\<input>" |
AWK "{sub(\"RTRN001000000000   0000\",\"RTRN001000000000
0000\");print}">".\<output>"



How about padding lines to a certain length??


Report this thread to moderator Post Follow-up to this message
Old Post
i_robot73@hotmail.com
08-16-07 11:57 PM


Re: Delete & Pad spaces (2 places) in single line help.
i_robot73@hotmail.com wrote:

[ please don't top-post, fixed below ]


>
> On Aug 15, 4:46 pm, Ted Davis <tda...@umr.edu> wrote:
> 
>
>
>
> Sorry, damn line wraps & crap....
>
> In the same record:
>      1st space-padding, delete a space before the '123456789<date>'
>      2nd space-padding, add two spaces
>
> I was able to do it with 2 runs through AWK using a temporary file,
> but want to see if I can't cut it down to something 'smaller' code-
> wise.
>
> current code:
>
> AWK "{gsub(\"U            2\",\"U           2\");print}"
> orig.file>temp.file
> AWK "{gsub(\"N001000000000   0\",\"N001000000000     0\");print}"
> temp.file>final.file
>
>

If I understand you correctly, all you need with gawk is:

{print gensub(/ (123456789[^ ]*)/,"\\1  ","")}

e.g. (UNIX syntax):

$ echo "a  12345678920070814RTXXX  c"
a  12345678920070814RTXXX  c
$ echo "a  12345678920070814RTXXX  c" |
gawk '{print gensub(/ (123456789[^ ]*)/,"\\1  ","")}'
a 12345678920070814RTXXX    c

If you don't gave gawk, just use 2 subs():

$ echo "a  12345678920070814RTXXX  c" |
awk -v p="123456789" '{sub(" "p,p);sub(p"[^ ]*","&  ");print}'
a 12345678920070814RTXXX    c

Regards,

Ed.



Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
08-16-07 11:58 PM


Re: Delete & Pad spaces (2 places) in single line help.
i_robot73@hotmail.com wrote:

> I was able to do it in one line...but prob. not the BEST way (one big
> line, so watch the wraps)
>
> AWK "{sub(\"U            2\",\"U           2\");print}" ".\<input>" |
> AWK "{sub(\"RTRN001000000000   0000\",\"RTRN001000000000
> 0000\");print}">".\<output>"
>
>
>
> How about padding lines to a certain length??
>

Show some sample input and expected output if you'd like advice. It's
bnot clear what you're asking above. Also when replying to a netnews
posting like this (NOT a web forum), you need to include enough context
that your current posting stands alone so the rest of us don't have to
go digging back through the previous postings to figure out what this
one is talking about.

Ed.

Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
08-16-07 11:58 PM


Re: Delete & Pad spaces (2 places) in single line help.
On Thu, 16 Aug 2007 10:59:43 -0500, Ed Morton wrote:

> Show some sample input and expected output if you'd like advice. It's
> bnot clear what you're asking above. Also when replying to a netnews
> posting like this (NOT a web forum), you need to include enough context
> that your current posting stands alone so the rest of us don't have to
> go digging back through the previous postings to figure out what this
> one is talking about.

Especially when we are using Pan and the other message is not even on this
machine, but on one many miles away (Agent syncs correctly with a newsrc,
but Pan doesn't download messages that have been read on the other machine
but don't exist on the current one).

--
T.E.D. (tdavis@umr.edu)



Report this thread to moderator Post Follow-up to this message
Old Post
Ted Davis
08-16-07 11:58 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 11:27 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.