Replacing the last field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing the last field
# 1  
Old 09-23-2011
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.
# 2  
Old 09-23-2011
To change the last field of every line to become "86" you can use

Code:
awk -F. '{$4="86"; print}'

# 3  
Old 09-23-2011
That $4 wont work on the test file user provided.

If the last field is not static you can use:

Code:
awk -F. '{$NF="toto"; print}' file


Last edited by maverick72; 09-23-2011 at 11:05 AM.. Reason: works better with file at the end...
# 4  
Old 09-23-2011
Using sed

Code:
val=86; sed "s/[0-9]*$/$val/g" input_file

--ahamed
# 5  
Old 09-23-2011
Code:
awk -F. '$NF="86"' OFS="." file

# 6  
Old 09-23-2011
Code:
awk -F. '{gsub($NF,"CHANGEME") }1' file

---------- Post updated at 05:24 PM ---------- Previous update was at 05:13 PM ----------

Quote:
Originally Posted by ahamed101
Using sed

Code:
val=86; sed "s/[0-9]*$/$val/g" input_file

--ahamed
try like thisSmilie
Code:
val=CHANGEME; sed "s/.[^.]*$/.$val/" file
## or
sed "s/[^.]*$/$val/" file

# 7  
Old 09-23-2011
Code:
$ awk -F. '{sub($NF,86);print}' log.log 
11.3.5.86
11.3.6.86
11.6.8.9.86

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

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

5. 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

6. 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

7. Shell Programming and Scripting

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... (5 Replies)
Discussion started by: shennanigan83
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