Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

deleting data and merging data

Status
Not open for further replies.

omoo

Programmer
May 30, 2005
87
0
0
US
Hi,

I have the following data:

"header1";"header2";"header3";"header4";"data1";"data2";"1Lnum1";"num2";"num3";"num4";"1Lnum5";"num6";"num7";"num8"

I wan the final output to be:

"header1","header3","header4"
"1Lnum1.num2","num3","num4"
"1Lnum5.num6","num7","num8"

In summary, I want to merge all the data under header1 and header2 with a dot in between and delete all the unnecessary data (data1,data2) in between the last header and the first "1L data. I hope I am clear with my problem.





 
man awk

look for IFS and printf info



HTH,

p5wizard
 
Hi,

Can put some scripts so that I can work from there?
 
You should get the idea from this:

Code:
echo '"h1";"h2";"h3";"h4";"d1";"d2";"1L1";"n2";"n3";"n4";"1L5";"n6";"n7";"n8"' | awk -F';' '{
 printf "%s,%s,%s\n", $1, $3, $4
 printf "%s.%s,%s,%s\n", $7, $8, $9, $10
}'

start counting...

HTH,

p5wizard
 
Hi,

Thanks I have sort out the main part of the formatting. Now I only have one problem, merging the data:

input:
"num1","num2","num3"

output:
"num1num2","num3"

What I always end up getting is:
"num1""num2","num3"
but I do not want the "" in the middle of 1st field and 2nd field. Any advice?




 
just to clarify, the fields are not all with the " at both ends. That is to say for example:

input:
"num1","num2","num3",num4,num5,"num6"

 
# echo '"abcd"'
"abcd"

# echo '"abcd"'|awk '
{
printf "%s\n", substr($1,2,length($1)-2)
}'
abcd

You take it from here...

HTH,

p5wizard
 
You could try:
Code:
sed 's/^"//;s/"$//' | awk -F '"?,"?'
in the first example given by p5wizard.

The sed suppress any " at start or end of line and the -F in awk allowed for optional " before and after the comma.

This simple trick would not work if there is any comma or escaped quotes inside the quoted fields of your csv file.
For example:
[tt]"value 1","value 2,3 & 4","He said \"hello\""[/tt]
And you're in trouble.

--------------------

Denis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top