replace 3rd field of space delimited text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace 3rd field of space delimited text file
# 1  
Old 04-05-2011
replace 3rd field of space delimited text file

how to replace the 3rd colum? Each line begins similarly, but they all ends variously.

XX YY 03 variable text here
XX YY 03 more variable text here
XX YY 03 even more variable text here really long setence
XX YY 03 variable numbers also appear 03 11. 123 456
XX YY 03 the occasional comma, and period. are in here also

anyway, I want the above text file to look like this:

XX YY 01 variable text here
XX YY 01 more variable text here
XX YY 01 even more variable text here really long setence
XX YY 01 variable numbers also 03 11. 123 456
XX YY 01 the occasional comma, and period. are in here also

The X's and Y's are ALWAYS random two-digit numbers, the 3rd colum is always 03, but sometimes 03 is found elsewhere in the line. So the only thing I want changed is the 3rd column from 03 to 01. (as seen above)

Any ideas? I searched and found extensive sed/awk tricks..but this (I think) is simpler.
# 2  
Old 04-05-2011
Something like this?
Code:
awk '{$3="01"}1' file

# 3  
Old 04-05-2011
assuming XX and YY never match 03

Code:
sed 's/03/01/' infile


Last edited by ctsgnb; 04-06-2011 at 06:32 AM..
# 4  
Old 04-05-2011
this worked...

Franklin52, your reply helped, thank you... not sure why but the commad:
awk '{$3="01"}1' file
did not work
however, it got me thinking of what would work and I came up with this:
awk '{$3="01"; print $0}' file
this seemed to work. Does this make sense?
Thank you so much!
# 5  
Old 04-05-2011
All these should work.

Code:
awk '{$3="01"};1' filename

Code:
awk '$3="01"' filename

Code:
awk '{$3="01"}1' filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace field in the delimited file

Hi, I have the requirement similar to the one mentioned in the below thread. https://www.unix.com/unix-for-dummies-questions-and-answers/128155-search-replace-string-only-particular-column-delimited-file.html The only difference is that I need to change the field for row 1,2 and the last... (14 Replies)
Discussion started by: chetanojha
14 Replies

2. Shell Programming and Scripting

How to remove alphabets/special characters/space in the 5th field of a tab delimited file?

Thank you for 4 looking this post. We have a tab delimited file where we are facing problem in a lot of funny character. I have tried using awk but failed that is not working. In the 5th field ID which is supposed to be a integer only of that file, we are getting corrupted data as below. I... (12 Replies)
Discussion started by: Srithar
12 Replies

3. UNIX for Dummies Questions & Answers

Changing only the first space to a tab in a space delimited text file

Hi, I have a space delimited text file but I only want to change the first space to a tab and keep the rest of the spaces intact. How do I go about doing that? Thanks! (3 Replies)
Discussion started by: evelibertine
3 Replies

4. UNIX for Dummies Questions & Answers

Swap two rows in a space delimited text file?

Hi, How do you swap two rows in a space delimited text file? Thanks! (4 Replies)
Discussion started by: evelibertine
4 Replies

5. UNIX for Dummies Questions & Answers

Converting a text file with irregular spacing into a space delimited text file?

I have a text file with irregular spacing between values which makes it really difficult to manipulate. Is there an easy way to convert it into a space delimited text file so that all the spaces, double spaces, triple spaces, tabs between numbers are converted into spaces. The file looks like this:... (5 Replies)
Discussion started by: evelibertine
5 Replies

6. UNIX for Dummies Questions & Answers

Deleting lines that contain a specific string from a space delimited text file?

Hi, I have a space delimited text file that looks like the following: 250 rs10000056 0.04 0.0888 4 189321617 250 rs10000062 0.05 0.0435 4 5254744 250 rs10000064 0.02 0.2403 4 127809621 250 rs10000068 0.01 NA 250 rs1000007 0.00 0.9531 2 237752054 250 rs10000081 0.03 0.1400 4 17348363... (5 Replies)
Discussion started by: evelibertine
5 Replies

7. UNIX for Dummies Questions & Answers

How do you view specific columns from a space delimited text file?

I have a space delimited text file with 1,000,000+ columns? I would only like to view specific ones (let's say through 1:10), how can I do that? Thanks! (3 Replies)
Discussion started by: evelibertine
3 Replies

8. UNIX for Dummies Questions & Answers

Deleting columns from a space delimited text file

I have a space delimited text file with 1,000,000+ columns and 100 rows. I want to delete columns 2 through 5 (2 and 5) included from the text file. How do I do that? Thanks. (3 Replies)
Discussion started by: evelibertine
3 Replies

9. UNIX for Dummies Questions & Answers

Replacing a field in pipe delimited TEXT File

Hi, I want to replace a field in a text delimited file with the actual number of records in the same file. HDR|ABCD|10-13-2008 to 10-19-2008.txt|10-19-2008|XYZ DTL|0|5464-1|0|02-02-2008|02-03-2008||||F||||||||| DTL|1|5464-1|1|02-02-2008|02-03-2008|1||JJJ... (3 Replies)
Discussion started by: ravi0435
3 Replies

10. UNIX for Dummies Questions & Answers

Searching for text in a Space delimited File

Hi I am trying to search a firewall syslog space delimeted file for all of the different tcp and udp destination ports. I know that grep will find lines that contain specific text. And I have tried using the the the cut command to cut out of the file certain colums. However the test I am... (6 Replies)
Discussion started by: andyblaylock
6 Replies
Login or Register to Ask a Question