Creating values based on position

How i create a value by comparing the data from specific field and fetch particular character from other column.
For example i want to compare the 3rd field and fetch 4th character from last column, say if 3rd column have 'bcd' then fetech "i" and append the same "i" as last column if data doesn't match then do nothing. all columns separated by delimiter comma.

abcd,123,bcd,4342337,sjeikfmerielafllfoe
das,344,edc,43453,erewmferm
mefefe,242,bcd,4354646757,kewjfoefgomlrewfp

Required out put

abcd,123,bcd,4342337,sjeikfmerielafllfoe,i
das,344,edc,43453,erewmferm
mefefe,242,bcd,4354646757,kewjfoefgomlrewfp,j

The usual questions:

Which OS are you using?
What are your preferred tools/utilities/shells/languages?

And most of all what have you tried?

1 Like

Thanks for the reply.
Linux 4.4.180-94.113-default #1 SMP Fri Dec 13 14:20:57 UTC 2019 (c6649f6) x86_64 x86_64 x86_64 GNU/Linux

I tried to fetch 3rd column using awk and cut , but having difficulty to compare the data with 'bcd' and fetching specific character from last column and append as last column with delimiter.

cut -d"," filename | sort -u

Hi
The string must be changed. Tools like awk, sed, and a shell script in the built-in programming language are suitable
This example on 'sed'

sed -r 's/([^,]*,){2}bcd,.*,(.){4}.*/&,\2/'

REMARK:
I forgot to indicate the anchor ^ of the beginning of the line,
only then the pattern of 'bcd' will exactly correspond to the third field by the comma separator

sed -r 's/^([^,]*,){2}bcd,.*,(.){4}.*/&,\2/'
1 Like

And, the .* would allow for one or more comma-separated fields, both in the middle as well as at line end. You might want to anchor the regex at line end, too. Proposal (untested)

sed -r 's/^([^,]*,){2}bcd,.*,([^,]){4}[^,]*$/&,\2/'
1 Like

Hi, @RudiC

sed -r 's/^([^,]*,){2}bcd,.*,(.){4}.*/&,\2/'

The marked greedy expression contains all the available fields except the last one,
which means that in the positions noted by you, the comma simply cannot appear.
In addition, this makes the pattern more flexible.
There is no condition in the task how many fields will be, there are only 3 fields and the last.
The last greedy expression also makes the end of line anchor '$' unnecessary.
I apologize in advance if I could not convey my thinks accurate,
but of course I could be wrong
Thanks

--- Post updated at 17:18 ---

Just what you specify would make sense with the hard setting of the number of fields

sed -r 's/^([^,]*,){2}bcd,[^,]*,([^,]){4}[^,]*$/&,\2/'
1 Like

@nezabudka: You are right if the last field is well-formed, i.e. >= 4 chars. Compare the versions for the case of last field < 4 char...

1 Like

Try:

awk -F, '$3=="bcd"{$0=$0 FS substr($NF,4,1)}1' file
1 Like

Thanks for helping out. Solution worked perfectly. Is there a way to fetch and create value from last column of 9th char if 3rd column is not "bcd",it would be better if we can add both condition in one command. I tried with "!" but that doesn't help.

Hi, @zooby
pay attention to the previous post #8
it will be enough for you to replace == with != and instead of 4 use the number 9
if of course I understood everything correctly

--- Post updated at 16:59 ---

Together it will look like this

awk -F, '
$3=="bcd"    {$0=$0 FS substr($NF,4,1)}
$3!="bcd"    {$0=$0 FS substr($NF,9,1)}
1' file

--- Post updated at 17:22 ---

sed -r 's/^([^,]*,){2}bcd,.*,(.){4}.*/&,\2/;t1; s/.*,(.){9}.*/&,\1/;:1' file

Or

awk -F, '
    {print $0 FS substr($NF,($3=="bcd") ? 4 : 9, 1)}
' file
1 Like