![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | 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 and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| 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 12:15 PM |
| Need help for 2 data file merging | getdpg | Shell Programming and Scripting | 2 | 07-12-2006 10:07 AM |
| Merging info | Manan | Shell Programming and Scripting | 3 | 05-20-2006 08:51 AM |
| Merging Help | kumarc | Shell Programming and Scripting | 3 | 05-04-2006 03:24 PM |
| Merging Partitions | camerja1 | UNIX for Dummies Questions & Answers | 1 | 12-10-2002 06:20 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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! |
|
||||
|
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. |
|
||||
|
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. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|