sdubey
1
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 changed to ";"
Please help how can this be done in any scripting language sed, awk or perl?
Regards
Sandeep
File
a|b|c|d|3|f1|f2|f3
a|b|c|d|5|f1|f2|f3|f4|f5
awk -F'|' ' {OFS="|";
six="";
for(i=6;i<=NF;i++)
{ six=six $i ";" }
print $1,$2,$3,$4,$5,six
}' filename
input & output:
sdubey
4
Thanks Jim. It helped me in getting started. Just one followup: If the file is as:
a|b|c|d|3|f;;1;|f2;;0;|f3;;1;
a|b|c|d|5|m;;1;|x;;1;|f3;;0;|f4;;1;|f5;;1;
how can I change to
a|b|c|d|3|f;f2;f3;
a|b|c|d|5|m;x;f3;f4;f5;
Basically I need to starting from 6th column to the end of line, delete ";1;|" or ";0;|"
Thanks again for your help.
Regards
Sandeep
sb008
5
sed -e "s/;[^|][^|]*//g" -e "s/|/;/g" -e "s/\([^;]*\);\([^;]*\);\([^;]*\);\([^;]*\);\([^;]*\);/\1|\2|\3|\4|\5|/" <file>
hanny
6
A shorter one:
sed -e 's/;[0-9].//g' -e 's/;|/;/g'
Cheers
sb008
7
Compliments, smart use of the double ";"