Adding a character

Hello experts.

I trying to achieve 2 things here. I'm trying to convert all of the host names to lower case and add an "m" to each hostname. Can anyone provide some guidance on what I can do? Your input is greatly valued!

Here are the desired results:

  1. read the host name from a file.
  2. convert from upper to lower case. ex SERVER > server
  3. add character "m" to the host name. ex. serverm
  4. write the desired output to a separate file called hosts.

---------- Post updated at 10:25 AM ---------- Previous update was at 10:23 AM ----------

# get the filename
#!/usr/bin/ksh
 
echo -n "Enter filename:"
read filename
 
# make sure the file exists for reading
if [ ! -f $filename ]; then
             echo "filename $filename does not exists"
fi
 
# convert upper to lower case and write to hosts file 
tr '[A-Z]' '[a-z]' <$filename > hosts
 
# add character m to the end of each hostname 
if [[ $filename = "hosts" ]]; then
 
             sed 's/./&m/6' hosts
             exit 1
fi

Post a sample of the input and desired output...

Input: SERVER
Output: serverm

Using awk

awk '{ print tolower($0) "m" }' filename
1 Like

Yoda,

Thank you for your reply. That was really helpfull but I'm still having difficulty writing the desired output to the file.

What difficulties are you having?

By the way you can redirect the output to another file: hosts

awk '{ print tolower($0) "m" }' filename > hosts
1 Like

This is in the ksh environment. Will it override the exiting file each time?

> hosts will overwrite each time you run.

>> hosts will append each time you run.

1 Like

Thank you Yoda! That did it!:slight_smile: