I would like to replace "CC" with "C" and "AA" with A" in a particular column of Tab delimited file (using awk probably).
-
My input data file abc.txt has this "20131008","0004","0568","98" I ran this command to replace the text nawk -F, -vOFS=',' '{gsub("0568","0808",$3); print }' abc.txt In the output the delimiter "," is replaced with a space. The output looks like this "20131008" "0004" "0568" "98". Can someone please help with this? I tried to replace the -vOFS but doesnt work.user3213938– user32139382014-08-19 01:14:24 +00:00Commented Aug 19, 2014 at 1:14
3 Answers
awk -F'\t' -vOFS='\t' '{ gsub("CC", "C", $1) ; gsub("AA", "A", $1) ; print }'
Replace $1 with the column that you wish to modify.
-
In case the delimiter has to be kept, a
-vOFS='\t'option may be added.manatwork– manatwork2012-06-14 10:18:01 +00:00Commented Jun 14, 2012 at 10:18 -
@manatwork Good point, added.Chris Down– Chris Down2012-06-14 10:20:05 +00:00Commented Jun 14, 2012 at 10:20
-
fantastic! works as per usual!!!alex– alex2012-06-14 10:51:58 +00:00Commented Jun 14, 2012 at 10:51
-
Some versions of
awkdon't have agsubfunction. How would you do it then? Cansedbe used instead?rahmu– rahmu2012-06-14 13:33:26 +00:00Commented Jun 14, 2012 at 13:33 -
1@rahmu -
gsub()has been defined by POSIX for over 20 years, so my suggestion would be to get a version of awk that isn't downright awful ;-)Chris Down– Chris Down2012-06-14 13:40:33 +00:00Commented Jun 14, 2012 at 13:40
If you didn't have access to gsub(), but you do have access to split(), you could just create the equivalent thusly:
Given the input
AA AA CC CC AA CC
the following awk script
BEGIN {
OFS = "\t";
split("1 3 5", Fields);
split("A C", Replacements);
}
{
for (i in Fields) {
for (j in Replacements) {
Replace = Replacements[j];
sub(Replace Replace, Replace, $Fields[i]);
}
}
print;
}
would produce the desired results for fields 1, 3, and 5:
A AA C CC A CC
Even without split() it's possible if you want to hard-code the Fields and Replacements arrays in the BEGIN block.
To find and replace in one field use this command:
sed 's/whatyouwanttofind/whatyouwanttoreplace/field#'
i.e. you echo this:
$echo -e "1are,2are,3are,4are\n5are,6are,7are,8are"
1are,2are,3are,4are
5are,6are,7are,8are
and you want to do this
$echo -e "1are,2are,3are,4are\n5are,6are,7are,8are" | sed 's/are/arrr/2'
1are,2**arrr**,3are,4are
5are,6**arrr**,7are,8are