Pattern Replace using sed or awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pattern Replace using sed or awk
# 1  
Old 05-31-2011
Pattern Replace using sed or awk

Hi ,

My file have data like

Code:
4:ALMOST NEVER PR                 1925836
5:NEVER PR W DDA 5857610
6:NEVER PR WO DDA   26770205

but i want to replace the spaces before last numric digits out put should be like this
Code:
4:ALMOST NEVER PR=1925836
5:NEVER PR W DDA=5857610
6:NEVER PR WO DDA=26770205


I come up with this Smilie
Code:
sed 's|[ ]*[0-9]*$|=&|;s/=[ ]*/=/'

its working for me but the code look lousy any better way to do this

regards ,
max
# 2  
Old 05-31-2011
how about this?
Code:
awk '{$NF=$(NF-1)"="$NF}1' filename

# 3  
Old 05-31-2011
This one..?
Code:
sed 's/\(  *\)\([0-9][0-9]*\)/=\2/' inputfile > outfile

# 4  
Old 05-31-2011
RE :

hi Praveen thank you quick reply but

Code:
  echo "5:NEVER PR W DDA 5857610" | awk '{$NF=$(NF-1)"="$NF}1'

Gives
Code:
5:NEVER PR W DDA  DDA=5857610

DDA is repeating twice

hi michaelrozar

thank you
your code is working fine but if there is a space in the start of the line then it is failing
Code:
echo " 5:NEVER PR W DDA 5857610" | sed 's/\(  *\)\([0-9][0-9]*\)/=\2/'
=5:NEVER PR W DDA 5857610

# 5  
Old 05-31-2011
try this,
Code:
awk '{$(NF-1)=$(NF-1)"="$NF;$NF=""}1'  inputfile

This User Gave Thanks to pravin27 For This Post:
# 6  
Old 05-31-2011
Code:
 
sed 's/\(  *\)\([0-9][0-9]*$\)/=\2/' inputfile > outfile

This User Gave Thanks to xoops For This Post:
# 7  
Old 05-31-2011
thank praveen And Xoop

its working for me great Smilie

praveen can you explain how this awk is working

Xoop 1 question
Code:
 echo  5:NEVER PR W DDA 5857610 | sed 's/\(  *\)\([0-9][0-9]*$\)/(\1)(\2)'
5:NEVER PR W DDA( )(5857610)

i can't get why it matching last white spaces not the first (NEVER PR)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find pattern and replace using sed

Hi, i want to replace the following lines in such a way that if the word merge exists in first column it must replace the 3rd column as M and if parse exists in first column then the last column must P, if neither it must mark it as X. I have tried the solution using awk, but it is saying... (6 Replies)
Discussion started by: charlie87
6 Replies

2. UNIX for Beginners Questions & Answers

sed replace pattern

I have a file with multiple lines, all in the same format. For each line, I need to replace the sequence of digits after the last : with a new value, but keep the single quote at the end of the line. Example: Input: ( two lines of file) Name: 'text1:200/text2:1.2.3.4' Name2:... (19 Replies)
Discussion started by: Beginner101
19 Replies

3. Shell Programming and Scripting

sed find/replace a pattern, but not this one..

I've got a file like so: ...lots of lines, etc. push "route 10.8.0.0 255.255.255.0" push "route 192.168.1.123 255.255.255.0" ...lots of lines, etc. I want to sed find/replace the IP address in the second line, whatever it is, with a new IP address, but I don't want to touch the first line.... (5 Replies)
Discussion started by: DaHai
5 Replies

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

5. Shell Programming and Scripting

Sed/awk/perl command to replace pattern in multiple lines

Hi I know sed and awk has options to give range of line numbers, but I need to replace pattern in specific lines Something like sed -e '1s,14s,26s/pattern/new pattern/' file name Can somebody help me in this.... I am fine with see/awk/perl Thank you in advance (9 Replies)
Discussion started by: dani777
9 Replies

6. Shell Programming and Scripting

replace using pattern using awk or sed

I have file file_1.txt which contains: file_1.txt <tr> 1 MAIL # 1 TO src_1 </tr> <tr><td class="hcol">col_id</td> <td class="hcol">test_dt</td> <td class="hcol">user_type</td> <td class="hcol">ct</td></tr> <tr><td class="bcol">1</td> <td class="bcol">2012-09-20</td> <td class="bcol">a</td>... (2 Replies)
Discussion started by: sol_nov
2 Replies

7. Shell Programming and Scripting

Replace a pattern in a file with a generated number using sed or awk

my file has thousands of line but let me show what i want to achieve... here is one line from that file cat fileName.txt (2,'','user3002,user3003','USER_DATA_SINGLE',1,0,0,'BACKUP',2,NULL,0,450,NULL,NULL,'','2011-05-10... (13 Replies)
Discussion started by: vivek d r
13 Replies

8. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

9. Shell Programming and Scripting

How to replace the last pattern using sed?

myfile: AAAaaa BBBbbb CCCccc AAAeee DDDddd how to replace the last AAA as EEEEE using sed? like this: AAAaaa BBBbbb CCCccc EEEEEeee DDDddd (14 Replies)
Discussion started by: vistastar
14 Replies

10. Shell Programming and Scripting

SED Search Pattern and Replace with the Pattern

Hello All, I have a string "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031", and I just want to extract LLSV1, but I dont get the expected result when using the sed command below. # echo "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031" | awk '{print... (4 Replies)
Discussion started by: racbern
4 Replies
Login or Register to Ask a Question