Using Echo

Hi All,

Need help in resoving the issue . Can anyone let me know how to get fix length using unix shellscript.

For Ex: echo "NUMBER OF RECORDS "${NO_OF_ROWS}"\nFILE CREATION DATE&TIME "${PROD_DT}"

output should be :

NUMBER OF RECORDS 2546
CREATIN DATE&TIME 2009-12-01

Each record created in Unix should be of 80 byte length

Kindly help me out on the same

Take the spurious quotes out of the middle of the echo, and put the second line of output in a second echo. That should produce the output you need.

You'll need to clarify what you mean by "Each record created in Unix should be of 80 byte length" as nothing in your question implies you are creating anything at all...

The script will be easier to read as two commands:

echo "NUMBER OF RECORDS ${NO_OF_ROWS}"
echo "FILE CREATION DATE&TIME ${PROD_DT}"

I too don't understand "Each record created in Unix should be of 80 byte length".

Are you trying to create text files in which each line has exactly 80 characters?

The echo command seems to automatically strip off leading and trailing whitespace, which thwarts you. But this should work:

IFS= echo "Whatever ...                            " > outfile

The IFS variable is very special. The echo command consults it before interpreting whitespace. I've made it blank in the above example.

If you are counting lines in a file, use the wc command.

wc -l file

Or if you still want to use echo, use -e to enable interpretation of backslash escape sequences.

$ echo -e "first line\nsecond line"
first line
second line

Hey,

This is what i am looking for ... i have a shellscript whcih creates a report. Report has got 5 rows and each row should start from byte 1 and end at byte 80 . This should follow for the remainign 5 rows ..... hope this explains .....

Did you mean like this?

printf "%-73s%-7s" "hello" "goodbye"
hello                                                                    goodbye

Methyl,

This hold goods if we are using printf .... but can we get this out using 'ECHO'

  • As Ken has already shown, you can do this with echo easily enough.
    (It might require a -e flag or setting IFS depending on your implementation of echo, but it works with neither on /usr/bin/echo for Solaris 9)
  • wc -c will list the number of characters (in your situation that works for bytes too).
  • Integer maths in shell can be done using expr (eg `expr 80 - /usr/bin/echo "${myoutput}\c" | wc -c`)
  • You can generate a string of X bytes using echo and a for-do loop.

Let us know which parts of the solution remain a problem for you and we can elaborate as required. :slight_smile:

But really, that's pretty convoluted when printf does the job out of the box. Just for my own curiosity, why do you need to use echo?

It makes me wonder; Is this a homework question? If so we can still help but you need to move this thread (ask a moderator) and fill out the homework question template.

Boss,

If this is a homework question .... am in dumb to post it here :wink: ...... anyway still my question is un-answered ... let me give it out ...

Current scenario:

$var_datafile = MYFILE.TXT

PROD_DT=`date '+%Y%m%d %H:%M'`
NO_OF_ROWS=`cat "$var_datafile"|wc -l|tr -d ' '`

echo "NUMBER OF RECORDS "${NO_OF_ROWS}"\n
FILE CREATION DATE&TIME "${PROD_DT}"

Criteria:

  • Each row should be of 80 byte length...
  • Even though the value gets changed for $NO_OF_ROWS , but the byte length must remain constant .
 
typeset -L80 NO_OF_ROWS="NUMBER OF RECORDS "=`cat MYFILE.TXT |wc -l|tr -d ' '`
typeset -L80 PROD_DT="\nFILE CREATION DATE&TIME "`date '+%Y%m%d %H:%M'`
echo "${NO_OF_ROWS}${PROD_DT}"

Assume you use ksh.
Each row isalso 80 bytes.

I too think this is a homework question.
Let me watch.

@samtel

Explain why you can't live with a simple printf or any of the other suggestions given, ty.

If this is not homework, then why must you use echo v. printf?

Only homework questions require such things. Real world work problems do not require echo, they require the correct commands / tools for the job.

I am going to close this thread unless Samtel explains exactly why he cannot use printf (or anything other than echo).

Come to think of it, I fully concur with you Neo.
I think I should not have given the full answer.
Wth out the printf typeset is the only alternative.
And I think that is what they were looking for the students to come up with.
:mad:

Guys,

relax..... i can pretty well use printf ...... i tried with printf and found solution .

echo "$(print - $var_NO_OF_ROWS | sed 's/.*/& /')"|cut -c 1-80 >> sample.txt

Thanks a lot guys for ur help .....