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 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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
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!
  #2 (permalink)  
Old 06-02-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
Post the script you have.
  #3 (permalink)  
Old 06-02-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
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
  #4 (permalink)  
Old 06-02-2005
muthukumar muthukumar is offline
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.
  #5 (permalink)  
Old 06-02-2005
muthukumar muthukumar is offline
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.
  #6 (permalink)  
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!
  #7 (permalink)  
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?
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:50 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0