Adding a character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding a character
# 1  
Old 05-21-2013
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 ----------

Code:
# 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


Last edited by Scrutinizer; 05-21-2013 at 12:38 PM.. Reason: Removed spurious formatting
# 2  
Old 05-21-2013
Post a sample of the input and desired output...
# 3  
Old 05-21-2013
Input: SERVER
Output: serverm
# 4  
Old 05-21-2013
Using awk
Code:
awk '{ print tolower($0) "m" }' filename

This User Gave Thanks to Yoda For This Post:
# 5  
Old 05-21-2013
Yoda,

Thank you for your reply. That was really helpfull but I'm still having difficulty writing the desired output to the file.
# 6  
Old 05-21-2013
Quote:
Originally Posted by david_tech
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
Code:
awk '{ print tolower($0) "m" }' filename > hosts

This User Gave Thanks to Yoda For This Post:
# 7  
Old 05-21-2013
This is in the ksh environment. Will it override the exiting file each time?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding end of line character in the last record

Need to add end of line character to last record in a fixed width file. When i take the record length of each line, for all the records it gives example like 200 except the last line as 199. Due to this my other script fails. Please help me on this. (4 Replies)
Discussion started by: Amrutha24
4 Replies

2. Shell Programming and Scripting

Adding new line character

Hi All, i am taking end of records using tail command got the out is "END OF FILE. ROW COUNT: 10" and record count in the file is 3. while append the both data into test2.txt file,it's append like END OF FILE. ROW COUNT: 108 my output should be in the test2.txt file END OF FILE. ROW... (9 Replies)
Discussion started by: bmk
9 Replies

3. UNIX for Dummies Questions & Answers

Adding space after character using sed?

dears i have the data below, i want a command ( i think it should be sed) that add a space after the seconds as below : Jun 24 22:28:18966568406148@ Jun 24 05:47:35966555747744@ Jun 24 05:47:53966560825239@ Jun 24 06:07:52966541147164@ Jun 24 15:49:55966566478883@ thanks... (5 Replies)
Discussion started by: thehero
5 Replies

4. Shell Programming and Scripting

sort file adding extra character

HI all i have this script : #!/bin/bash sort /usr/tmp/"REPORT"$1 -o \ /usr/tmp/"SREPORT"$1 -k 1,7 -S 150 end of script now i'm doing this command : ls -lsgt *REPORT* 4 -rw-r--r-- 300 Sep 16 REPORT54784 4 -rw-r--r-- 301 Sep 16 SREPORT54784 as you can see the sorted file... (5 Replies)
Discussion started by: naamas03
5 Replies

5. UNIX for Dummies Questions & Answers

Adding a character

Hi, I cant seem to figure out how I add a character to an existing one. Heres my problem. So I have a line that looks like this: >56 584848392394958586858484849393 What I want to do is add Num in front of the 56. So basically I want to recognize the > and add Num in front of the... (6 Replies)
Discussion started by: kylle345
6 Replies

6. Shell Programming and Scripting

Adding a special character at the end of the line

I used following to add * at the end of the line in file1. It adds * at the end but has a space before it for some lines but some other lines it adds exactly after the last character. How do I take out the space ? sed 's/$/*/' file1 > file2 example: contents of file1 : ... (2 Replies)
Discussion started by: pitagi
2 Replies

7. Shell Programming and Scripting

adding a character in front of a line

Hi everyon, I am trying to search for a pattern in a file and add "//" to the begining of the file. lets say i want to comment out a line from my codes. the line that should be commented out contains the word "reset". i need to search for the word "reset" and comment out that specific line. is... (5 Replies)
Discussion started by: ROOZ
5 Replies

8. Shell Programming and Scripting

Adding character to spaces in a table

Hi All, I am trying to get the spaces in the below table to be fill up with a character " - ". For eg, coordinates 0202 is a space but i would want to fill up with " - ". Can anybody help ? Input: 04 D H ... (15 Replies)
Discussion started by: Raynon
15 Replies

9. Shell Programming and Scripting

Adding CTRL-M Character

Hi, I have file generated in HP-UNIX environment (for Example) with the line feed (i.e $): a$ b$ C$ Now, I need to append CTRL-M character at the end of each line, but the last line of the file should not have both CTRL-M and Line Feed (i.e ^M and $). The file should look like: a^M$ b^M$... (2 Replies)
Discussion started by: mohan.nadaraja
2 Replies
Login or Register to Ask a Question