LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Is there a program that copies in reverse order? (https://www.linuxquestions.org/questions/linux-software-2/is-there-a-program-that-copies-in-reverse-order-4175450567/)

snmcdonald 02-17-2013 01:38 PM

Is there a program that copies in reverse order?
 
I am looking for a program that copies programs in byte reverse order. Something similar to dd where bs and count can be specified. Is there currently a commandline program that exists for that?

ie.

Code:

file1: abcdefghijklmnopqrstuvwxyz

ddreverse bs=1 count=10 if=file1 of=file2
file2: jihgfedcba

ddreverse bs=5 count=2 if=file1 of=file2
file2: fghijabcde

ddreverse bs=10 count2 skip=6 if=file1 of=file2
file2: qrstuvwxyzghijklmnop


Snark1994 02-17-2013 02:14 PM

Code:

tac -r -s '.\| '
should reverse a file byte-by-byte, so you can just use dd then reverse the file.

snmcdonald 02-17-2013 02:38 PM

Thank you, That works! I had to add the flag -b

Code:

tac -b -r -s '.\|' file1
However,

Why do you escape the pipe? Seems to have some strange nuances. GNU BRE adds alteration by escape. You originally had '.\| ', which means any character except new line (. == [^n]) or space.

Technically, '.\|' does not make sense to me, so I am not sure why it works. I'd prefer to avoid regex if possible.

chrism01 02-17-2013 06:45 PM

See also http://linux.die.net/man/1/rev

frieza 02-17-2013 06:55 PM

Quote:

Originally Posted by snmcdonald (Post 4893870)
Thank you, That works! I had to add the flag -b

Code:

tac -b -r -s '.\|' file1
However,

Why do you escape the pipe? Seems to have some strange nuances. GNU BRE adds alteration by escape. You originally had '.\| ', which means any character except new line (. == [^n]) or space.

Technically, '.\|' does not make sense to me, so I am not sure why it works. I'd prefer to avoid regex if possible.

why escape the pipe?

because the pipe has actual significance in a Linux shell
Code:

ls | less
and
Code:

ls \| less
are two different commands

one pipes the output of ls into the less command
the other tries to list the file named | and a file named less
the pipe takes output of one command and uses it as the input for the next, unless escaped.
understand?

Snark1994 02-18-2013 03:25 AM

Quote:

Originally Posted by snmcdonald (Post 4893870)
Thank you, That works! I had to add the flag -b

Code:

tac -b -r -s '.\|' file1
However,

Why do you escape the pipe? Seems to have some strange nuances. GNU BRE adds alteration by escape. You originally had '.\| ', which means any character except new line (. == [^n]) or space.

Technically, '.\|' does not make sense to me, so I am not sure why it works. I'd prefer to avoid regex if possible.

I don't see why you need the '-b' flag, it sees to work without for me... Can you give me an example of a file where it doesn't work?

With regards to the pipe, it's an "or": there's a space after the pipe, so it's saying "match anything except a newline". If you didn't escape it, it would literally be matching the sequence "any character, then |, then a space" which isn't what you want.

rigor 02-18-2013 04:49 PM

If the distro you're using has the rev command, you could just use that without any args.

snmcdonald 03-01-2013 11:30 AM

Quote:

Originally Posted by rigor (Post 4894674)
If the distro you're using has the rev command, you could just use that without any args.

rev | tac is what I need

Take for example:
helloworld
abcdef
12345

rev produces:
echo -e "hello world\nabcdef\n12345" | rev
dlrow olleh
fedcba
54321

rev | tac produces
echo -e "hello world\nabcdef\n12345" | rev | tac
54321
fedcba
dlrow olleh

snmcdonald 03-01-2013 11:35 AM

Quote:

Originally Posted by frieza (Post 4894015)
why escape the pipe?

because the pipe has actual significance in a Linux shell
Code:

ls | less
and
Code:

ls \| less
are two different commands

one pipes the output of ls into the less command
the other tries to list the file named | and a file named less
the pipe takes output of one command and uses it as the input for the next, unless escaped.
understand?

I understand pipe redirection. The escaped pipe symbol is part of the regex expression and not part of the bash shell.

Quote:

On top of what POSIX BRE provides as described above, the GNU extension provides \? and \+ as an alternative syntax to \{0,1\} and \{1,\}. It adds alternation via \|, something sorely missed in POSIX BREs.
http://www.regular-expressions.info/gnu.html

The pipe is an GNU extension to POSIX BRE. The original suggestion was escape . or white space. My change was to remove the white space which should be nothing.


All times are GMT -5. The time now is 04:16 AM.