KianStar
KianStar

Reputation: 177

How to manipulate components of just one column in a txt file

In a folder I have some txt files with 3 columns. I'm wondering if it is possible to append a character (say L) to all components of the third column. I managed to do it in matlab, but for those numbers consisting of less than 3 digit, like 81 , it appears as L 81 however,the space after L is not desired for me. Im wondering if there is a way to do it in UNIX/shell? The files look like

0 1400000 165
1400000 1700000 336
1700000 2500000 216
2500000 3000000 228
3000000 3900000 366
3900000 4100000 108
4100000 4900000 250
4900000 5100000 81

I want them to be

0 1400000 L165
1400000 1700000 L336
1700000 2500000 L216
2500000 3000000 L228
3000000 3900000 L366
3900000 4100000 L108
4100000 4900000 L250
4900000 5100000 L81

Upvotes: 1

Views: 44

Answers (2)

Mark Setchell
Mark Setchell

Reputation: 207678

You can use sed more simply like this to substitute the second space for a space followed by L

sed 's/ / L/2' yourFile

Upvotes: 1

Farvardin
Farvardin

Reputation: 5424

use sed to add L after the second space :

$ sed -r 's/(.* .* )/\1L/' file 
0 1400000 L165
1400000 1700000 L336
1700000 2500000 L216
2500000 3000000 L228
3000000 3900000 L366
3900000 4100000 L108
4100000 4900000 L250
4900000 5100000 L81

to save result to same file use -i :

$ sed -i -r 's/(.* .* )/\1L/' file 

Upvotes: 0

Related Questions