sed command to replace a character at last


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed command to replace a character at last
# 8  
Old 06-08-2011
try this ..
Code:
$ echo "inform|10.232.167.18|1|1|50|25|0|0|0|5|1|1|78|0037|" | sed 's,|[0-9][0-9]|[0-9][0-9][0-9][0-9]|,|AA|AAAA|,g'

inform|10.232.167.18|1|1|50|25|0|0|0|5|1|1|AA|AAAA|


Last edited by Franklin52; 06-08-2011 at 11:06 AM.. Reason: Please use code tags, thank you
# 9  
Old 06-08-2011
Awk writes to the stdout.
If you don't want to create a temp file and must want to edit the original file. use perl (posted by getmmg).
# 10  
Old 06-08-2011
Quote:
Originally Posted by jayan_jay
try this ..
$ echo "inform|10.232.167.18|1|1|50|25|0|0|0|5|1|1|78|0037|" | sed 's,|[0-9][0-9]|[0-9][0-9][0-9][0-9]|,|AA|AAAA|,g'

inform|10.232.167.18|1|1|50|25|0|0|0|5|1|1|AA|AAAA|
or more simply

Code:
$ echo "inform|10.232.167.18|1|1|50|25|0|0|0|5|1|1|78|0037|" | sed 's:[^|]*|[^|]*|$:AA|AAAA|:'
inform|10.232.167.18|1|1|50|25|0|0|0|5|1|1|AA|AAAA|

# 11  
Old 06-08-2011
borrowing from anchal_khare:
Code:
{ rm file; awk -F\| '{$(NF-1)="AAAA";$(NF-2)="AA";print}' OFS=\| file; } < file

# 12  
Old 06-08-2011
Quote:
Originally Posted by vgersh99
borrowing from anchal_khare:
Code:
{ rm file; awk -F\| '{$(NF-1)="AAAA";$(NF-2)="AA";print}' OFS=\| file; } < file

I believe you meant to redirect AWK's stdout to "file". Otherwise, AWK will try to open ./file for reading, a pathname which does not exist after the rm.

As with any use of the unlink trick, should the last process holding open a file descriptor to the original "file" crash, you've lost the data (just a heads up for novices Smilie).

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 13  
Old 06-08-2011
Quote:
Originally Posted by alister
I believe you meant to redirect AWK's stdout to "file". Otherwise, AWK will try to open ./file for reading, a pathname which does not exist after the rm.

As with any use of the unlink trick, should the last process holding open a file descriptor to the original "file" crash, you've lost the data (just a heads up for novices Smilie).

Regards,
Alister
ooops, fat fingers - good catch, thanks!
Code:
{ rm file; awk -F\| '{$(NF-1)="AAAA";$(NF-2)="AA";print}' OFS=\| > file; } < file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command to replace two character pattern with another pattern

Not able to paste my content. Please see the attachment :-( (2 Replies)
Discussion started by: vivek d r
2 Replies

2. Shell Programming and Scripting

[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, echo "123"|fold -1|tr '\n' '+'|bc But the problem is after " echo "123"|fold -1|tr '\n' '+' "... (5 Replies)
Discussion started by: M.Choudhury
5 Replies

3. Shell Programming and Scripting

How do I replace a unicode character using sed

I have a unicode character {Unicode: 0x1C} in my file and I need to replace it with a blank. How would a sed command look like? cat file1 | sed "s/&#x28;//g;" > file2 Is X28 the right value for this Unicode character?? (4 Replies)
Discussion started by: Hangman2
4 Replies

4. Shell Programming and Scripting

how to replace the special character with another using SED

I have the replace the pattern in the file , ); to ); Could someone please help me to get this command. (2 Replies)
Discussion started by: mohan.bit
2 Replies

5. Shell Programming and Scripting

sed special character replace

I need to do the following: text in the format of: ADDRESS=abcd123:1111 - abcd123:1111 is different on every system. replace with: ADDRESS=localhost:2222 sed 's/ADDRESS=<What do I use here?>/ADDRESS=localhost:2222/g' Everything I've tried ends up with: ... (3 Replies)
Discussion started by: toor13
3 Replies

6. Shell Programming and Scripting

how to replace specific character , if possible using sed

My script is extracting data from SQl session, however sometimes the result contains one or multiple space after/before any numerical value. e,g . "123","1 34","1 3 45", "43 5" How to remove these unwanted spaces..so that I can get the following result : "123","134",1345","435" (1 Reply)
Discussion started by: mady135
1 Replies

7. 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

8. 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

9. Shell Programming and Scripting

replace two character strings by two variables with sed command

Hello, I want to writte a script that replace two character strings by two variables with the command sed butmy solution doesn't work. I'm written this: sed "s/TTFactivevent/$TTFav/g && s/switchSLL/$SLL/g" templatefile. I want to replace TTFactivevent by the variable $TTFav, that is a... (4 Replies)
Discussion started by: POPO10
4 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