Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Append without Carriage Return

Status
Not open for further replies.

TSch

Technical User
Jul 12, 2001
557
DE
Hello everyone,

I understand that using >> I'm able to append lines to a file ...

Is it also possible to append words to the same line in the file somehow ?

e.g.:

hostname >> /file
cat /etc/hosts | grep 172 | awk '{ print $1 }' >> /file

This would generate let's say the following file content:

server
172.16.238.34

But I want it to look like this when I do a "cat /file":

server 172.16.238.34

Is it possible ?

Regards
Thomas
 
workaround: use command substitution, then finally echo one line into /file

[tt]name=$(hostname)
addr=$(grep 172 /etc/hosts)

echo "$name $addr" > /file[/tt]




HTH,

p5wizard
 
Do you need the interemediate varaiables?
Code:
echo $(hostname) $(grep 172 /etc/hosts)
works for me.

Ceci n'est pas une signature
Columb Healy
 
Of course Columb, but I don't know what else Thomas wants to do with the strings, I assmume he just used this as a trivial example.

Other solution is to use printf or no-newline echo in the first instance

[tt]printf "%s " $(hostname) >> /file

grep whatever /etc/hosts >> /file[/tt]

or

[tt]echo "%s \c" $(hostname) >> /file

grep whatever /etc/hosts >> /file[/tt]



HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top