Operating System - HP-UX
1832207 Members
2993 Online
110040 Solutions
New Discussion

question on replacing blanks with other values

 
SOLVED
Go to solution
Narasimham
Advisor

question on replacing blanks with other values

How is it possible(in awk or shell programming) to replace blank lines in a file with some other charecter like "0".


Thanks
Narasimham
14 REPLIES 14
Pete Randall
Outstanding Contributor

Re: question on replacing blanks with other values

sed 's/" "/"0"/'

Pete

Pete
Paula J Frazer-Campbell
Honored Contributor

Re: question on replacing blanks with other values

Hi

This Word doc should help:-

Paula

If you can spell SysAdmin then you is one - anon
Pete Randall
Outstanding Contributor

Re: question on replacing blanks with other values

Oops, sorry, don't need quotes: sed 's/ /0/'

Pete

Pete
Paula J Frazer-Campbell
Honored Contributor

Re: question on replacing blanks with other values

Hi Pete

Sorry that will replace blank spaces - question is blank LINES ?

A blank line could contain many spaces or unprintable chars.


Perhaps an example of the file will help more - can you attach one?

Paula
If you can spell SysAdmin then you is one - anon
Pete Randall
Outstanding Contributor

Re: question on replacing blanks with other values

Geez, I'm sorry. I wish I could blame it on Monday but that's not even close. But thanks for pointing it out, Paula.

Pete

Pete
Shahul
Esteemed Contributor
Solution

Re: question on replacing blanks with other values

Hi

This command will replace all blank lines in a file to "y".

#sed s/^$/y/g file

Here "s" is for replacing
"^$" represents blank line
"g" is for global replacement. If g is not there, it will replace only the first occurence of blank line.

I hope now U are clear.

Best of luck
Shahul
James R. Ferguson
Acclaimed Contributor

Re: question on replacing blanks with other values

Hi:

Treating blanks, tabs and newline characters and their combinations as "blank" lines, you can do:

# sed -e 's/^[ ]*/0/'

Note that this is typed as "[", a blank (space) character, a tab character and a "]".

Regards!

...JRF...
#
MANOJ SRIVASTAVA
Honored Contributor

Re: question on replacing blanks with other values

Hi Narsimhan

cat filenaem | sed -e "1,$ s / /0/g "

the tip is that try it in vi mode and then use the same string in quotes with sed -e , most of the time it works cool.


Manoj Srivastava
A. Clay Stephenson
Acclaimed Contributor

Re: question on replacing blanks with other values

Plan B.

tr ' ' '0' < oldfile > newfile
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: question on replacing blanks with other values

Sorry, forget my Plan B. I read your title rather than reading the question.
If it ain't broke, I can fix that.
Pete Randall
Outstanding Contributor

Re: question on replacing blanks with other values

Try blaming it on Monday, Clay.

;^)

Pete

Pete
Paula J Frazer-Campbell
Honored Contributor

Re: question on replacing blanks with other values

Hi Pete

Isn't it Monday then ----

It certainly feels like it.


Paula
If you can spell SysAdmin then you is one - anon
Pete Randall
Outstanding Contributor

Re: question on replacing blanks with other values

Hi Paula,

I was off yesterday. Maybe that's why it seems like Monday but then, don't they all? I didn't think that would disable my ablity to read my native language but you never know, I guess.

Pete

Pete
James R. Ferguson
Acclaimed Contributor

Re: question on replacing blanks with other values

Hi (again):

Oops, Monday is must be for me too! I dropped the "$" anchor:

# sed -e 's/^[ ]*$/0/'

Again, note that this is typed as "[", a blank (space) character, a tab character and a "]".

By the same token:

# sed 's/^$/0/g'

...should also be:

# sed 's/^$*/0/g'

Regards!

...JRF...