Operating System - HP-UX
1752777 Members
6318 Online
108789 Solutions
New Discussion юеВ

Removal of character # from a file

 
SOLVED
Go to solution
Manoj1
Advisor

Removal of character # from a file

Hi All,

I have a file which contains some data as below:-

pwd ##
date #
uptime ###
ls -lrt ##
time ##

How to see content of the file with cat command without # Character from each line ?

Thanks,
Manoj

9 REPLIES 9
Steven Schweda
Honored Contributor
Solution

Re: Removal of character # from a file

man sed

sed -e 's/#//g' < a_file

> [...] with cat command [...]

If you insist:

cat a_file | sed -e 's/#//g'
Manoj1
Advisor

Re: Removal of character # from a file

Actually there are some characters after # and ##. I don't want to see characters after # sign.

pwd ##pa;afsaf##
date #alsf'amsdf#
uptime ###afaf
ls -lrt ##asfsdf
time ##tab;ojd;afa

I want to see output as below:-
pwd
date
uptime
ls -lrt
time

Steven Schweda
Honored Contributor

Re: Removal of character # from a file

> Actually [...]

Actually, it's often helpful to ask the
actual question whose answer you want, rather
than some other question whose answer is not
very useful to you. (Especially if you can't
adapt the useless answer to provide the
desired answer.)

> man sed

Still might be useful.

> [...] I don't want to see characters after
> # sign.

sed -e 's/#.*//' < a_file

s = substitute

#.* = "#", followed by one or more of any characters.

So, 's/#.*//' substitutes nothing for (the
first) "#", followed by one or more of any
characters.

Did I remember to suggest "man sed"?
Dennis Handly
Acclaimed Contributor

Re: Removal of character # from a file

>Steven: Did I remember to suggest "man sed"?

And see also regexp(5) where it discusses Regular Expressions.
Steven Schweda
Honored Contributor

Re: Removal of character # from a file

> And see also regexp(5) [...]

Of course, if you get to the bottom of the
"man sed" results, then that's already taken
care of, right?

[...]
SEE ALSO
awk(1), ed(1), grep(1), environ(5), lang(5), regexp(5), standards(5).

sed: A Non-Interactive Streaming Editor tutorial in the Text
Processing Users Guide.
Ron Barak
Advisor

Re: Removal of character # from a file

Try:

cat FILE_NAME | awk -F# '{ print $1 }'
James R. Ferguson
Acclaimed Contributor

Re: Removal of character # from a file

Hi:

> cat FILE_NAME | awk -F# '{ print $1 }'

While I like this as yet another way to meet the goal, the use of the 'cat' process to read an input file and open a pipe for 'awk' to read that, is wasteful.

Simply do:

# awk -F# '{ print $1 }' FILE_NAME

...which is why Steven originally wrote, "If you insist" when he used 'cat'.

Regards!

...JRF...
Kees C
Visitor

Re: Removal of character # from a file

cut -d'#' -f1 FILENAME
Joseph Steinhauser
New Member

Re: Removal of character # from a file

Frequently I will use this:

sed 's/#.*//;/^[ ]*$/d'
to display a script or config file without comments. (also removed blank lines)

For the same thing in awk, you can also use:

awk 'BEGIN{FS="#"}$1{print $1}'
Note: these methods will also cut code lines if "#" char is inside a quoted string, so they are not 'safe' for stripping comments from script/program code.

For your purpose, sed 's/#.*//' will work.
Old School efficiency. New World Tools.