How to move certain line to the first line
I have a data file which contains a line of characters like
Code:
5 0.0275 10 0.2 0 0.2255 0.3105 0.375 0.39 0.231 0.361 0.38 0.2095 Code:
"model" "h" "K" "pcau" "nprot" "CCA" "single_TOW" "MTOW" "AWRR" "CCAc" "single_TOWc" "MTOWc" "AWRRc" Suppose I have a batch of these files named as ***.txt. How can I do it at one time? |
Simplest would be to just pull that line out with say sed or grep, then pull all the lines *except* that. Redirect all the stdout to a new file. Rename both.
For the list just use bash loop - for i in *.txt ; ... This causes all the lines before the header to be read twice, but they should still be in page cache, so wouldn't cause any extra physical I/O. Edit: only true if you take steps to quit reading the file after findng the header - else the entire file gets read twice. I use sed to do this. KISS. |
I would probably use awk for the moving of the line, but my first question would be, what creates the data? Is it not possible to get the data correct the first time around??
|
Quote:
|
Fair enough :) So have a look at awk to solve your problem. You can store the lines until the header in an array and then once you find the header, print it and all stored in the array
and just print the rest of the lines after that. You will of course need to redirect this into a new file. http://www.gnu.org/software/gawk/man...ode/index.html |
I would suggest creating the header first, then have the rest of the systems append to the file without the header...
|
Maybe something simple as this:
Code:
#!/bin/bash Run as Code:
shifter.sh *.txt |
In the spirit of "sed is Turing complete -- it can do anything," how about something as obscure as this:
Code:
$ cat >/tmp/movelabel.sed That second line of the script might need a little explanation. It handles the case where no label line is found in the file and just regurgitates the whole file as read if that is the case. Without that line it would discard all the data and leave just an empty file. |
All times are GMT -5. The time now is 07:03 AM. |