LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-24-2015, 01:49 AM   #1
Mike_Brown
Member
 
Registered: May 2015
Posts: 37

Rep: Reputation: Disabled
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
5 0.05 10 0.2 0 0.352 0.637 0.7045 0.7095 0.36 0.702 0.7135 0.404
1 0.02 10 0.2 0 0.0845 0.1265 0.0805 0.0865 0.0865 0.126 0.0815 0.0735
4 0.04 10 0.2 0 0.2705 0.4465 0.4945 0.503 0.284 0.517 0.503 0.214
"model" "h" "K" "pcau" "nprot" "CCA" "single_TOW" "MTOW" "AWRR" "CCAc" "single_TOWc" "MTOWc" "AWRRc"
2 0.03 10 0.2 0 0.165 0.2285 0.22 0.221 0.181 0.261 0.229 0.153
2 0.05 10 0.2 0 0.237 0.437 0.288 0.344 0.2425 0.5045 0.2785 0.2025
2 0.02 10 0.2 0 0.1245 0.1605 0.144 0.1365 0.1225 0.184 0.148 0.1025
4 0.02 10 0.2 0 0.1665 0.1945 0.2185 0.202 0.1705 0.217 0.221 0.12
1 0.08 10 0.2 0 0.215 0.2835 0.2215 0.171 0.2265 0.3235 0.233 0.1465
4 0.03 10 0.2 0 0.2185 0.3405 0.3595 0.351 0.2305 0.409 0.363 0.164
5 0.035 10 0.2 0 0.241 0.421 0.439 0.5165 0.245 0.484 0.449 0.275
6 0.03 10 0.2 0 0.13 0.3395 0.223 0.283 0.144 0.3865 0.2325 0.167
6 0.02 10 0.2 0 0.1025 0.2095 0.155 0.1675 0.1035 0.23 0.1595 0.102
6 0.04 10 0.2 0 0.1805 0.4655 0.324 0.3925 0.189 0.5385 0.332 0.17
5 0.02 10 0.2 0 0.1425 0.2235 0.259 0.249 0.162 0.261 0.257 0.1475
4 0.05 10 0.2 0 0.32 0.568 0.6035 0.617 0.3405 0.64 0.62 0.2765
1 0.04 10 0.2 0 0.1305 0.1585 0.12 0.1135 0.135 0.168 0.1255 0.113
6 0.01 10 0.2 0 0.072 0.094 0.0905 0.083 0.067 0.1115 0.0915 0.0645
3 0.07 10 0.2 0 0.4225 0.3655 0.3985 0.368 0.4275 0.4195 0.4 0.4135
3 0.08 10 0.2 0 0.4355 0.422 0.412 0.399 0.4455 0.464 0.4085 0.4095
2 0.06 10 0.2 0 0.285 0.537 0.3565 0.437 0.294 0.571 0.3575 0.2885
4 0.01 10 0.2 0 0.101 0.097 0.115 0.1045 0.1085 0.1155 0.115 0.0665
1 0.1 10 0.2 0 0.279 0.343 0.2375 0.227 0.289 0.367 0.2565 0.183
1 0.06 10 0.2 0 0.1945 0.2045 0.1485 0.154 0.2035 0.2365 0.1535 0.1315
3 0.06 10 0.2 0 0.3455 0.3225 0.311 0.326 0.3565 0.3535 0.309 0.362
5 0.0425 10 0.2 0 0.3145 0.5365 0.596 0.625 0.3185 0.6095 0.614 0.338
2 0.04 10 0.2 0 0.2005 0.3485 0.2585 0.2865 0.208 0.3875 0.2655 0.196
3 0.05 10 0.2 0 0.2965 0.2495 0.266 0.2655 0.303 0.2865 0.2695 0.2865
Everytime, the number of line which contains characters is not always same. Is there a way to move the line
Code:
"model" "h" "K" "pcau" "nprot" "CCA" "single_TOW" "MTOW" "AWRR" "CCAc" "single_TOWc" "MTOWc" "AWRRc"
to first line?
Suppose I have a batch of these files named as ***.txt. How can I do it at one time?
 
Old 12-24-2015, 03:43 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,838

Rep: Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822
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.

Last edited by syg00; 12-24-2015 at 03:50 AM.
 
Old 12-24-2015, 03:45 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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??
 
Old 12-24-2015, 11:32 AM   #4
Mike_Brown
Member
 
Registered: May 2015
Posts: 37

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
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??
I am using campus computers to running program, each of computers will write a data line to the above data file. And One computer will write both of the header and the data line to file to let me known what each column represents. Since each computer's speed is different. So the header line in different line number. I cannot control the computer (which will write both of the header and the data line) to finish program first.

Last edited by Mike_Brown; 12-24-2015 at 11:34 AM.
 
Old 12-24-2015, 12:25 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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
 
Old 12-24-2015, 12:26 PM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
I would suggest creating the header first, then have the rest of the systems append to the file without the header...
 
1 members found this post helpful.
Old 12-24-2015, 01:08 PM   #7
Aia
Member
 
Registered: Jun 2006
Posts: 66

Rep: Reputation: 21
Maybe something simple as this:
Code:
#!/bin/bash

for i
do
    sed -n '/^[^0-9]/p' "${i}" > "${i}.tmp"
    sed -n '/^[0-9]/p' "${i}"  >> "${i}.tmp"
    #if [ -s "${i}.tmp" ]; then
    #    mv -v "${i}.tmp" "${i}"
    #fi
done
Remove the #s if you are happy with the result.
Run as
Code:
shifter.sh *.txt
 
Old 12-24-2015, 02:57 PM   #8
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,959

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
In the spirit of "sed is Turing complete -- it can do anything," how about something as obscure as this:
Code:
$ cat >/tmp/movelabel.sed
/"/{p;x;s/^\n//p;b}
${x;s/"/&/;x;t copy;H;x;s/^\n//p;b}
{x;s/"/&/;x;t copy;H;b;: copy;p;b}
$ sed -i -n -f /tmp/movelabel.sed *.txt
I suggest trying that on just one file and without the "-i" option to see what it does. As written, it uses the presence of a double quote mark to identify the line with the labels. You can substitute whatever string you like for that.

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.

Last edited by rknichols; 12-24-2015 at 10:05 PM. Reason: Show the shell commands in blue, sed script in black
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Cursor does not move to the bottom line and overwrite to same line on command line mesuutt Linux - Newbie 3 02-25-2012 09:04 AM
[SOLVED] Copy and replacing specific line from file1 to file2 line by line vjramana Programming 10 03-28-2011 08:49 AM
Move alternate line to the line above micyew Programming 10 01-14-2011 04:12 PM


All times are GMT -5. The time now is 12:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration