tricky file output


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers tricky file output
# 1  
Old 09-01-2009
tricky file output

I have a following file, I want to output 2 files. Is that easy to do?


Quote:

"""Name"",""# Day"",""cost"""
"""jacket"",5,2300.0"
"""Items"",""day"",""cost"""
"""jacket"",1,300.0"
"""jacket"",2,500.0"
"""jacket"",3,500.0"
"""jacket"",4,500.0"
"""jacket"",5,500.0"
"""Name"",""# Day"",""cost"""
"""men,sweater"",3,800.0"
"""Items"",""day"",""cost"""
"""men,sweater"",1,300.0"
"""men,sweater"",2,500.0"
"""men,sweater"",3,500.0"
....
....
....

Output 1
Quote:

Name,# Day,Cost
"Jacket",5,2300.0
"men,Sweater",3,800.0
...
output2
Quote:
Items,day,cost
"Jacket",1,300
"Jacket",2,500
"Jacket",3,500
"Jacket",4,500
"Jacket",5,500
"men,sweater",1,300
"men,sweater",2,500
"men,sweater",3,500
...
Thanks
# 2  
Old 09-01-2009
It could be something like this if you don't have a large input file
Code:
#!/bin/bash

while read line; do
   if [[ $line =~ Name ]]; then
      destFile="name.out"
      ((y++))
      [ $y -gt 1 ] && echo "$line" >/dev/null || echo "$line" >> $destFile
   elif [[ $line =~ Items ]]; then
      destFile="item.out"
      ((i++))
      [ $i -gt 1 ] && echo "$line" >/dev/null || echo "$line" >> $destFile
   else
      echo "$line" >> $destFile
   fi
done < jbchen.in

Code:
echo | cat name.out - item.out
"""Name"",""# Day"",""cost"""
"""jacket"",5,2300.0"
"""men,sweater"",3,800.0"

"""Items"",""day"",""cost"""
"""jacket"",1,300.0"
"""jacket"",2,500.0"
"""jacket"",3,500.0"
"""jacket"",4,500.0"
"""jacket"",5,500.0"
"""men,sweater"",1,300.0"
"""men,sweater"",2,500.0"
"""men,sweater"",3,500.0"

otherwise, awk could be a better friend.

Last edited by daPeach; 09-01-2009 at 06:34 PM..
# 3  
Old 09-01-2009
I would love to use Awk but I think it's still hard to do text qualifier "double quote".

The solution you provided doesn't take care of double quote issue. when i open up csv file in excel, it looks as "one column" since it has begin doublequote and end doublequote. (3 double quotes in the begining, 1 double quote in the end)
# 4  
Old 09-02-2009
Code:
heads() {
   [ $1 -gt 1 ] && echo "$line" >/dev/null || sed 's/"//g' <<< $line >> $destFile
}
while read line
 do
   if [[ $line =~ Name ]]; then
      destFile="name.out"
      heads $((++i))
   elif [[ $line =~ Items ]]; then
      destFile="item.out"
      heads $((++y))
   else
      sed 's/^"//; s/"$//; s/""/"/g' <<< $line >>$destFile
   fi
done < jbchen.in

Code:
echo | cat name.out - item.out
Name,# Day,cost
"jacket",5,2300.0
"men,sweater",3,800.0

Items,day,cost
"jacket",1,300.0
"jacket",2,500.0
"jacket",3,500.0
"jacket",4,500.0
"jacket",5,500.0
"men,sweater",1,300.0
"men,sweater",2,500.0
"men,sweater",3,500.0



---------- Post updated 09-02-09 at 12:11 AM ---------- Previous update was 09-01-09 at 07:25 PM ----------

and its condensed form:
Code:
#!/bin/bash

heads() {
   [ $1 -gt 1 ] && echo "$line" > /dev/null || sed 's/"//g' <<< $line >> $destFile
   }
   while read line; do
      [[ $line =~ Name ]] && { destFile="name.out"; heads $((++i)); }
      [[ $line =~ Item ]] && { destFile="item.out"; heads $((++y)); }
      [[ ! $line =~ Name|Item ]] && sed 's/^"//; s/"$//; s/""/"/g' <<< $line >> $destFile
   done < jbchen.in

