sed adding a blank line

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed adding a blank line
# 1  
Old 09-10-2011
sed adding a blank line

I use the following as part of a script to correct for a faulty hostname file.

# get the domain name
read -r thehostname < /etc/hostname
dom="$(echo $thehostname | cut -d'.' -f2)"
if [ "$numchar" -le 5 ]
echo "It appears as though the hostname is not correctly set."
echo "Hostname has $thehostname as your hostname." 
echo "Please enter the domain name"
read realdom
sed -i 1c"$thehostname" /etc/hostname

The code works fine, but it always adds a blank line at the end of the hostname file. (the extra is noticed by an additional byte as well as in notepad++.)

It does not appear to be a real problem, since it doesnt actually affect anything else. But I would like the script to be more precise and not add anything.

Am I using sed incorrectly? Is there a switch that would prevent it from adding the newline?
# 2  
Old 09-10-2011

Your 'sed' command seems ok. What are the contents of /etc/hostname and $thehostname just before it?

# 3  
Old 09-10-2011
Originally Posted by birei

Your 'sed' command seems ok. What are the contents of /etc/hostname and $thehostname just before it?

Thanks for the response.

/etc/hostname contains "" only with no trailing space or line.

This is the top of the script, so $thehostname is only declared and filled here.

I have manually edited /etc/hostname many times in testing this. As soon as I run this script, it fixes the incorrect hostname, but always adds a trailing newline.
# 4  
Old 09-11-2011
Sorry, but I can not reproduce your problem. See:
$ echo "" >hostname
$ cat hostname
$ cat
#!/usr/bin/env bash

sed -i 1c"$thehostname" ./hostname
$ bash
$ cat hostname

# 5  
Old 09-11-2011
the extra is noticed by an additional byte as well as in notepad++
Cygwin? And '/etc/hostname' was edited in notepad++ too?
# 6  
Old 09-11-2011
Thanks guys. I think it may just be the way that linux does things. When I edit in nano, it shows the line, but nothing changes when I delete it.

Cat does not show the extra line at all anyway.

If I edit it in windows in notepad++, then it shows and can be deleted resulting in the lowering of the file size by one byte.

Thank you all.

---------- Post updated at 11:10 PM ---------- Previous update was at 11:08 PM ----------

Originally Posted by yazu
Cygwin? And '/etc/hostname' was edited in notepad++ too?
No, I dont use cygwin. I use windows at home and modify a remote vps server using winscp, putty, notepad++, nano etc.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding a blank line in between two O/Ps in tabular format which gets received over email

Hi Guys, I am stuck in between and seeking help here. Requirement: A script that will run every morning which will connect to Mysql database and run the query to inform us about the holidays (it will also check if there were any holidays during last 2 business days). So the three queries are... (11 Replies)
Discussion started by: Sambit Sahu
11 Replies

2. Shell Programming and Scripting

sed - How to insert line before the first blank line following a token

Hello. I have a config file (/etc/my_config_file) which may content : # # port for HTTP (descriptions, SOAP, media transfer) traffic port=8200 # network interfaces to serve, comma delimited network_interface=eth0 # set this to the directory you want scanned. # * if have multiple... (6 Replies)
Discussion started by: jcdole
6 Replies

3. Shell Programming and Scripting

Adding a blank line after every 5th line

Hello... I have a file which contain certain number of records. I want to generate another file from this file which will contain 1st line as a blank line & after every 5 lines one blank line will be inserted. How to achieve this through shell scripting? Thanks... (5 Replies)
Discussion started by: 46019
5 Replies

4. Shell Programming and Scripting

Fill the empty line by adding line before blank line

FIle A "A" 2 aa 34 3 ac 5 cd "B" 3 hu 67 4 fg 5 gy output shud be A"" 2 aa 34 "A" 3 ac 34 "A" 5 cd 34 "B" 3 hu 67 "B" 4 fg 67 "B" 5 gy 67 (6 Replies)
Discussion started by: cdfd123
6 Replies

5. Shell Programming and Scripting

SED - adding blank line after each Range Match

the following range matching works great but i wish to add a blank line after each range result set... which i've tried and researched to no avail MY INPUT DATA: CURRENT CODE I'M USING: sed -n '/*$/,/;/p' $INPUT_FILE RESULTS I'M GETTING: RESULT I looking to... (5 Replies)
Discussion started by: danmauer
5 Replies

6. Shell Programming and Scripting

Need help in sed command (adding a blank line btw each block generated by pattern)

Hello friends, I have a C source code containing sql statements. I use the following sed command to print all the sql blocks in the source code.... sed -n "/exec sql/,/;/p" Sample.cpp The above sed command will print the sql blocks based on the pattern "exec sql" & ";"... (2 Replies)
Discussion started by: frozensmilz
2 Replies

7. Shell Programming and Scripting

sed: print out to first blank line

I am trying to use wget and sed to extract a text based weather forecast for the Greater Victoria area only, this is one paragraph of many in a web page. I have tried /^$/ but that does not seem to work. So far I get mostly what I want with this: wget -qO -... (2 Replies)
Discussion started by: lagagnon
2 Replies

8. Shell Programming and Scripting

Remove blank line - SED

HI, I have this list of apps like so: DivX DivX Uninstall DivX for Build Install Flash Player 8 ... (10 Replies)
Discussion started by: pcwiz
10 Replies

9. Shell Programming and Scripting

sed: delete regex line and next line if blank

Hi, I want to write a sed script which from batiato: batiato/giubbe: pip_b.2.txt pip_b.3.txt pip_b.3mmm.txt bennato: bennato/peterpan: 123.txt consoli: pip_a.12.txt daniele: (2 Replies)
Discussion started by: one71
2 Replies

10. UNIX for Dummies Questions & Answers

adding blank line in egrep

I followed the egrep example given in the thread "parse text or complex grep ?". It is exactly what I need...except... how do I insert a blank line after the second line? My exact command is: egrep 'patt1|patt2' filename the result is: patt1 patt2 patt1 patt2 and so on. I would... (2 Replies)
Discussion started by: antalexi
2 Replies
Login or Register to Ask a Question