| Author |
I need help on a command for my Unix class---PLEASE ! ?
|
|
|
| I've got a file that contains one line of text. The line looks like
this: 34
That's all. When I try to echo the results in a sentence,
I can't seem to ignore the leading white spaces!!
I think I've tried all the man pages. How can I delete the
white spaces and echo the result in a sentence?
This Project is due soon. Any help would be greatly
appreciated.
| |
| Davide Bianchi 2004-01-23, 4:50 pm |
| 1328 <fewiisdf@iop.com> wrote:quote:
> I think I've tried all the man pages. How can I delete the
> white spaces and echo the result in a sentence?
sed 's/[ ]\+//g' yourfilenamehere
man sed for more info
Davide
| |
| Dave Hinz 2004-01-23, 4:50 pm |
| On Sat, 18 Oct 2003 12:25:42 GMT, 1328 <fewiisdf@iop.com> wrote:quote:
> I've got a file that contains one line of text. The line looks like
> this: 34
> That's all. When I try to echo the results in a sentence,
> I can't seem to ignore the leading white spaces!!
Take a look at the man page for "tr" - perhaps you could change
those spaces to, say, nothing?
Dave Hinz
| |
|
| Thank you all for your help. The sed command worked. We haven't used
it class yet, but it works.
On 18 Oct 2003 12:54:32 GMT, Davide Bianchi
<davideyeahsure@onlyforfun.net> wrote:
quote:
>1328 <fewiisdf@iop.com> wrote:
>
>sed 's/[ ]\+//g' yourfilenamehere
>
>man sed for more info
>
>Davide
| |
|
| 1328 <fewiisdf@iop.com> wrote in message news:<r8c2pvc2dc6viqnp1dr5vqlefn2av7esl3@4ax.com>...quote:
> I've got a file that contains one line of text. The line looks like
> this: 34
> That's all. When I try to echo the results in a sentence,
> I can't seem to ignore the leading white spaces!!
> I think I've tried all the man pages. How can I delete the
> white spaces and echo the result in a sentence?
> This Project is due soon. Any help would be greatly
> appreciated.
Simpler is:
echo Number is `cat file_name` inside the file # Bourne shell
or
echo Number is $(<file_name) inside the file # Korn shell
This will print:
Number is 34 inside the file - look at the IFS variable in the shell's
environment. Be careful of quoting - this tendes to preserve white
space.
Bill
| |
| Matthew Thorley 2004-01-23, 4:53 pm |
| If your class allows you could use a Python (or perl) script to do this
better. Here is a script that I copied from the book UNIX POWER TOOLS:
#!/bin/sed -f
s/[ ][ ]*/ /g
The first set of brackets has a space the second a tab. Try
experimenting with this and hopefully it will work for you.
--matthew
1328 wrote:quote:
> I've got a file that contains one line of text. The line looks like
> this: 34
> That's all. When I try to echo the results in a sentence,
> I can't seem to ignore the leading white spaces!!
> I think I've tried all the man pages. How can I delete the
> white spaces and echo the result in a sentence?
> This Project is due soon. Any help would be greatly
> appreciated.
| |
| James T. Dennis 2004-01-23, 4:54 pm |
| Matthew Thorley <ruach@chpc.utah.edu> wrote:quote:
> 1328 wrote:
[QUOTE][color=darkred]
> If your class allows you could use a Python (or perl) script to do this
> better. Here is a script that I copied from the book UNIX POWER TOOLS:
quote:
> #!/bin/sed -f
> s/[ ][ ]*/ /g
quote:
> The first set of brackets has a space the second a tab. Try
> experimenting with this and hopefully it will work for you.
quote:
> --matthew
This regex requires a space followed by any number of tabs. It's
not anchored.
If you want to ensure that it's *leading* space and that you
effect any sequences of spaces and tabs I think it should look more
like:
s/^[ ]*/ /g
... where [...] atom is a class consisting of one space *and* one tab.
I don't think PERL or Python would perform this task any "better."
If you were already writing a PERL or Python script and *also* need to
perform this trivial operation then each has native methods for trimming
leading and trailing whitespace or other characters.
You might also try piping it through: tr -s '\t' ' '
(That's the tr command with the --squeeze-repeats option and the
two strings are a [Tab] and a space).
Of course that will affect *all* spacing on the line (not just
leading spaces) and it some older versions of tr might not support
the -s option.
--
Jim Dennis,
Starshine: Signed, Sealed, Delivered
|
|
|
|