Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

awk or sed help

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 08-18-2013
Indra2011 Indra2011 is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 25 July 2015, 1:37 PM EDT
Posts: 106
Thanks: 18
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 Unix and Linux 08-18-2013
Jotne's Unix or Linux Image
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 Unix and Linux 08-18-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 4 August 2015, 2:11 PM EDT
Location: Aachen, Germany
Posts: 6,417
Thanks: 112
Thanked 1,770 Times in 1,656 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 Unix and Linux 08-18-2013
Indra2011 Indra2011 is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 25 July 2015, 1:37 PM EDT
Posts: 106
Thanks: 18
Thanked 0 Times in 0 Posts
Thanks to both RudiC and Jotne for helping out!!!
Have a great day1!
Sponsored Links
    #5  
Old Unix and Linux 08-18-2013
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 4 August 2015, 9:51 PM EDT
Location: San Jose, CA, USA
Posts: 6,846
Thanks: 290
Thanked 2,281 Times in 1,951 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 10:21 PM.