replace word

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace word
# 1  
Old 09-18-2011
replace word

Hi All,

I want to do some calculation in 5th field and then replace the original 5th value to calcualted new value:

1|2|3|4|one two three|test

Expecting ofile

I tried below commands and acheived.

awk -F"|"  '{print $5}' ifile | awk '{if (NF==3) {print $2$3} else if (NF==2) {print $2}}' > tempfile
paste -d "|" ifile tempfile | awk 'BEGIN {FS=OFS="|"} { $5=$NF;NF--;print }' > ofile

How can I acheive this with one awk command itself.

Thanks in advance.
# 2  
Old 09-18-2011
Like this?

 awk -F"|" '{ for(i=1;i<=NF;i++){t=gsub(/ /,"@",$i); if(t!=0){split($i,arr,"@"); printf arr[2]  arr[3] FS}else{printf $i FS} }}' inputFile

# 3  
Old 09-18-2011
awk -F\| '{x=split($5,a,OFS);for(i=1;++i<=x;){y=(y)?y a[i]:a[i]};sub($5,y)}1'

If is not what you want please elaborate.
# 4  
Old 09-18-2011
awk -F\| '{split($5,F," ");$5=F[2](F[3]==x?x:F[3])}1' OFS=\| file

awk '{sub(/[^|]*$/,x,$1)}1' OFS= file

Last edited by Scrutinizer; 09-18-2011 at 11:32 AM..
# 5  
Old 09-18-2011
Thanks Ahmed & Danmero,

Both your commands are working.

for sample I given below calculation in 5th field
awk '{if (NF==3) {print $2$3} else if (NF==2) {print $2}}'

But I wanted to do calclulation dynamically. My aim is to do some calculation in 5field and replace by the same.
# 6  
Old 09-18-2011
Originally Posted by Jairaj
But I wanted to do calculation dynamically. My aim is to do some calculation in 5field and replace by the same.
What is the logic for your calculation ? print only the last 2 words from $5?
awk -F\| '{x=split($5,a,OFS);sub($5,a[(x-1)]a[x])}1'

Last edited by danmero; 09-18-2011 at 07:04 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How do i replace a word ending with "key" using awk excpet for one word?

echo {mbr_key,grp_key,dep_key,abc,xyz,aaa,ccc} | awk 'gsub(/^|abc,|$/,"") {print}' Required output {grp_key,xyz,aaa,ccc} (5 Replies)
Discussion started by: 100bees
5 Replies

2. Shell Programming and Scripting

Shell Script @ Find a key word and If the key word matches then replace next 7 lines only

Hi All, I have a XML file which is looks like as below. <<please see the attachment >> <?xml version="1.0" encoding="UTF-8"?> <esites> <esite> <name></name> <storeId>10001</storeId> <module> ... (4 Replies)
Discussion started by: Rajeev_hbk
4 Replies

3. Shell Programming and Scripting

How to replace the word?

Hi Everybody, I am having a query like I want to change only first three patterns of the first line in a file. ex: I like unix unix unix unix is very much. after using the command it should be like I like linux linux linux unix is very much. Thanks, Naga (3 Replies)
Discussion started by: nagraju.allam
3 Replies

4. Shell Programming and Scripting

Find and replace a word in all the files (that contain the word) under a directory

Hi Everyone, I am looking for a simple way for replacing all the files under a directory that use the server "xsgd1234dap" with "xsdr3423pap". For Example: In the Directory, $pwd /home/nick $ grep -l "xsgd1234dap" *.sh | wc -l 119 I have "119" files that are still using... (5 Replies)
Discussion started by: filter
5 Replies

5. Shell Programming and Scripting

Replace a word in a string starting with another word

Hi All, I have a file in which a number of lines are starting with similar first word but different next words. I want to replace the any nth word(not 1st or 2nd) with another word. Eg:- My file contains are like this:- Ram is a boy. Ram is a good boy. Ram plays cricket. Here I want to... (2 Replies)
Discussion started by: mukeshbaranwal
2 Replies

6. Shell Programming and Scripting

How to replace a word with another word

Hello Friends, How to substitue with a word by another word in file written in VI Editor. Thanks & Regards Siva Ranganath 7760774961 (2 Replies)
Discussion started by: sivaranganath
2 Replies

7. Shell Programming and Scripting

Replace a word after a particular word in a file

Hi, I want to replace a word in a file which occurs after a particular word. For example : $cat file.txt CASE WHEN AND c1 = 'I' AND c2= '2' THEN 1 WHEN AND c1= 'I' AND c2= '0' THEN 2 So in this example i want to replace... (4 Replies)
Discussion started by: ashwin3086
4 Replies

8. Shell Programming and Scripting

Replace a word from a string

How can i replace a particular word from string i.e. var="shiv_dutt_para_shar" wrd="para" rep_wrd="PARA" what i am trying to do that first i'll search if $var catains #wrd or not. if it contains then i've to replace $wrd with $rep_wrd. I have tried following #!/bin/sh t="shiv... (5 Replies)
Discussion started by: jadoo_c2
5 Replies

9. Solaris

Replace word.

Dear I want to ask about replace word ( sharing word ) in files ( more than one files ) in one directory. Example:- I serach about ( ahmad ) in directory ( x ) i saw the results in ( 10 ) files ( differetn files ) i want to replace this word ( ahmad ) to ( Mohammad ) in one command. how i... (2 Replies)
Discussion started by: abu_hassan
2 Replies

10. Shell Programming and Scripting

how to replace a word with another

Hi All, v_notes = "Ravi & kumar & garlapati" i want out put like "Ravi and kumar and garlapati" What is the command to get this as output, we have to replace all & with and. Hope this is clear. Regards, Ravi Kumar Garlapati (1 Reply)
Discussion started by: rkrgarlapati
1 Replies
Login or Register to Ask a Question