How to cut a line that contains two different separator


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users How to cut a line that contains two different separator
# 1  
Old 07-08-2011
How to cut a line that contains two different separator

Hi all,

I have a script that receives a file, receives the values and put them on an oracle database.
I need to do this for multiple files that will contain in the end near 500 million records, so performance is a major concern.

until now there was no issue because the file I'm receiving is a csv in the format:

Code:
value1,value2,value3,value4
value1,value2,value3,value4
value1,value2,value3,value4
value1,value2,value3,value4

but management decided to change to a diferent provider and this one is providing the files with the following output:
Code:
field1=value1,field2=value2,field3=value3,field4=value4
field1=value1,field2=value2,field3=value3,field4=value4
field1=value1,field2=value2,field3=value3,field4=value4
field1=value1,field2=value2,field3=value3,field4=value4

previously when i wanted to access a field, for example field 3 i just need to do:
Code:
cut -d"," -f3

I'm trying to get one expression that doesn't require regular expressions to get the same output with the new file but I'm unable to find a way do get this.

Is there any option I can indicate two separators to the cut expression?
Code:
cut -d"," and -d"=" -f6

Using this with both expressions I would get "value3".

In this case I would not require regular expression and just needed to ajust the indexes I use for the several fields...

Thanks in advance.

Best regards,
Ricardo Tomás
# 2  
Old 07-08-2011
Is awk OK?
Code:
awk -F"[,=]" '{print $6}' file

Alternatively you could use two cuts:
Code:
cut -d"," -f3 file | cut -d"=" -f2

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 07-08-2011
I forgot to add something...
I've tried that and this is what i get:
Code:
cut -f1 -d"," test2.txt | cut -f2 -d"="
value1
cut -f2 -d"," test2.txt | cut -f2  -d"="
value2
cut –f3 -d"," test2.txt | cut -f2  -d"="
value3

but I need to get multiple values at once I'm not able to make it work.
Is it possible to configure cut to do that?

For example if I want to get "value1", "value2" and "value3" at once!

It works with AWK. but i would prefer to use cut because cut I know it can be integrated correctly in another tool I use and I'm not sure It as support for AWK.

with AWK:
Code:
awk -F"[,=]" '{print $2 $4 $6 }' test2.txt
value1value2value3

Regards

---------- Post updated at 08:12 AM ---------- Previous update was at 07:49 AM ----------

Got a solution that works for me:
Code:
 
sed s/=/,/g test2.txt | cut -d"," -f2,4,6,8

Thanks.

Last edited by naoseionome; 07-08-2011 at 12:52 PM.. Reason: added awk results
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Replace \n char true line Separator

Unix File is pipe delimited with 17 fields. We may get extra pipes in data also. We may get \n char (1 or more \n in one field or multi fileds) in data in any field. Need to replace \n true ( line separator) with 'space and bell char space' chars (' \a ') Not data \n. Input:... (1 Reply)
Discussion started by: rajeshkumare
1 Replies

2. Shell Programming and Scripting

Use two field separator in the same line and print them

Hi Guys, I have the file --- HOST_NAME,data_coleta,data_carga,CPU_util,CPU_idle,run_queue,memory,MEMORY_SYSTEM,MEMORY_TOTAL,MEMORY_SWAPIN,MEMORY_SWAPOUT,DISK_READ,DISK_WRITE,DISK_IO,NET_IN_PACKET, NET_OUT_PACKET... (4 Replies)
Discussion started by: antoniorajr
4 Replies

3. Shell Programming and Scripting

How to change the line separator?

Hi All, I have a file with 20 columns, and the data itself has "\n" new line in it. So we have changed the row delimiter to ^E. Now i am unable to use head, wc -l etc... Please let me know how to change the line separator temporarily to run these unix commands. Thanks. (1 Reply)
Discussion started by: baranisachin
1 Replies

4. UNIX for Dummies Questions & Answers

Add a field separator (comma) inside a line of a CSV file

Hi... I can't find my little red AWK book and it's been a long while since I've awk'd. But I need to take a CSV file and convert the first word of the fifth field to its own field by replacing a space with a comma. This is for importing a spreadsheet of issues into JIRA... Example: a line... (9 Replies)
Discussion started by: Tawpie
9 Replies

5. Shell Programming and Scripting

Grep or print each section of a file on one line with a separator

I can obtain information from itdt inventory command however it display as below, I'd like to print each entity on one line but seperated by : the file is something like and each section ends with Volume Tag Drive Address 256 Drive State ................... Normal ASC/ASCQ... (3 Replies)
Discussion started by: gefa
3 Replies

6. Shell Programming and Scripting

Need a separator string between fields in cut -c command

Hi All, I'm trying to view data using cut command for a fixed length file using the below command: cut -c 1-3,4-5 FALCON_PIS_00000000.dat I want to mention a separator say | (pipe) in between 1-3 and 4-5. Please let me know how to achieve this. Thanks in Advance, (3 Replies)
Discussion started by: HemaV
3 Replies

7. Slackware

How should I cut this line using cut and grep?

not sure how to do it. wan't to delete it using cut and grep ince i would use it in the shell. but how must the command be? grep "64.233.181.103 wwwGoogle.com" /etc/hosts | cut -d the delimeter is just a space. can you help meplease. :D (1 Reply)
Discussion started by: garfish
1 Replies

8. Shell Programming and Scripting

reading line by line and cut

I have a file like name file.txt whose contents are 3 fields separated by colon':' . somewhat like code/OR_R1400_RC4/BM_ATEMP_11.0.1.33:28/01/2010:N code/OR_R1400_RC5/BM_ATEMP_11.0.1.35:28/01/2010:Y code/OR_R1400_RC4/BM_ATEMP_11.0.1.33:29/01/2010:N... (8 Replies)
Discussion started by: gotam
8 Replies

9. Shell Programming and Scripting

cut a string in a textfile line per line

i need to cut the string in a textfile but each line has a specific way of cutting it (different lengths) i have a for loop that gets the string line per line, then each line has to be compared: for x in `cat tmp2.txt`; do if; then echo 'BAC' elif ... (6 Replies)
Discussion started by: izuma
6 Replies

10. Shell Programming and Scripting

awk & cut record separator problem

Hi All, I've got some strange behaviour going on when trying to manipulate a file that contains spaces. My input file looks something like this: xxxxxxxxx,yyyy,sss sss sss,bbbbbbb If I use awk: When running from the command line I get: sss sss sss But when running from a... (7 Replies)
Discussion started by: pondlife
7 Replies
Login or Register to Ask a Question