The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts 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 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

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 06-02-2005
ReV ReV is offline
Registered User
 

Join Date: Jun 2005
Posts: 34
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!
Reply With Quote
Forum Sponsor
  #2  
Old 06-02-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,681
Post the script you have.
Reply With Quote
  #3  
Old 06-02-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,681
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///
Vino
Reply With Quote
  #4  
Old 06-02-2005
Registered User
 

Join Date: Feb 2005
Location: Coimbatore, Tamilnadu, India
Posts: 119
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.
Reply With Quote
  #5  
Old 06-02-2005
Registered User
 

Join Date: Feb 2005
Location: Coimbatore, Tamilnadu, India
Posts: 119
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.
Reply With Quote
  #6  
Old 06-02-2005
ReV ReV is offline
Registered User
 

Join Date: Jun 2005
Posts: 34
thanks muthukumar,

I have tried ur script and it works really well! thanks to vino for helping too!
Reply With Quote
  #7  
Old 06-02-2005
ReV ReV is offline
Registered User
 

Join Date: Jun 2005
Posts: 34
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?
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:31 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0