Tedee12345
Tedee12345

Reputation: 1362

AWK copy value from $7 to the line below

Input file.

1  30.01.2025     30.01.2025           telefontelefon       -1.00           87.39   87.39   30.01.2025
2  31.01.2025     31.01.2025           telefontelefon       -3.46           32.14   32.14   31.01.2025
3  01.02.2025     
4  02.02.2025     
5  03.02.2025     03.02.2025           telefontelefon       -10.00          71.14   71.14   03.02.2025
6  04.02.2025     04.02.2025           telefontelefon       1609.00         80.14   80.14   04.02.2025
7  05.02.2025     05.02.2025           telefontelefon       11.00           22.14   22.14   05.02.2025
8  06.02.2025 
9  07.02.2025     05.02.2025           telefontelefon       14.00           22.14   55.14   05.02.2025

I want to get an output file.

1  30.01.2025     30.01.2025           telefontelefon       -1.00           87.39   87.39   30.01.2025
2  31.01.2025     31.01.2025           telefontelefon       -3.46           32.14   32.14   31.01.2025
3  01.02.2025                                                                       32.14 
4  02.02.2025                                                                       32.14
5  03.02.2025     03.02.2025           telefontelefon       -10.00          71.14   71.14   03.02.2025
6  04.02.2025     04.02.2025           telefontelefon       1609.00         80.14   80.14   04.02.2025
7  05.02.2025     05.02.2025           telefontelefon       11.00           22.14   22.14   05.02.2025
8  06.02.2025                                                                       22.14
9  07.02.2025     05.02.2025           telefontelefon       14.00           22.14   55.14   05.02.2025

enter image description here

For one column I used this command.

awk 'NF{last=$0;print;next} {$0=last}1'  plik

This is what the spaces between the columns look like.

    $ awk '{gsub(" ","-")};1' plik 
1--30.01.2025-----30.01.2025-----------telefontelefon--------1.00-----------87.39---87.39---30.01.2025
2--31.01.2025-----31.01.2025-----------telefontelefon--------3.46-----------32.14---32.14---31.01.2025
3--01.02.2025-----
4--02.02.2025-----
5--03.02.2025-----03.02.2025-----------telefontelefon--------10.00----------71.14---71.14---03.02.2025
6--04.02.2025-----04.02.2025-----------telefontelefon-------1609.00---------80.14---80.14---04.02.2025
7--05.02.2025-----05.02.2025-----------telefontelefon-------11.00-----------22.14---22.14---05.02.2025
8--06.02.2025-
9--07.02.2025-----05.02.2025-----------telefontelefon-------14.00-----------22.14---55.14---05.02.2025
pic@pi

Upvotes: 2

Views: 86

Answers (2)

Ed Morton
Ed Morton

Reputation: 204381

This doesn't retain spacing but makes the output columnar regardless of the original spacing or width of values stored in $7 so it may be acceptable if not better than what you asked for, using any awk and GNU column regardless of what the white space is in the input:

$ awk -v OFS='\t' 'NF<7{$7=prev} {prev=$7=$7} 1' file | column -s$'\t' -t
1  30.01.2025  30.01.2025  telefontelefon  -1.00    87.39  87.39  30.01.2025
2  31.01.2025  31.01.2025  telefontelefon  -3.46    32.14  32.14  31.01.2025
3  01.02.2025                                              32.14
4  02.02.2025                                              32.14
5  03.02.2025  03.02.2025  telefontelefon  -10.00   71.14  71.14  03.02.2025
6  04.02.2025  04.02.2025  telefontelefon  1609.00  80.14  80.14  04.02.2025
7  05.02.2025  05.02.2025  telefontelefon  11.00    22.14  22.14  05.02.2025
8  06.02.2025                                              22.14
9  07.02.2025  05.02.2025  telefontelefon  14.00    22.14  55.14  05.02.2025

Note: prev=$7=$7 is not a typo, that's shorthand for $7=$7; prev=$7 where $7=$7 will change all chains of white space on the line to tabs since modifying any field reconstructs $0 replacing all FSs with OFSs and prev=$7 saves the current value of $7 in the variable prev so it's available for use if needed in the next line, both of which are necessary steps to get the desired output.

If you don't have GNU column it's not hard to implement the same columnar formatting functionality in awk - just remember the width of every field as you process the input, save the processed fields in an array, and then in the END section print every field using the max width of all fields in each column. There are plenty of examples of that on SO or otherwise available by googling.

Upvotes: 2

Gilles Qu&#233;not
Gilles Qu&#233;not

Reputation: 185600

Check:

awk -v{O,}FS="\t" 'NF>3{last=$7;print;next} {$7="\t\t"last}1' file

or

awk 'BEGIN{FS=OFS="\t"}NF>3{last=$7;print;next} {$7="\t\t"last}1' file

yields:

1   30.01.2025  30.01.2025  telefontelefon  -1.00   87.39   87.39   30.01.2025
2   31.01.2025  31.01.2025  telefontelefon  -3.46   32.14   32.14   31.01.2025
3   01.02.2025                                              32.14
4   02.02.2025                                              32.14
5   03.02.2025  03.02.2025  telefontelefon  -10.00  71.14   71.14   03.02.2025
6   04.02.2025  04.02.2025  telefontelefon  1609.00 80.14   80.14   04.02.2025
7   05.02.2025  05.02.2025  telefontelefon  11.00   22.14   22.14   05.02.2025
8   06.02.2025                                              22.14
9   07.02.2025  05.02.2025  telefontelefon  14.00   22.14   55.14   05.02.2025

Upvotes: 2

Related Questions