Text file editing

kwanza95

New Member
Joined
Oct 30, 2019
Messages
2
Reaction score
2
Credits
0
Hi, I have a series of text files that I need to rearrange. Given the large number of files/lines, manual editing is not an option.
Any suggestions on using an awk command or similar would be appreciated.
Thank you for your help.

Input format:

SAMPLE 2200
23 3453 165 3963 483 4312 732 4405
1100 4513 1554 4645 1953 4800 2367 5056
2679 5283 2921 5379 4886 6027
SAMPLE 2400
27 3432 115 3598 296 4056 685 4389
1134 4606 1607 4684 2162 4839 2623 5110
2926 5236 3289 5410 4458 5820
.....
.....

Required output format:

2200 23 3453 165 3963 483 4312 732 4405
1100 4513 1554 4645 1953 4800 2367 5056
2679 5283 2921 5379 4886 6027
2400 27 3432 115 3598 296 4056 685 4389
1134 4606 1607 4684 2162 4839 2623 5110
2926 5236 3289 5410 4458 5820
....
....
 


This awk one liner should perform the task for a single file:
Bash:
awk '/^SAMPLE/ {gsub("SAMPLE ","") ; printf $0 " "; next} 7' < input.txt > output.txt

For any lines starting with the word "SAMPLE" - we use gsub to strip out "SAMPLE " - including the space after it.
Then we use printf to print the remainder of the line plus a space and then move to the next line. Print appends a newline, so using printf, we can assure that the output from any "SAMPLE" lines will effectively be pre-pended to the next line.

Also note - the 7 at the end of the awk command will just make awk perform it's default action (print) for any non-matching lines. The number is completely arbitrary. As long as it isn't zero, it will cause awk to perform it's default operation.
 

Members online


Top