Removing last character of a specific line from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing last character of a specific line from a file
# 1  
Old 06-16-2015
Removing last character of a specific line from a file

Hello guys,

I would need to remove the last character ")" of a specific line. This can be from any line. Your help is appreciated. Below is the line.

Code:
HOSTNAME=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP))

Please help.

Last edited by Don Cragun; 06-16-2015 at 04:24 PM.. Reason: Add CODE tags.
# 2  
Old 06-16-2015
Hello sang8g,

Welcome to forum, please use code tags for commands/codes/Inputs which you show us in your posts. For your request you can try following.
i-
Code:
 a="HOSTNAME=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP))"
 echo ${a%)}

Output will be as follows.
Code:
 HOSTNAME=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)

ii- If line is every time ending with character ) then
Code:
 awk '{sub(/\)$/,X,$0);print}' Input_file

Output will be as follows.
Code:
 HOSTNAME=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)

Hope this may help you.


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 06-16-2015
Hello Ravinder Singh,
Thank you for your response. But yeah in a normal scenario. it works. But actually i am trying to do this in a script, so my script has the below lines

Code:
cp /root/new/test.prop /root/sid/
su -l suadm <<EOF

source /root/sid/test.prop

after sourcing it connecting it using sqlplus by taking the conn strings from test.prop file.
so in the test.prop, ")" extra character should be removed and then source this test.prop file and conn using sqlplus.

Last edited by Don Cragun; 06-16-2015 at 04:26 PM.. Reason: Add CODE AND ICODE tags.
# 4  
Old 06-16-2015
Hello sang8g,

Not sure if I understand requirement completely but why should a tnsnames(actual file name for oracle) or test.prp file will NOT have complete pair of ) braces at first place. If still that is correct entry then could you please be more clear in your requirement because in my first post I have suggested one awk solution which can help you if you only want to remove character ) and get the values.


Thanks,
R. Singh
# 5  
Old 06-17-2015
actually my requirement is that i should open the test.prop file and remove the last character ")" from a particular line and then save and quit the file. this should be done by a script itself.
# 6  
Old 06-17-2015
Hello sang8g,

Yes, following may help you in same.
Code:
awk '{sub(/\)$/,X,$0);print}' Input_file > temp_input_file
mv temp_input_file Input_file

There is one more option sed -i but above option is more convenient. Hope this helps.

NOTE: You can change files names Input_file and temp_input_file accordingly.


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 7  
Old 06-18-2015
Thanks Ravinder. That works !!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

2. Shell Programming and Scripting

Insert character at specific location in a each line of the file

Hi All, I am trying to write a shell script where it should insert character 'I' in 180th position of each line(except first and last line) of the file. Below is the script for file in /home/test/bharat/*.RET do # Process file echo "File Name=" $file #l_fileName="${file##*/}" ... (19 Replies)
Discussion started by: bharath561989
19 Replies

3. Linux

Removing a character at specific position in a column

Hi, I have a file like this (about 8 columns in total, this being the 2nd column) gi_49482297_ref_YP_039521.1_ gi_49482297_ref_YP_039521.1_ gi_49482315_ref_YP_039539.1_ gi_49482315_ref_YP_039539.1_I want to remove the _ at the end of the line. And at later stages I would want to replace the... (5 Replies)
Discussion started by: Syeda Sumayya
5 Replies

4. Shell Programming and Scripting

Read file from nth line to specific character

Hi, I want to read the file from nth line (where n is an integer) to until I encounter @ char. Can any one please help me how to do this? Thanks. (3 Replies)
Discussion started by: laalesh
3 Replies

5. Shell Programming and Scripting

Removing first and last character of line

Hi all, Please help me to remove first and last character of each line in a file. Thanks, Baski (5 Replies)
Discussion started by: baskivs
5 Replies

6. UNIX for Dummies Questions & Answers

Need help removing last character of every line if certain character

I need help removing the last character of every line if it is a certain character. For example I need to get rid of a % character if it is in the last position. Input: aaa% %bbb ccc d%dd% Output should be: aaa %bbb ccc d%dd I tried this but it gets rid of all of the % characters.... (5 Replies)
Discussion started by: raptor25
5 Replies

7. Shell Programming and Scripting

How to concat line first and then removing the ^M character

hi Someone can give me some clue or script in writing the below requirement I am having 5 or 10 files of unix files which contain ^M charactes. First we have to find ^M character and concat the line where it has broken and then we have to remove the ^M character from the uxix... (11 Replies)
Discussion started by: vsantoshusa
11 Replies

8. Shell Programming and Scripting

Removing last character from each line of file

How can I remove the last character from each line of a file? This must be done without "funny" characters, as I want to transfer the code to/from Windows. Any ideas? (17 Replies)
Discussion started by: cjhancock
17 Replies

9. UNIX for Dummies Questions & Answers

removing a character and addending to end in each line in a file

HI i am having a file this (sys19:pnlfct:/pfact/temp>) cat temp_sand 1234567890 1234567890 1234567890 1234567890 I want to make this file as (sys19:pnlfct:/pfact/temp>) cat temp_sand 1456789023 1456789023 1456789023 1456789023 just take the 2nd and 3rd position and put it... (5 Replies)
Discussion started by: arunkumar_mca
5 Replies

10. Shell Programming and Scripting

removing new line character

I'm appending header and trailer record for a binary file using echo "$header" > filename cat oldfilename >> filename echo "$trailer" >> filename The echo is introducing newline character after header and trailer.Please let me know is there any possibility to get rid of newline character. (2 Replies)
Discussion started by: ammu
2 Replies
Login or Register to Ask a Question