Find and replace a field in the last line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Find and replace a field in the last line
# 1  
Old 10-27-2008
Find and replace a field in the last line

I have a file 'test.out' with contents:
1|1|10|10|I|asdf|
2|1|10|10|I|sdfg|
4|1|10|10|I|hgfj|
34|0|10|10|I|sdg|

I want to modify the fifth column with value 'I' to 'A' for only the last line. Below is what I expect to see:

1|1|10|10|I|asdf|
2|1|10|10|I|sdfg|
4|1|10|10|I|hgfj|
34|0|10|10|A|sdg|

Thanks,

- CB
# 2  
Old 10-27-2008
Question Need more info

While last line, always the 4th line? Always with a 34 in field 1?

When replace I with A, will data always be in this layout? Do we need to worry about other I characters on the line?

For instance, the following works, but perhaps not considering enough of what you need.

Code:
> head -3 file05 ; tail -1 file05 | sed "s/I/A/"
1|1|10|10|I|asdf|
2|1|10|10|I|sdfg|
4|1|10|10|I|hgfj|
34|0|10|10|A|sdg|


Last edited by joeyg; 10-27-2008 at 06:34 PM.. Reason: Added example
# 3  
Old 10-27-2008
The size of the file will change, and '34' is only an example. The id may be different.

Thanks,

- CB
# 4  
Old 10-27-2008
Use /usr/xpg4/bin/sed on Solaris:

Code:
sed '$s/\(\([^|]*|\)\{4\}\)[^|]/\1A/' infile

 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find max length of the field and then replace zero

hai guys, pick the 1st field and calculate max length. if suppose max length is 2, then compare the all records if <2 then add zero's prefix of the record. for ex: s.no,sname 1,djud 37,jtuhe in this max length of the 1st field is 2 right the output wil be s.no,sname 01,djud... (6 Replies)
Discussion started by: Suneelbabu.etl
6 Replies

2. Shell Programming and Scripting

Find a blank field and replace values to NA

Hi All, i have a file like col1 col2 col3 13 24 NA 12 13 14 11 12 13 14 22 NA 18 26 NA in this file if i found "NA" other values in the line are also replace by NA Could you help me! (7 Replies)
Discussion started by: Shenbaga.d
7 Replies

3. Shell Programming and Scripting

To find char field and replace null

hi, i having a file with | seperated in which i need to search char in 3rd column and replace with null. i need to replace only the coulmn where character occurs in 3rd field for eg: file1.txt xx|yy|xx|12 output file: xx|yy||12 (5 Replies)
Discussion started by: rohit_shinez
5 Replies

4. Shell Programming and Scripting

Replace first field of a line with previous filed of the line

Hi Everyone, I have a file as below: IM2345638,sherfvf,usha,30 IM384940374,deiufbd,usha,30 IM323763822,cdejdkdnbds,theju,15 0,dhejdncbfd,us,20 IM398202038,dhekjdkdld,tj,30 0,foifsjd,u2,40 The output i need is as below IM2345638,sherfvf,usha,30... (4 Replies)
Discussion started by: usha rao
4 Replies

5. Shell Programming and Scripting

sed to replace a field from a line with another field

i have something like this, cat filename.txt hui this si s"dfgdfg" omeone ipaddress="10.19.123.104" wel hope this works i want to replace only 10.19.123.104 with different ip say 10.19.123.103 i tried this sed -i "s/'ipaddress'/'ipaddress=10.19.123.103'/g" filename.txt ... (1 Reply)
Discussion started by: vivek d r
1 Replies

6. Shell Programming and Scripting

Find field count and replace

Hello All, I have a file with contents like apple|ball|charlie|David| England|France|Germany| Ireland|Japan|King|London| Man|Nancy|Orange| here the column delimiter is | so if any of the lines/rows in the file has 3 only records (last field is empty), i want to place a | at the end of... (4 Replies)
Discussion started by: vinredmac
4 Replies

7. Shell Programming and Scripting

Find and replace blank in the last field

Hi all, I have a huge file and I need to get ride of the fields 6-11 and replace the blanks in field 5 with a missing value(99999). 159,93848,5354,343,67898,45,677,5443,434,5545,45 677,45545,3522,244, 554,54344,3342,456, 344,43443,2344,444,23477... (12 Replies)
Discussion started by: GoldenFire
12 Replies

8. Shell Programming and Scripting

awk: find and replace in certain field only, help needed

I got a sample file like this. $ cat test 12|13|100|s 12|13|100|s 100|13|100|s 12|13|100|s I want to replace all 100 by 2000 only in 3rd field using "awk" This is replacing all 100's :-( $ awk -F "|" '{gsub( /100/,"2000");print}' test 12|13|2000|s 12|13|2000|s 2000|13|2000|s... (5 Replies)
Discussion started by: jkl_jkl
5 Replies

9. Shell Programming and Scripting

find pattern and replace another field

HI all I have a problem, I need to replace a field in a file, but only in the lines that have some pattern, example: 100099C01101C00000000059394200701CREoperadora_TX 100099C01201C00000000000099786137OPERADORA_TX2 in the example above I need to change the first field from 1 to 2 only if... (3 Replies)
Discussion started by: sergiioo
3 Replies
Login or Register to Ask a Question