Sed or awk, update 1st data column

I read about sed last week, and now, I am doing some extraction from the file using sed, that’s fine. Now I would like to change the data in the first column to the row number, say

1 3 3 0 0 0 1 3 35 34
16 3 3 0 0 0 34 35 33 19
31 3 3 0 0 0 19 33 71 68
46 3 3 0 0 0 68 71 72 69
61 3 3 0 0 0 69 72 73 70
76 3 3 0 0 0 70 73 67 53

and change the first column to 1-6, how can I do this in awk or sed? Best, Umut

+3
source share
3 answers

Definitely awkfor this scenario. The easiest way to accomplish this using sedwas as follows.

cat file | sed "s/^[0-9]*//g;=;" | sed -n "N;s/\n//g;p"

If the first expression deletes the first number and prints the line number =from the read channel.

The second expression sedextracts two lines at a time and deletes a new line \n.

, = , . sed.

+1

awk

awk '$1=NR' file
+6

You can try something like this
echo "1 3 3 0 0 0 1 3 35 34" | sed 's / ^ 1 / A / g'

Change A to the number you need, and I assume the first column is at the beginning of the row

0
source

Source: https://habr.com/ru/post/1761064/


All Articles