0

Code below can, for example ...

[anony@mous-pc ~]$ (printf 'g?%s?m0\n' 008 006 004 002 ; printf 'wq\n') | ed -s file.txt

... take all of numbers within ...

[anony@mous-pc ~]$ cat file.txt
005
003
110
069
002
008
004
245
009
007
006

... and move desired numbers (or other strings) to top, BUT in reverse order of executed code ...

[anony@mous-pc ~]$ cat file.txt
002
004
006
008
005
003
110
069
245
009
007

... just so long as you give the order in reverse, the order will be first to last (opposite order), or ... others may call it top to bottom formatting ... so one more time, to achieve first to last order I had to execute this way ...

[anony@mous-pc ~]$ (printf 'g?%s?m0\n' 008 006 004 002 ; printf 'wq\n') | ed -s file.txt

BUT, how can one input their numbers (or strings) in the order they intended for being first to last and still achieve first to last order?

If not possible to change inputted results, would sed be able to change output results and flip or reverse them before saving file.txt?

3
  • Can you supply the expected output? May 21 '19 at 3:49
  • 3
    What’s the difference between this and your previous question on the topic? May 21 '19 at 4:24
  • @Stephen the other question is asking to reverse its action meaning instead of top of file, bottom of file. This question is asking to reverse the executing order required during usage.
    – Anonymous
    May 23 '19 at 12:44
1
$ perl -lne '
   /00[2468]$/ ? $h{$_} : $A[@A] = $_; }{
   print for @h{qw/008 006 004 002/}, @A;
' file.txt

Store in separate containers matching and nonmatching lines. When time comes to print them, pull out the order from the anonymous array keying into the hash %h.

Explanation:

  • As we read in each line, look to see it contains the matching line, those that end in 2,4,6, or 8. Store them in the hash %h with the key being the whole matching line $h{$_}.
  • OTW, for a no match, push the nonmatching line to the back of the array @A.
  • After we've seen the end of file, we print the hash %h first. As you would be aware that access of hashes is inherently random. But we must impose an order since that was one of your requirements. So we prescribe the order by listing the keys in the order you want them to appear: qw/008 006 004 002/ This is an anonymous array. This is gonna print $h{008} followed by $h{006} and so on.
  • After that the array @A is printed which had the non matched lines. Q.E.D.
  • Now, if you were to interchange the order of printing the array @A and the hash slice @h{...} you should see the matching elements move south instead of north.
  • The code just prints to the stdout, which you would have to move back into the original: perl '...' file.txt > /tmp/file.txt; mv /tmp/file.txt file.txt
4
  • You are awesome thank you so much. How can I achieve the same results however instead of moving to top of file, move to bottom of file?
    – Anonymous
    May 23 '19 at 12:48
  • Sorry but your code had no effect on the input file for me. I am running Manjaro Linux 17.1.7 and have perl installed. Do you know what could be the reason?
    – Anonymous
    May 23 '19 at 12:57
  • Just change the order of printing the array@A and the hash %h and you're done. Store the output into another file and then move that file to the original. May 23 '19 at 15:15
  • Wow, I have no idea how to do even just one of the things you are suggesting. I don't even see %h in your code. Can you possibly explain what you are talking about? The code you provided does nothing to input.file after executed.
    – Anonymous
    May 23 '19 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.