LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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-15-2019, 08:43 PM   #1
Pinguino99
Member
 
Registered: Dec 2019
Posts: 44

Rep: Reputation: Disabled
problem tr using pipe


i'm using tr to change letters but it has a problem when using the pipe

Code:
cat file |tr 'aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ1234567890' '7Ns3GwKfPDihv9dFq6Z8grnyLWSCp0VEQoBkAzbU5jmOJYeuXM24Ixl1aRtHT'
words in file
abc : 123foobar

Code:
output: +++ 9 r3s++++++

Last edited by Pinguino99; 12-15-2019 at 08:52 PM.
 
Old 12-15-2019, 08:50 PM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
I suppose you want us to guess which problem it has. My suggestion: Describe the problem.

By the way, you need neither cat nor pipe:
Code:
tr 'aAbBcCd...' '7Ns3GwKfP...' file
 
Old 12-15-2019, 09:05 PM   #3
Pinguino99
Member
 
Registered: Dec 2019
Posts: 44

Original Poster
Rep: Reputation: Disabled
tr sends me to help if I run this way
and i need cat in pipe, it's a file
 
Old 12-15-2019, 09:49 PM   #4
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,803

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by Pinguino99 View Post
i'm using tr to change letters but it has a problem when using the pipe

Code:
cat file |tr 'aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ1234567890' '7Ns3GwKfPDihv9dFq6Z8grnyLWSCp0VEQoBkAzbU5jmOJYeuXM24Ixl1aRtHT'
words in file
abc : 123foobar

Code:
output: +++ 9 r3s++++++
  1. Try:
    Code:
    SET1=aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ1234567890
    SET2=7Ns3GwKfPDihv9dFq6Z8grnyLWSCp0VEQoBkAzbU5jmOJYeuXM24Ixl1aRtHT
    $ cat > INPUT.FILE
    abc : 123foobar
    ^D
    $ OUTPUT=$( tr "$SET1" "$SET2" < INPUT.FILE )
    $ echo $OUTPUT
    7sG : Ixlipps7B
    or, yout could replace that variable assignment to send the results directly to a file with
    Code:
    $ tr "$SET1" "$SET2" < INPUT.FILE > OUTPUT.FILE
    $ cat OUTPUT.FILE
    7sG : Ixlipps7B
  2. Check your translation strings. Do you see that "SET1" is one character longer than "SET2"? I'm not sure how 'tr' deals with that.


    Update: According to the man page, 'tr' extends "set2" to the length of "set1" by repeating the last character. This wouldn't make unequal translation strings terribly useful for undoing the "encryption" (if that's the goal here).

Last edited by rnturn; 12-15-2019 at 10:17 PM. Reason: Added alternate command
 
Old 12-15-2019, 10:15 PM   #5
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by Pinguino99 View Post
tr sends me to help if I run this way
and i need cat in pipe, it's a file
Oops sorry, I didn't know that tr reads from stdin only.
Code:
tr ..... < file
Which output do you expect?

EDIT: My output is 7sG : Ixlipps7B, same as rnturn's. Perhaps it has something to do with your language setting; in my case, LANG=en_US.UTF-8.

Last edited by berndbausch; 12-15-2019 at 10:22 PM.
 
Old 12-15-2019, 10:43 PM   #6
Pinguino99
Member
 
Registered: Dec 2019
Posts: 44

Original Poster
Rep: Reputation: Disabled
if I redirect the input "<" the output is +++ 9 r3s++++++
 
Old 12-15-2019, 10:59 PM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by berndbausch View Post
Perhaps it has something to do with your language setting; in my case, LANG=en_US.UTF-8.
I agree with berndbausch, it is not a problem with the pipe or redirection, it is non-ascii character encodings in the file.

What is your LANG setting? (echo $LANG)

WHere did the file text come from? Did you type it or copy paste from a browser?
 
Old 12-15-2019, 11:06 PM   #8
Pinguino99
Member
 
Registered: Dec 2019
Posts: 44

Original Poster
Rep: Reputation: Disabled
lang is en_us.utf-8
 
Old 12-16-2019, 12:14 AM   #9
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Non-ASCII characters can be revealed like this:
Code:
od -x file
or this:
Code:
cat -v file
among other solutions.
 
Old 12-16-2019, 12:51 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,848

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
You may try to unset LANG also (to check if that solves this issue)
 
Old 12-16-2019, 01:29 AM   #11
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,792

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Set LANG or better LC_ALL for the tr command:
Code:
< file LC_ALL=C tr 'aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ1234567890' '7Ns3GwKfPDihv9dFq6Z8grnyLWSCp0VEQoBkAzbU5jmOJYeuXM24Ixl1aRtHT'
After the command the previous LC_ALL is again active.
Explanation:
The shell forks itself, and the forked instance execs (turns into) the command. An inline ENVVAR=value runs in the forked instance before the exec.
The
< file
is usually put after the command, but also works before the command (and even between command arguments).
 
Old 12-17-2019, 10:24 PM   #12
Pinguino99
Member
 
Registered: Dec 2019
Posts: 44

Original Poster
Rep: Reputation: Disabled
I was able to solve, just correctly set the language
thank you all
 
  


Reply



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
Using netcat to pipe stdout to pipe crabbit Linux - Networking 5 03-27-2018 12:06 PM
PHP shell_exec() always gives error "Broken pipe" when using pipe burek Linux - Server 1 01-19-2012 06:04 AM
pipe using no pipe soathana Programming 2 02-22-2003 04:33 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

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
Open Source Consulting | Domain Registration