The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
replacing a nul field with text DarkHound Shell Programming and Scripting 3 09-04-2007 02:34 PM
Moving Part of a field to another field using AWK rjsha1 Shell Programming and Scripting 5 08-04-2006 05:39 AM
Replacing certain field charbel Shell Programming and Scripting 1 04-26-2006 04:00 PM
add increment field when first field changes azekry Shell Programming and Scripting 2 11-14-2005 04:21 PM
Replacing the last field of a line. Darek Shell Programming and Scripting 3 08-19-2005 11:45 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-08-2008
shennanigan83 shennanigan83 is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 3
need help with replacing a certain field...

Hi, can anyone help me? This is what i want to do....I have a string

UNB+UNOA:1+OOCLIES+RTTC+080408:0358+1'

and i want to replace the "1" at the end (that specific field only) to 00001 such that the new output will be like this

UNB+UNOA:1+OOCLIES+RTTC+080408:0358+00001'

i tried using sed but it replaces all the "1"'s it encounters..i only need the "1" in the last part....maybe it has something to do with cut -d+ -f6? I'm not entirely sure...

I already figured out how to pad 1 with zeros to make it's width 5 but am having trouble with the replacement part.

Please help anyone...and thank you very much in advance!!!
  #2 (permalink)  
Old 04-08-2008
rubin's Avatar
rubin rubin is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2007
Posts: 321
One quick way using awk:

Code:
echo 'UNB+UNOA:1+OOCLIES+RTTC+080408:0358+1' | awk '{sub(/1$/,"00001")}1'
Output:
Code:
UNB+UNOA:1+OOCLIES+RTTC+080408:0358+00001
  #3 (permalink)  
Old 04-08-2008
danmero danmero is online now Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,420
Quote:
Originally Posted by shennanigan83 View Post
i tried using sed but it replaces all the "1"'s it encounters..i only need the "1" in the last part....
sed only the last digit.
Code:
 echo 'UNB+UNOA:1+OOCLIES+RTTC+080408:0358+1' | sed 's/[0-9]$/0000&/'
  #4 (permalink)  
Old 04-08-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,509
Code:
# echo "UNB+UNOA:1+OOCLIES+RTTC+080408:0358+1" | awk -F"+" '{$NF="00001"}1' OFS="+"
UNB+UNOA:1+OOCLIES+RTTC+080408:0358+00001
  #5 (permalink)  
Old 04-09-2008
shennanigan83 shennanigan83 is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 3
Hi all, thank you very much for your replies...but can you please explain what happened? Because i can see that you coded it as only "1", but see, these can be any number from 1-99999. what happens is i need to pad the said number with zeros to make it have a width of 5.

My code is as follows:

currCtlNo=`echo "UNB+UNOA:1+OOCLIES+RTTC+080408:0358+1" | cut -d+ -f6 | cut -d"'" -f1`
newCtlNo=$currCtlNo

typeset -Z5 newCtlNo

then i want to replace the last portion with the $newCtlNo..only the last portion
  #6 (permalink)  
Old 04-09-2008
shennanigan83 shennanigan83 is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 3
Smile

i already got it =)

UNB_seg="UNB+UNOA:1+OOCLIES+RTTC+080408:0358+1'"
currCtlNo=`echo $UNB_seg | cut -d+ -f6 | cut -d"'" -f1`
newCtlNo=$currCtlNo

typeset -Z5 newCtlNo

echo $UNB_seg | awk -F"+" '{$NF="'$newCtlNo'"}1' OFS="+" > newfile.txt
cat newfile.txt

the result is:
UNB+UNOA:1+OOCLIES+RTTC+080408:0358+00001'

but can you please explain the awk statement? I would just like to better understand what happened.

Thank you in advance!

and thank you everyone!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:14 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0