LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 02-17-2013, 01:38 PM   #1
snmcdonald
Member
 
Registered: Jul 2011
Location: Canada
Distribution: Debian, Arch
Posts: 55

Rep: Reputation: 0
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

Last edited by snmcdonald; 02-17-2013 at 01:39 PM.
 
Old 02-17-2013, 02:14 PM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Code:
tac -r -s '.\| '
should reverse a file byte-by-byte, so you can just use dd then reverse the file.
 
1 members found this post helpful.
Old 02-17-2013, 02:38 PM   #3
snmcdonald
Member
 
Registered: Jul 2011
Location: Canada
Distribution: Debian, Arch
Posts: 55

Original Poster
Rep: Reputation: 0
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.

Last edited by snmcdonald; 02-17-2013 at 03:13 PM.
 
Old 02-17-2013, 06:45 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,301

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
See also http://linux.die.net/man/1/rev
 
Old 02-17-2013, 06:55 PM   #5
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,104

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by snmcdonald View Post
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?
 
Old 02-18-2013, 03:25 AM   #6
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Quote:
Originally Posted by snmcdonald View Post
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.
 
1 members found this post helpful.
Old 02-18-2013, 04:49 PM   #7
rigor
Member
 
Registered: Sep 2011
Posts: 152

Rep: Reputation: Disabled
If the distro you're using has the rev command, you could just use that without any args.
 
1 members found this post helpful.
Old 03-01-2013, 11:30 AM   #8
snmcdonald
Member
 
Registered: Jul 2011
Location: Canada
Distribution: Debian, Arch
Posts: 55

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by rigor View Post
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
 
Old 03-01-2013, 11:35 AM   #9
snmcdonald
Member
 
Registered: Jul 2011
Location: Canada
Distribution: Debian, Arch
Posts: 55

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by frieza View Post
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.
 
  


Reply

Tags
dd


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
Reverse the order of lines using sed Karljoe2 Programming 4 12-26-2012 10:38 AM
how do i get ubuntu to print in reverse order baronobeefdip Linux - Hardware 4 05-09-2010 01:34 AM
[SOLVED] Can I set the cups output order to always print in reverse order Thane Ubuntu 2 12-06-2009 06:57 AM
thunar copies directory files to usb stick out of order. curmudgeonbob Linux - General 1 07-28-2009 11:59 AM
Reverse Order Printing lawrencegoodman Linux - Newbie 2 11-13-2003 07:59 AM


All times are GMT -5. The time now is 02:14 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration