awk or sed help | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

awk or sed help

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-18-2013
Indra2011 Indra2011 is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 4 June 2014, 5:39 AM EDT
Posts: 82
Thanks: 11
Thanked 0 Times in 0 Posts
awk or sed help

Hi Help,

I hope you guys are doing good!

I have an input file which looks like


Code:
11 12 13 16 17 |$678.09 78 p|
11 12 13 16 17 |     8.09278 p|
11 12 13 16 17 |3678             |

I want the op file to look like

Code:
$678.09078p0008.09278p36780000000

i.e only to print the characters within |&|(within the pipes) and to replace all the blank space within pipe by 0.The number of chars within th pipe are constant and here it is 11.

Is it possible.?

Thanks a lot in advance

Cheers!!
Moderator's Comments:
Having multiple adjacent spaces in your input and not using CODE tags makes it impossible for readers of this thread to understand what you are trying to do. You have been asked to use CODE tags multiple times and seem to think it is OK to ignore this request to make it possible for the volunteers reading your posts to clearly see what you are talking about.
If you continue to ignore the rules, you will likely be banned from posting more requests.

Last edited by Don Cragun; 08-18-2013 at 05:52 AM.. Reason: Added CODE tags.
Sponsored Links
    #2  
Old 08-18-2013
Jotne's Avatar
Jotne Jotne is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 20 September 2014, 2:08 AM EDT
Posts: 1,040
Thanks: 62
Thanked 216 Times in 204 Posts
Some like this?
One space "" is replaced by one 0
In your output you have multiple 0 , not sure why.


Code:
awk -F\| '{gsub(/ /,"0");print $2} END {print "\n"}' ORS= file
$678.090780p08.092780p36780

The Following User Says Thank You to Jotne For This Useful Post:
Indra2011 (08-18-2013)
Sponsored Links
    #3  
Old 08-18-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 30 September 2014, 7:36 AM EDT
Location: Aachen, Germany
Posts: 4,290
Thanks: 73
Thanked 1,035 Times in 980 Posts
Please use code tags as required by forum rules!

The number of chars within the pipe is not constant and it is not 11. Your output does not meet your requirements.

Try
Code:
awk -F\| '{gsub(/ /,"0",$2); printf "%s", $2} END {printf"\n"}' file
$678.090780p8.09278p3678

EDIT: Tried to interpret your desired output a bit more; still it doesn't fit what you require, but try:
Code:
awk -F\| '{$2=sprintf ("%-11s", $2); gsub(/ /,"0",$2); printf "%s", $2} END {printf"\n"}' file
$678.090780p08.092780p036780000000


Last edited by RudiC; 08-18-2013 at 06:57 AM.. Reason: typo
The Following User Says Thank You to RudiC For This Useful Post:
Indra2011 (08-18-2013)
    #4  
Old 08-18-2013
Indra2011 Indra2011 is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 4 June 2014, 5:39 AM EDT
Posts: 82
Thanks: 11
Thanked 0 Times in 0 Posts
Thanks to both RudiC and Jotne for helping out!!!
Have a great day1!
Sponsored Links
    #5  
Old 08-18-2013
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 30 September 2014, 8:27 AM EDT
Location: San Jose, CA, USA
Posts: 4,760
Thanks: 181
Thanked 1,588 Times in 1,352 Posts
Jotne and RudiC were confused because the original posting in this thread did not originally contain CODE tags. I have added CODE tags for you, but I agree with RudiC; the number of characters between the pipe symbols in your input lines is not a constant and is not 11 in any of your sample input lines. The number of characters between the pipe symbols is 12, 14, and 17 characters, respectively.

Replacing each space in your input with a "0" and adding a <newline> at the end produces the output:

Code:
$678.090780p000008.092780p36780000000000000

which is similar to, but certainly not the same as the output you said should be produced:

Code:
$678.09078p0008.09278p36780000000

The characters printed in red do not appear in what you say the output should be. I don't see a clear pattern to describe what spaces you want converted to zeros and what spaces you want to be deleted.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help with awk/sed Kishlay Shell Programming and Scripting 14 02-17-2012 05:43 AM
Using sed or awk? puttster Shell Programming and Scripting 13 10-07-2010 08:51 AM
Help with sed/awk quintet Shell Programming and Scripting 2 08-05-2008 11:59 AM
Awk or Sed help MrG-San UNIX for Advanced & Expert Users 7 04-02-2008 10:24 AM
sed in awk ? or nested awk ? varungupta UNIX for Advanced & Expert Users 11 02-08-2008 09:34 AM



All times are GMT -4. The time now is 09:50 AM.