Artificial intelligent assistant

Merge two lines of a file and adding number in the end of lines I have a large text file abc.txt with the following format: BALT -6.110 130.600 4.06874414 -0.03254425 BALT -6.620 154.460 3.92286595 -0.17842244 BARM 32.740 140.680 3.94326190 -0.15802649 I need this file to convert an output file xyz.txt in the format like: BALT 1 -6.110 130.600 4.06874414 -0.03254425 BALT 2 -6.620 154.460 3.92286595 -0.17842244 BARM 3 32.740 140.680 3.94326190 -0.15802649 The numbers 1, 2, 3... after four character data name will increase as I go for more data.

Here's a oneliner:


# sed 's/^[A-Z]/>&/' abc.txt | awk -v RS='>' 'NR>1 {printf("%s %d\
%s %s\
%s\
%s\
",$1,NR-1,$2,$3,$4,$5)}' > xyz.txt


Here's the output generated:


# cat xyz.txt
BALT 1
-6.110 130.600
4.06874414
-0.03254425
BALT 2
-6.620 154.460
3.92286595
-0.17842244
BARM 3
32.740 140.680
3.94326190
-0.15802649


The details:

First part - the sed part of the line adds an arbitrary special character to the start of each record (a record is a name followed by 4 numbers). I picked a '>' to start the record. This makes the processing by awk easy.

Second part - For each record, just print out a new format of the fields as you specified. The only quirk is that there's an extra blank record at the start - we skip over that (NR>1).

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy da5399b16b65c579b69ff06691a68b4c