![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| merging CSV data using a one liner from shell? | jjinca | Shell Programming and Scripting | 2 | 08-13-2007 08:15 AM |
| Need help for 2 data file merging | getdpg | Shell Programming and Scripting | 2 | 07-12-2006 06:07 AM |
| Merging info | Manan | Shell Programming and Scripting | 3 | 05-20-2006 04:51 AM |
| Merging Help | kumarc | Shell Programming and Scripting | 3 | 05-04-2006 11:24 AM |
| Merging Partitions | camerja1 | UNIX for Dummies Questions & Answers | 1 | 12-10-2002 03:20 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
Merging data
Hi,
I have the following problem: Input: "num1","num2","num3",num4,num5,"num6" required output: "num1num2","num3",num4,num5,"num6" I need to join field 1 and field 2 together but I always end up getting: "num1""num2","num3",num4,num5,"num6" Note that not all fields have " at both ends. Any advice? Thanks! |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
Post the script you have.
|
|
#3
|
||||
|
||||
|
How about this ?
Code:
sed -e 's/^\"\([a-z0-9]*\)\",\"\([a-z0-9]*\)\"\(.*\)/\"\1\2"\3/' rev.txt where rev.txt is your input file. Here is the explanation Code:
s/^\"\([a-z0-9]*\)\",\"\([a-z0-9]*\)\"\(.*\)/\"\1\2"\3/ breaks down to s/ string substitution - regex follows ^ starts with \"\([a-z0-9]*\)\" " followed by a sequence of alphabets and numbers(store it into buffer 1) followed by , , \"\([a-z0-9]*\)\" " followed by a sequence of alphabets and numbers (store it into buffer 2)followed by \(.*\) any combinations of charcters ( store it into buffer 3) \ end of regex "\1\2"\3 to get your final desired output / end of s/// |
|
#4
|
|||
|
|||
|
You can get as,
echo '"num1","num2","num3",num4,num5,"num6"' | while read line; do f1=$(echo $line | cut -d, -f1|sed 's/"$//') f2=$(echo $line | cut -d, -f2|sed 's/^"//') f3=$(echo $line | cut -d, -f3-) echo $f1$f2,$f3 done hth. |
|
#5
|
|||
|
|||
|
You can try with one line awk as,
echo '"num1","num2","num3",num4,num5,"num6"' | awk -F, '{ split($1,a,"\"");split($2,b,"\"");print "\""a[2]b[2]"\","$3","$4","$5","$6 }' If the field count is fixed 6. Else try as, echo '"num1","num2","num3",num4,num5,"num6"' | awk -F, '{ split($1,a,"\"");split($2,b,"\"");printf "\""a[2]b[2]"\",";for (i=3;i<=NF-1;i++){printf $i",";}print $NF }' hth. |
|
#6
|
|||
|
|||
|
thanks muthukumar,
I have tried ur script and it works really well! thanks to vino for helping too! |
|
#7
|
|||
|
|||
|
erm one question though:
why is it a[2]b[2] and not a[0]b[0] or any other numbers? what does "2" mean? |
|||
| Google The UNIX and Linux Forums |