change character(s) in specific column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting change character(s) in specific column
# 1  
Old 03-07-2011
change character(s) in specific column

Hi all!

I need to change the final e every time when it is present in any word in column 1 to a; moreover, to change the final i again to a in any word in column 1, but just if word in column 2 begins with ha or si.

Here below you can see a sample of my data:
Code:
achwa ungeliachwa ungeli 1
achwa ushaachwa usha 1
achwa vinaachwa vina 3
achwa vingeachwa vinge 1
achwa waachwa wa 1
achwa waliouachwa waliou 1
achwa yameachwa yame 1
achwa zilikwishaachwa zilikwisha 1
achwe akaachwe aka 1
achwe isachwe is 1
achwe niachwe ni 2
achwe visiachwe visi 2
achwe wasachwe was 1
achwi haachwi ha 2
achwi hachwi h 1
achwi haiachwi hai 2
achwi haviachwi havi 1
achwi hayaachwi haya 1
achwi haziachwi hazi 2
achwi siachwi si 2
dhani aliyoidhani aliyoi 2
dhani amdhani am 1

And here it is the result I need
Code:
achwa ungeliachwa ungeli 1
achwa ushaachwa usha 1
achwa vinaachwa vina 3
achwa vingeachwa vinge 1
achwa waachwa wa 1
achwa waliouachwa waliou 1
achwa yameachwa yame 1
achwa zilikwishaachwa zilikwisha 1
achwa akaachwe aka 1
achwa isachwe is 1
achwa niachwe ni 2
achwa visiachwe visi 2
achwa wasachwe was 1
achwa haachwi ha 2
achwa hachwi h 1
achwa haiachwi hai 2
achwa haviachwi havi 1
achwa hayaachwi haya 1
achwa haziachwi hazi 2
achwa siachwi si 2
dhani aliyoidhani aliyoi 2
dhani amdhani am 1

Thanks a lot!
mjomba
# 2  
Old 03-07-2011
Hi, Could this help you ?
Code:
awk '{$1=substr($1,length($1))=="e" || (substr($1,length($1))=="i" && ($2 ~ /^ha/ || $2 ~ /^si/))?substr($1,1,length($1)-1)"a":$1}1' inputfile

# 3  
Old 03-07-2011
Code:
awk '{if(substr($1,length($1),1)=="e") {$1=substr($1,1,length($1)-1)"a"}else{if(substr($1,length($1),1)=="i"&&(substr($2,1,2)=="ha"||substr($2,1,2)=="si")) {$1=substr($1,1,length($1)-1)"a"}}}1' file

or

Code:
sed -e 's/e /a /' -e 's/i ha/a ha/' -e 's/i si/a si/' file


Last edited by yinyuemi; 03-07-2011 at 03:30 AM..
# 4  
Old 03-07-2011
Code:
awk '$2 ~ /^ha|^si/ && $1 ~ /e$|i$/{$1=substr($1,1,length($1)-1)"a"}1' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using awk to change a specific column and in a specific row

I am trying to change the number in bold to 2400 01,000300032,193631306,190619,0640,1,80,,2/ 02,193631306,000300032,1,190618,0640,CAD,2/ I'm not sure if sed or awk is the answer. I was going to use sed and do a character count up to that point, but that column directly before 0640 might... (8 Replies)
Discussion started by: juggernautjoee
8 Replies

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

3. Shell Programming and Scripting

Change some string in specific column with space

Hello All of Master Script , i need help to solve my problem before : mount /dev/rdsk/c1t69d0s6 /dev/rdsk/c1t69d0s6 /vol/cl123/PURGE1 ufs mount /dev/rdsk/c1t70d0s6 /dev/rdsk/c1t70d0s6 /vol/cl123/PURGE2 ufs expected : mount /dev/dsk/c1t69d0s6 /dev/rdsk/c1t69d0s6 /PURGE1 ufs mount ... (3 Replies)
Discussion started by: k0p0nkkk
3 Replies

4. Shell Programming and Scripting

Add character to specific columns using sed or awk and make it a permanent change

Hi, I am writing a shell script where I want that # should be added in all those lines as the first character where the pattern matches. file has lot of functions defined a.sh #!/bin/bash fn a { beautiful evening sunny day } fn b { } fn c { hello world .its a beautiful day ... (12 Replies)
Discussion started by: ashima jain
12 Replies

5. Shell Programming and Scripting

Overwrite specific column in xml file with the specific column from adjacent line

I have an xml file dumped from rrd file, that I want to "patch" so the xml file doesn't contain any blank hole in the resulting graph of the rrd file. Here is the file. <!-- 2015-10-12 14:00:00 WIB / 1444633200 --> <row><v> 4.0419731265e+07 </v><v> 4.5045912770e+06... (2 Replies)
Discussion started by: rk4k
2 Replies

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

7. Shell Programming and Scripting

How to replace a character in a specific column in a file?

This is a file that I have test line 1 (55) ) test line 2 (45) ) I would like to change all the parens in position 1 of this file to a ); i only want to check position 1 in every line of the file. I have tried different varations of sed, but cannot seem to be able to limit it to... (1 Reply)
Discussion started by: JoeG
1 Replies

8. Shell Programming and Scripting

remove special character from a specific column

Hello , i have a text file like this : A123 c12AB c32DD aaaa B123 23DS 12QW bbbb C123 2GR 3RG cccccc i want to remove the numbers from second and third column only. i tried this : perl -pe 's///g' file.txt > newfile.txt but it will remove the number from... (7 Replies)
Discussion started by: shelladdict
7 Replies

9. Shell Programming and Scripting

How to extract first column with a specific character

Hi All, Below is the sample data of my files: O|A|571000689|D|S|PNH|S|SI sadm|ibscml1x| I|A|571000689|P|S|PNH|S|SI sadm|ibscml1x| O|A|571000689|V|S|PNH|S|SI sadm|ibscml1x| S|C|CAM|D|S|PNH|R|ZOA|2004 bscml1x| ... (3 Replies)
Discussion started by: selamba_warrior
3 Replies

10. Shell Programming and Scripting

How to change a specific character in a file

Hi, I have a data file with following structure: a|b|c|d|3|f1|f2|f3 a|b|c|d|5|f1|f2|f3|f4|f5 I want to change this data to: a|b|c|d|3|f1;f2;f3 a|b|c|d|5|f1;f2;f3;f4;f5 Data in column 5 tells the number of following fields. All fields delimiter after the 5th column needs to be... (6 Replies)
Discussion started by: sdubey
6 Replies
Login or Register to Ask a Question