need help with replacing a certain field...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need help with replacing a certain field...
# 1  
Old 04-08-2008
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  
Old 04-08-2008
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  
Old 04-08-2008
Quote:
Originally Posted by shennanigan83
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  
Old 04-08-2008
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  
Old 04-09-2008
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  
Old 04-09-2008
Bug

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? Smilie I would just like to better understand what happened. Smilie

Thank you in advance!

and thank you everyone! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing field based on the value of other field

Hi Team, In the below input file, if I have the value 23,24,25 then for those records 1st field value should get updated from "a" to "b". I also want to pass these values in file as input as it can be done dynamically. Tried awk commands but not getting desired output.Using SunOS 5.10 version.... (14 Replies)
Discussion started by: weknowd
14 Replies

2. Shell Programming and Scripting

Replacing nth field with nth_text for each line in a file

Hi All, I am very new to shell scripting and tried to search this in the forum but no luck. Requirment: I have an input file which is comma separated. I need to replace the value in 4th column with another value. This has to happen for all the lines in the file. Sample data: Input... (2 Replies)
Discussion started by: arunkumarsd
2 Replies

3. Shell Programming and Scripting

replacing a value in a field

Hi All, I have a file yum.conf that has a field called gpgcheck this field sometimes has a value of 0 gpgcheck=0 and at other times it has a 1. I need to check the value and if it is a 1 change it to a 0 any ideas? thanks, Gartie (1 Reply)
Discussion started by: gartie
1 Replies

4. Shell Programming and Scripting

Replacing the last field

Suppose I have a file abc.txt which contain lines:- 11.3.5.7 11.3.6.7 11.6.8.9.10 I want to replace the last field of the line to some value .I want the first line should become 11.3.5.86 .Same applies for rest lines.Please help. (6 Replies)
Discussion started by: maitree
6 Replies

5. Shell Programming and Scripting

Help with Awk finding and replacing a field based on a condition

Hi everybody, I'm trying to replace the $98 field with "T" if the last field (108th) is T I've tried awk 'BEGIN{OFS=FS="|"} {if ($108=="T")sub($98,"T"); print}' test.txt but that doesn't do anything also tried awk 'BEGIN{OFS=FS="|"}{ /*T.$/ sub($98,"T")} { print}' test.txt but... (2 Replies)
Discussion started by: jghi123
2 Replies

6. Shell Programming and Scripting

replacing field in specific line in a file

Hi, I know there are lots of threads on replacing text within files, usually using sed or awk. However, I find it hard to adapt examples that I found to my specific case. I am kind of new to UNIX and have hard times learning the syntax either for sed or awk so I would appreciate any help. Here's... (5 Replies)
Discussion started by: vytenis
5 Replies

7. Shell Programming and Scripting

Supressing and replacing the output of a field in Awk

Wondering if anybody can help with changing the output of a field. I'm needing to change the output of a field in this file: User Process ID Time Active Licences Type ChangeAdmin (Phys-agsdev/19353 212), start Wed 1/21 6:30 (linger: 1800) u414013 (Phys-agsdev/19353 1491), start Wed 1/21 12:54... (5 Replies)
Discussion started by: Glyn_Mo
5 Replies

8. Shell Programming and Scripting

replacing a nul field with text

Ok here's my pickle. I have a file in which every line must be the same length. Each field within the line is a certain length. None of these can be changed. What I need to do is look at a specific field within this file, let's say it starts with character 30 and ends with 50. If this field is... (3 Replies)
Discussion started by: DarkHound
3 Replies

9. Shell Programming and Scripting

Replacing certain field

hi all, i am having a script file which contains lot of fields and commands. let's say i have the following word (example1) spread all over the script in every place, how can i replace it with the word (example2)?....the only way i know is to use to either delete the word (example1) and write... (1 Reply)
Discussion started by: charbel
1 Replies

10. Shell Programming and Scripting

Replacing the last field of a line.

Hi, I wrote a script which extracts data from 2 tables (joining the tables together) and outputs the fields to a csv file. the output may look something like scenario 1: a,b,c,d,1,2,3,4 or scenario 2: a,b,c,d,,,, now, in the second scenario, there are some empty fields at the end of... (3 Replies)
Discussion started by: Darek
3 Replies
Login or Register to Ask a Question