which gives the same result.

Last edited by daPeach; 09-01-2009 at 09:33 PM..
# 5  
Old 09-02-2009
Any chance to use AWK to get the output.
I just want to learn.

Thanks
Jay
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Tricky sed required

Hi All I need to put some sed together for a task and its a bit advanced for me, so I thought I'd ask if anyone here could help. I have a csv file with content like this - "","abcde","","" "'","abcde","","" "","","","1234" "'e'","","","" I need to remove any single quotes that fall... (17 Replies)
Discussion started by: steadyonabix
17 Replies

2. HP-UX

Tricky situation getting IP address

Hi, I have a multihomed system HP-UX with two NIC cards having IP address 10.9.0.13 & 10.9.0.45 I have two weblogic servers running one listening on "10.9.0.13" and the other on "10.9.0.45" Given a PID how is it possible to extract the IP Address that the weblogic server is using and... (1 Reply)
Discussion started by: mohtashims
1 Replies

3. UNIX for Dummies Questions & Answers

Tricky GREP question..

I have some large login files that I need to extract (user)@(server) from. Where it gets tricky is that there is usually more than one '@' sign on each line(although it does have a leading space if it's not part of the (user)@(server) string), I need only the (user)@(server) section, I need only... (6 Replies)
Discussion started by: Mordaris
6 Replies

4. Solaris

Tricky egrep

Hi folks! My first post here. I'm working on a script that retrieves a range of files from a list depending on a range of time. UPDATE: I've seen it could be difficult to read all this thing, so I'll make a summarize it.. How come I do this and take a result.. grep "..\:.." lista.new |... (4 Replies)
Discussion started by: kl0x
4 Replies

5. Shell Programming and Scripting

create csv in ksh from tricky log file

hi guys, trying to create a csv from a tricky log file in ksh, using 'awk '{print $1" "$14" "$15" "$16" "$17" "$18" "$19}' >> $TMP_FILE' on another set of files I have an output file with hundreds of lines in which looks like so: ABC_DEFGHI_16_JKLMNP11.20101115_095412_374.log:09:54:29.579... (3 Replies)
Discussion started by: rich@ardz
3 Replies

6. Shell Programming and Scripting

Dynamic output file generation using a input text file with predefined output format

Hi, I have two files , one file with data file with attributes that need to be sent to another file to generate a predefined format. Example: File.txt AP|{SSHA}VEEg42CNCghUnGhCVg== APVG3|{SSHA}XK|"password" AP3|{SSHA}XK|"This is test" .... etc --------- test.sh has... (1 Reply)
Discussion started by: hudson03051nh
1 Replies

7. Shell Programming and Scripting

Tricky data manipulation...

Hi everyone.. I am new here, hello.. I hope this doesn't come across to you folks as a stupid question, I'm somewhat new to scripting :) I'm seeking some help in finding a way to manipulate data output for every two characters - example: numbers.lst contains the following output:... (3 Replies)
Discussion started by: explicit
3 Replies

8. Shell Programming and Scripting

Tricky Sed

Hello. I am trying to convert occurrences of 'NULL' from a datafile. The 'NULL' occurences appears at this: |NULL| NULL|NULL| NULL|NULL| NULL|NULL| NULL| There should be 52 fields per line. I would like any occurrence of | NULL| or |NULL| to appear as '||' Currently I am using this sed... (2 Replies)
Discussion started by: bestbuyernc
2 Replies

9. Windows & DOS: Issues & Discussions

Tricky one...

Here's my problem: I have a laptop running Windows XP Pro with no internal CD or Floppy drives. I want to install Linux on it. I don't care about the Windows XP Pro installation, in fact I would like to install Linux over the entirety of the HD. However I cannot boot from any external CD drive... (1 Reply)
Discussion started by: saabir
1 Replies

10. Filesystems, Disks and Memory

Tricky File Permission Question

I'm trying to answer the following question about file permissions in Unix. Consider a file with the following permissions: rwx---r-- I am not the owner of this file, but I am a member of the group of this file. My question is: do I have read access to this file? I thought... (3 Replies)
Discussion started by: Hook
3 Replies
Login or Register to Ask a Question