[Solved] sed : last character replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] sed : last character replace
# 1  
Old 07-07-2011
[Solved] sed : last character replace

Hi all ,

I have to write a shell script that takes a number as input , like 123
and the output will be 6 ,i.e the output will be the sum of digits of the input.

I have an idea as follows,
Code:
echo "123"|fold -1|tr '\n' '+'|bc

But the problem is after " echo "123"|fold -1|tr '\n' '+' " the output is 1+2+3+ which is not working with bc.

Is there is any way to replace the last character of a line to "\n" ???

Thanking You ,
M.Choudhury Smilie

Last edited by Franklin52; 07-07-2011 at 04:26 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 07-07-2011
Code:
 
$ echo "123" | nawk ' BEGIN{FS=""} {for(i=1;i<=length($0);i++) {a=a+substr($0,i,1)}} END{print a}'
6

# 3  
Old 07-07-2011
Or:
Code:
echo "123" | sed -e 's/./&+/g' -e 's/.$//' | bc

This User Gave Thanks to Franklin52 For This Post:
# 4  
Old 07-07-2011
Alternatively..
Code:
echo "123"|fold -1|tr '\n' '+'| sed 's/+$/\n/'| bc
echo "123"|awk 'BEGIN{FS=""} {print $1"+"$2"+"$3}'| bc

This User Gave Thanks to michaelrozar17 For This Post:
# 5  
Old 07-07-2011
Code:
echo "123" | awk '$1=$1' FS= OFS=+ | bc

This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 07-07-2011
Thanks everyone ... Problem Solved Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] Replace character in 3rd column and leave 1rst and last

Hello to all, I have the following text where columns are separated by spaces. I want to have the 3rd column separating 3 strings with 2 "_" in the format below: LeftSring_CentralString_RightString So, in 3rd column I want to replace all "_" with "-", except the first and last "_" The... (5 Replies)
Discussion started by: Ophiuchus
5 Replies

2. Shell Programming and Scripting

[Solved] sed - how replace date in script

Hi All, I have a requirement to find and replace old date with new date value. Below is the scenario: # In the input file, date is MM/DD/YYYY format PREV_DTE=09/15/2013 I want to replace with 09/30/2013. It should look like PREV_DTE=09/30/2013 I am using below sed command :... (4 Replies)
Discussion started by: rockygsd
4 Replies

3. Shell Programming and Scripting

[Solved] sed to replace words

Hello All, I have file named filelist.txt a.bteq.ctl b.bteq.ctl c.bteq.ctl I want to replace the word bteq to tpt in this file. I used this sed command cat filelist.txt | sed 's/bteq/tpt/g' > filelist.txt But this command deletes all records from the filelist.txt Can... (2 Replies)
Discussion started by: nnani
2 Replies

4. Shell Programming and Scripting

[Solved] Find and replace till nth occurence of a special character

Hi, I have a requirement to search for a pattern in each line in a file and remove the in between words till the 3rd occurrence of double quote ("). Ex: CREATE TABLE "SCHEMANAME"."AMS_LTV_STATUS" (Note: "SCHEMANAME" may changes for different schemas. Its not a fixed value) I need to... (2 Replies)
Discussion started by: satyaatcgi
2 Replies

5. Shell Programming and Scripting

[Solved] SED - Bash - Inserting multi Tab character in the command

Hello. I am using : sed -i -e '/§name_script§/a#'"${MY_TAB11}"'# \ #'"${MY_TAB1}"'The Standard way'"${MY_TAB7}"'# \ #'"${MY_TAB1}"'==============='"${MY_TAB7}"'# \ ' "$CUR_FILE" Is there a better way to define "MY_TAB7","MY_TAB11" in other way than : MY_TAB1=$'\t' MY_TAB2=${MY_TAB1}$'\t'... (2 Replies)
Discussion started by: jcdole
2 Replies

6. Shell Programming and Scripting

In Sed how can I replace starting from the 7th character to the 15th character.

Hi All, Was wondering how I can do the following.... I have a String as follows "ACCTRL000005022RRWDKKEEDKDD...." This string can be in a file called tail.out or in a Variable called $VAR2 Now I have another variable called $VAR1="000004785" (9 bytes long), I need the content of... (5 Replies)
Discussion started by: mohullah
5 Replies

7. Shell Programming and Scripting

sed help,to replace the last character

cat input.txt agsbdafgd ertebrtreter ahrbrwerg The last character of a line that does not start with a would be changed to Z. Final output: agsbdafgd ertebrtreteZ ahrbrwerg Can anyone post the sed command to do that? (2 Replies)
Discussion started by: cola
2 Replies

8. Shell Programming and Scripting

Solved - Sed - Positional Replace

Hi All, I don't know what I am doing wrong in the regex below. I have the following string: 31000000010201005181121360000000100000000003000000YYY-YYY-YYY-20100518-104139.txt.YYY I need to split it in parts: - Group 1: 3100000001020100518112136 - Group 2: 000000010 - Group 3:... (0 Replies)
Discussion started by: felipe.vinturin
0 Replies

9. Shell Programming and Scripting

Sed Replace a repeating character

I have a text file and every line ends in |^ |^^ |^^^ |^^^^ I need to use sed to make all lines end it |^ regardless of the amount of carrots. The code i was using is: cat FILE | sed 's/\^\^\^/\^/g' But then they threw that curveball at me. Also is there a way to... (2 Replies)
Discussion started by: insania
2 Replies

10. Shell Programming and Scripting

replace first character of string sed

I want to change the first/or any character of a string to upper-case: String: test Desired results: Test or tEst or teSt or tesT thanks (6 Replies)
Discussion started by: prkfriryce
6 Replies
Login or Register to Ask a Question