0

I would like to use a one line Perl command to change data in a Bash variable. I think the problem is that the Perl one liner isn't receiving the piped in data.

I know about the bash change variable i.e. findString=${findString//\//\\/}

I'm curious about getting Perl to work too. I do not know Perl so keep it simple.

To be clear, these two lines are not working: I would like the tabs in the text to be changed to \t. I would like any Unix line ends to be changed to \n.

findString=$(cat "${findString}" | perl -0777pe 's/\t/\\t/g')

findString=$(cat "${findString}" | perl -0777pe 's/\n/\\n/g')

Here is my bash code:

#!/bin/bash

#The idea here is to change tab to \n
# and line End to \n

# debug info
export PS4='+(${BASH_SOURCE}:${LINENO}):'

# trace all the lines
#set -o xtrace
echo "---------------------- start ----------------------------------------"



# string to change.
# chops off the last \n

read -d '' findString <<"EOFEOFEOF"
# Usage: /Users/mac/Sites/bithoist/commandLine/BitHoist/BitHoist-PPC-MacOS-X [options] input... < input > output
               # Options and inputs may be intermixed
    -stdin     # Use standard input as input file
    -offset nn # Offset next input file data by nn

EOFEOFEOF

findString=$(cat "${findString}" | perl -0777pe 's/\t/\\t/g')

findString=$(cat "${findString}" | perl -0777pe 's/\n/\\n/g')

echo "------------> findString of length ${#findString} is:"
echo -E "${findString}"
echo 
2
  • 4
    You want echo, not cat, in those commands. Commented Aug 2, 2012 at 17:55
  • Thanks for the observation, chepner. I need echo. Commented Aug 3, 2012 at 18:11

2 Answers 2

2

It should work.

Here's how

$ echo Helloabtb | perl -0777pe 's/a/x/g'
Helloxbtb
$ myvar=`echo Helloabtb | perl -0777pe 's/a/x/g'`
$ echo $myvar
Helloxbtb

So if it works with echo it should work with cat. I suggest using backticks as shown above in my example, and try. Something like

 findString=`cat $findString | perl -0777pe 's/\t/\\t/g'`

also in most probability, cat expects a file. So in your case echo might be suited as

findString=`echo $findString | perl -0777pe 's/\t/\\t/g'`

OR

findString=$(echo "$findString" | perl -0777pe 's/\t/\\t/g')

OR

command="echo $findString | perl -0777pe 's/\t/\\t/g'"
findString=eval($command)
Sign up to request clarification or add additional context in comments.

4 Comments

Don't advise using backticks for command substitution in place of $() which is what the OP used. The latter form is much preferred over backticks.
The question was not whether backticks or $() works. That was just a suggestion. Its more of a choice than option, both do the same thing. The OP is using a fairly simple command. If it were a complicated nested command, sure $() may make the code readable. But for me both are one and the same. For the sake of it I am updating the answer.
No, both don't do the same thing as you seem to recognize. The readability issue includes avoiding the confusion between single quotes and backticks even for simple process substitution. Your statement "I suggest using backticks" is what prompted my comment.
Yes, thats because I have been using backticks for a long time now and I did not find any practical issues with doing that, hence "my suggestion". But you are welcome to edit my answer.
1

as @chepner already pointed out in his comment, you want to use echo instead of cat. cat expects a filename to cat, so it treats $findString as a filename.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.