最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

bash - AWK copy value from $7 to the line below - Stack Overflow

programmeradmin6浏览0评论

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

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

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

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
Share Improve this question edited 6 hours ago Tedee12345 asked 7 hours ago Tedee12345Tedee12345 1,3424 gold badges17 silver badges26 bronze badges 2
  • edit your question to tell us what separates the fields in your input - tabs or blanks or is it fixed-width fields or something else. Also - do you REALLY care about preserving spacing or do you just want tabular output but don't really care if the spacing is exactly the same as in your input? Creating columnar output is simple with column, retaining spacing becomes harder to achieve if $7 can contain some number other than dd:dd format e.g. 100.1234 or 1.0 which MUST change spacing so edit your example to include such values if they can occur or state that they cannot otherwise. – Ed Morton Commented 6 hours ago
  • 1 @Ed Morton - I edited the question. This is what the column spacing looks like. It doesn't have to be identical formatting. I want column output – Tedee12345 Commented 6 hours ago
Add a comment  | 

2 Answers 2

Reset to default 1

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.

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
发布评论

评论列表(0)

  1. 暂无评论