sed or awk to remove specific column to one range

I need to remove specific column to one range

source file

3 1 000123456
2 2 000123569
3 3 000123564
12 000123156
15 000125648
128 000125648

Output required

3 000123456
2 000123569
3 000123564
12 000123156
15 000125648
128 000125648
 
sed -r 's/\s\w+\s/ /' file

But this one removing all the second column. I need to remove only up to three lines. After third i need the two column

Wouldn't it be nice to post a complete specification in the first place, then? And, to show your own ideas and attempts?

Try an awk approach:

awk 'NF == 3 {$2 = ""} 1' file
3  000123456
2  000123569
3  000123564
12 000123156
15 000125648
128 000125648 

The refinements to eliminate the remaining two adjacent field separators are left as an exercise to the reader.

1 Like

it was working fine thanks

according to the problem statement

sed -r 's/\s\w+\s/ /' file
3 000123456
2 000123569
3 000123564
12 000123156
15 000125648
128 000125648

the desired range is set simply and the editor ignores the remaining lines out of range

echo 1 2 3 4 5 >> file
sed -r '1,3s/\s\w+\s/ /' file
3 000123456
2 000123569
3 000123564
12 000123156
15 000125648
128 000125648
1 2 3 4 5

If the sample data shown in post #1 is representative of the actual data that is to be processed and you don't want the extra <space> in the output produced by changing the field 2 value to an empty string, one could also try:

awk '{print $1, $NF}' file

which just prints the first and last field from each input line.

1 Like