if, sed or awk with conditions | 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.

if, sed or awk with conditions

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-03-2010
ski ski is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 13 July 2010, 12:13 PM EDT
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
if, sed or awk with conditions

I do not know how to do this unless I use a bunch of if statements. I need a script to replace numbers in each record in a file. I am really getting tangled in this web.
If a fieldA (19 positions) is greater than 14 digits, I have to change the data (resulting fieldA is fixed 19 postions).
If there are preceding zeros. For example:
'000123456789012 ' would be changed to '123456789012 '
'001234567890123 ' would be changed to 1234567890123 '
'012345678901234 ' would be changed to 12345678901234 '
'123456789012345 ' would not be changed but would report error

I would appreciate any help or direction. Thanks.
Sponsored Links
    #2  
Old 03-03-2010
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 2 September 2014, 5:43 PM EDT
Location: The Netherlands
Posts: 7,709
Thanks: 121
Thanked 535 Times in 505 Posts
Something to start with:

Code:
awk 'length($0) > 14 {print "Error on this line : " $0; next}
{printf("%d\n", int($0)) > "OutFile"}' file

Sponsored Links
    #3  
Old 03-04-2010
dr.house dr.house is offline
Registered User
 
Join Date: Dec 2008
Last Activity: 30 October 2011, 12:05 PM EDT
Location: Switzerland
Posts: 454
Thanks: 4
Thanked 27 Times in 27 Posts

Code:
[house@discovery] echo "000123456789012" | sed 's/^0*//'
123456789012

    #4  
Old 03-04-2010
vivekraj's Avatar
vivekraj vivekraj is offline
Registered User
 
Join Date: Feb 2010
Last Activity: 9 July 2010, 8:42 AM EDT
Location: Chennai
Posts: 112
Thanks: 0
Thanked 0 Times in 0 Posts
Try the following script to do it.


Code:
sed -r 's/[1-9]+{15,}/Error on this line exceeding 14 digits/g;s/^0*//g;' <inputfilename>

Sponsored Links
    #5  
Old 03-04-2010
karthigayan's Avatar
karthigayan karthigayan is offline
Registered User
 
Join Date: Apr 2009
Last Activity: 9 July 2013, 6:35 AM EDT
Location: India , Chennai
Posts: 137
Thanks: 0
Thanked 3 Times in 3 Posts
Actually vivek raj has did that , but it has a small bug to check the 0 preceding the line.

It will not work for this input,

123456789012345

Actually for this input the error should come.


Code:
sed -r 's/^[1-9][0-9]+{14,}/Error on this line exceeding 14 digits/g;s/^0*//g'

Sponsored Links
    #6  
Old 03-04-2010
ski ski is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 13 July 2010, 12:13 PM EDT
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you everyone for the various means to do it. I will work with them all in testing just to become familiar with the different coding techniques. While I was waiting for someone to respond, I was researching and tested with the following method that is working but I need to incorporate the error message which I will do from above. Thanks again.


Code:
FieldB=`echo "${FieldA}" | sed "s/^0*//"`

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
About awk conditions majormark Shell Programming and Scripting 1 10-29-2009 07:14 AM
conditions singh is king Shell Programming and Scripting 1 10-16-2009 06:43 PM
multiple if conditions bashshadow1979 Shell Programming and Scripting 4 04-21-2009 03:08 PM
2 or more if conditions dr_sabz UNIX for Dummies Questions & Answers 3 01-15-2009 05:20 AM
test with two conditions (OR) chebarbudo Shell Programming and Scripting 3 11-29-2008 04:39 PM



All times are GMT -4. The time now is 07:43 PM.