Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

A=`echo | tr '\012' '\001' `

Original Message
Name: kamalpreet
Date: December 12, 2006 at 00:39:29 Pacific
Subject: A=`echo | tr '\012' '\001' `
OS: solaris
CPU/Ram: sun-sparc
Model/Manufacturer: sun
Comment:
A="`echo | tr '\012' '\001' `"
sed -e "s$A$1$A$2$A" $file

I am not able to understand the first line of the script.how is it working?


Warm Regards
Kamalpreet Singh


Report Offensive Message For Removal


Response Number 1
Name: nails
Date: December 12, 2006 at 09:45:33 Pacific
Subject: A=`echo | tr '\012' '\001' `
Reply: (edit)
The new-line character is being replaced by control-a

You are displaying the guts of the 'gres' script from page 51 of "sed & awk" by Robbins and Dougherty:

http://www.amazon.com/gp/product/15...

gres stands for Global Regular Expression Substitution. According to the authors:

"The echo|tr ... line is a complicated but portable way to generate a Control-A character to use as the separator for the sed substitue command."

The shell variable "$A" is then used as the separator on the sed command. Using this method is better than embedding a control-a in the script.


Report Offensive Follow Up For Removal

Response Number 2
Name: kamalpreet
Date: December 12, 2006 at 20:26:40 Pacific
Subject: A=`echo | tr '\012' '\001' `
Reply: (edit)
Yes its the same book(first edi.),anyways what I fail to understand is how its generating CNTL A .using that command.pls eloborate
Also why is he not usung the any visible(printable) seprator that can be used like (|or/).

Warm Regards
Kamalpreet Singh


Report Offensive Follow Up For Removal

Response Number 3
Name: James Boothe
Date: December 14, 2006 at 08:20:30 Pacific
Subject: A=`echo | tr '\012' '\001' `
Reply: (edit)
The echo command is echoing just an empty line, and each line in unix/linux terminates with one character referred to as a newline character. On other platforms it may be referred to as the linefeed or LF character. Code-wise, this is the decimal value 10, which is also octal value 12.

Let's echo an empty line and send it to a file:

echo > emptyline
wc -l < emptyline
ls -l emptyline

You can see from the above that we created a file with a single line, and the total size of that file is one character. That one character is the newline character that terminates that one empty line.

For the command string in question, the output of the echo (the empty line terminated by a linefeed) is not being sent to a disk file, but instead it is sent (piped) forward as input to the next command, which in this case is a tr command.

This particular tr command will change each linefeed character to a Ctrl-A character. An easy way to specify these special characters is via their octal values of 12 and 1 respectively.

The output of this entire pipe (a single Ctrl-A character) is captured in a shell variable named A. Another way to see what is going on is break it apart like this:

echo > emptyline
cat emptyline | tr '\012' '\001' > controlA
ls -l emptyline controlA
A=`cat controlA`

The ls command shows that both the emptyline and controlA files have a total size of one character. That character for emptyline is a newline (octal value 12), and the character for controlA is a Ctrl-A (octal value 1).

If you call each of these files up in vi, you will see nothing in emptyline because vi does not show the newline characters that terminate each line. But when you vi controlA, you will probably see the Ctrl-A character, represented on the screen as ^A. But there is a vi setting that tells whether it should visibly show non-graphical characters or not.

And you ask why he wants to use a non-printable character as a separator. If your old and new text strings are hard-coded, then of course you would know what they are, so you can use a separator (maybe a slash) that is not contained in either your old or new strings. But in this case, the old and new strings are coming in as variables $1 and $2. We do not know what these strings will be ahead of time. If you use a slash such as s/$1/$2/ then there will be an error if either $1 or $2 contains a slash.

So he wants to use a special separator that will never appear in a normal ascii data file.


Report Offensive Follow Up For Removal

Response Number 4
Name: nails
Date: December 14, 2006 at 12:06:31 Pacific
Subject: A=`echo | tr '\012' '\001' `
Reply: (edit)
James:

That's a most excellent explanation!



Report Offensive Follow Up For Removal

Response Number 5
Name: kamalpreet
Date: December 14, 2006 at 23:03:37 Pacific
Subject: A=`echo | tr '\012' '\001' `
Reply: (edit)
I get every BIT of it.Thanks for your time and effort for explaning it to me in such detail

Warm Regards
Kamalpreet Singh


Report Offensive Follow Up For Removal


Response Number 6
Name: James Boothe
Date: December 15, 2006 at 07:39:49 Pacific
Subject: A=`echo | tr '\012' '\001' `
Reply: (edit)
No problem, kamalpreet. And thanks nails for the feedback - means a lot.

Report Offensive Follow Up For Removal

Response Number 7
Name: James Boothe
Date: December 15, 2006 at 08:58:20 Pacific
Subject: A=`echo | tr '\012' '\001' `
Reply: (edit)
And here is an alternative for capturing an ascii character into a variable. I will use a lowercase "a" instead of the non-printing Ctrl-A because a printable character is easier to test with.

First, note that the following does NOT work:

A="\141"
echo $A
\141
echo ${#A}
4

Above, I attempt to store the letter a (as specified by its octal value 141) into the variable A.  But this actually stores the 4-character string \141, as shown by the first echo, and the second echo confirms that $A does in fact have a length of 4.

echo -e $A
a

Above, note that the echo -e option evaluates backslash-escaped character strings, so the output of this echo command is our desired letter a.  So we can take advantage of the echo conversion:

A=$(echo -e "\141")
echo $A
A
echo ${#A}
1

So the following will capture a Ctrl-A:

A=$(echo -e "\001")


Report Offensive Follow Up For Removal

Response Number 8
Name: nails
Date: December 15, 2006 at 10:46:38 Pacific
Subject: A=`echo | tr '\012' '\001' `
Reply: (edit)
Jim:

Since you've expanded the discussion on special characters: I once needed to know if a user pressed the TAB key and then pressed return. I deleted the TAB key from the string and if the resulting string were null, I knew the user had:

read variable # enter TAB key and press return

if [[ -z $(echo "$variable"|tr -d '\011') ]]
then # this statement executes if user enters a tab key
echo "variable is a TAB key"
fi
# end stub

Maybe there's a better way .....


Report Offensive Follow Up For Removal

Response Number 9
Name: James Boothe
Date: December 19, 2006 at 08:34:42 Pacific
Subject: A=`echo | tr '\012' '\001' `
Reply: (edit)
We can capture a tab character into a variable if we first disable white space (spaces and tabs) as the word delimiter:

IFS=
tab=$(echo -e "\011")

or

IFS=
tab=$(echo -e '\t')

Then our check for a tab (and a script might have a bunch of these checks) is simple:

if [ "$variable" = $tab ] ...


Report Offensive Follow Up For Removal



Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: A=`echo | tr '\012' '\001' `

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




XP Installed to G?

exessive internet traffic

ZoneAlarm Question. Blocked Connect

Windows Live Messenger Problem

Delete $Uninstall after SP3 updates


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC