cutting columns if delimiter has more than one charecter


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers cutting columns if delimiter has more than one charecter
# 8  
Old 09-13-2006
Quote:
Originally Posted by Ygor
Escape the backslash, like this...
Code:
$ awk -F '\\$%' '{print $1}' file
abc
abc
abc

It's also the single quotes as well as the \\ that's makes it work. Since I started off as a programmer before becoming an admin, I typically use double quotes everywhere (I also use $i for my temporary loop variables). The "Learning The Korn Shell" book advises using single quotes everywhere and using double quotes when you have variables within text strings or have to maintain formatting.

So my habit of using double quotes even with the double escape results in:

Code:
$ awk -F"\\$%" '{print $1}' files
asd$%fdsasa$%fdafas$%fdsa$%
asd$%fdsasa$%fdafas$%fdsa$%
asd$%fdsasa$%fdafas$%fdsa$%
asd$%fdsasa$%fdafas$%fdsa$%
asd$%fdsasa$%fdafas$%fdsa$%
asd$%fdsasa$%fdafas$%fdsa$%
asd$%fdsasa$%fdafas$%fdsa$%

But if you habitually use single quotes, you'd get the correct answer:

Code:
$ awk -F'\\$%' '{print $1}' files
asd
asd
asd
asd
asd
asd
asd

Thanks for that bit of information. Smilie

Carl
# 9  
Old 09-14-2006
Code:
[sayonm@zion ~]$ sed -e 's/\$%/ /g' file | awk '{print $2}'
fdsasa
fdsasa
fdsasa
fdsasa
fdsasa
fdsasa
fdsasa

i think this can also be done....it just an alternative of cut, tr etc....the above mentioned methods are equally good
# 10  
Old 09-14-2006
While converting the $% into ' ' or ':' or any other single character sounds good for this specific test data file, what if the file looked more like this:

asd$%rqe fd$%BAE:RWD$%fdjk$%jkas$ %fd$%jkfds
asd$%rqe fd$%BAE:RWD$%fdjk$%jkas$ %fd$%jkfds
asd$%rqe fd$%BAE:RWD$%fdjk$%jkas$ %fd$%jkfds
asd$%rqe fd$%BAE:RWD$%fdjk$%jkas$ %fd$%jkfds
asd$%rqe fd$%BAE:RWD$%fdjk$%jkas$ %fd$%jkfds
asd$%rqe fd$%BAE:RWD$%fdjk$%jkas$ %fd$%jkfds
asd$%rqe fd$%BAE:RWD$%fdjk$%jkas$ %fd$%jkfds
asd$%rqe fd$%BAE:RWD$%fdjk$%jkas$ %fd$%jkfds
asd$%rqe fd$%BAE:RWD$%fdjk$%jkas$ %fd$%jkfds

You can use the sed or tr commands to convert the $% into another character but if you don't know what's actually in the data file, doing this can be dangerous.

The best way is using a tool that'll let you define multi-character delimiters like awk, perl or python.

Carl
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Cutting specific columns from lines

I am trying to remove columns 81-97 from a line that can be as long as 114 characters. Because a number of lines might not have under 80 characters, using the cut command following by paste could be a problem. While sed might work, is there some other utility that could do this more easily? ... (9 Replies)
Discussion started by: wbport
9 Replies

2. Shell Programming and Scripting

Cutting values with delimiter

Hi All, I have a string with , delimiter america,finland,netherlands Now i want these values to be stored in file as below with newline character at end of each value america finland netherlands Regards Prasad (3 Replies)
Discussion started by: krishna_gnv
3 Replies

3. Shell Programming and Scripting

Cutting a string using more than one character as delimiter

Hi , I have a set of files in a folder which i need to cut in to two parts.... Sample files touch AE_JUNFOR_2014_MTD_2013-05-30-03-30-02.TXT touch AE_JUNFOR_2014_YTD_2013-05-30-03-30-02.TXT touch temp_AE_JUNFOR_2014_MTD_2013-05-30-03-30-02.TXT touch... (4 Replies)
Discussion started by: chillblue
4 Replies

4. Shell Programming and Scripting

Cutting a part of line till delimiter

here are the few scenarios... isoSizeKB text NOT NULL, reserved1 varchar(255), KEY `deviceId` (`deviceId`) `d5` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `dHead` enum('HistoryInfo','Diversion') COLLATE utf8_unicode_ci, `ePR` int(11) DEFAULT '0', PRIMARY KEY (`id`) ... (7 Replies)
Discussion started by: vivek d r
7 Replies

5. UNIX for Dummies Questions & Answers

cutting columns if delimiter has more than one charecter (|^)

Hi All, I am having a file with the delimiter '|^'. File name:test_dlim.csv I want to cut the first field of this using awk command. I tried with the help of the following link:... (2 Replies)
Discussion started by: boopathyvasagam
2 Replies

6. Shell Programming and Scripting

Cutting a file with multiple delimiters into columns

Hi All I have recently had to start using Unix for work and I have hit brick wall with this prob.... I have a file that goes a little something like this.... EUR;EUR;EUR:USD:USD;USD;;;EUR/USD;XAU/AUD;XAU/EUR;XAU/AUD,GBP/BOB,UAD/XAU;;;1.11;2.22;3.33;4.44;5.55;6.66;;; is it possible to... (7 Replies)
Discussion started by: luckycharm
7 Replies

7. UNIX for Dummies Questions & Answers

Cutting a portion of a line seperated by pipe delimiter

Hi, In the below line a|b|10065353|tefe|rhraqs|135364|5347575 dgd|rg|4333|fhra|grhrt|46423|urdsgd Here i want to cut the characters in between the second and third pipe delimiter and then between fifth and sixth delimiter and retain the rest of the line. My output should be ... (3 Replies)
Discussion started by: ragavhere
3 Replies

8. Shell Programming and Scripting

Cutting a tab delimiter file

I have a 30 column tab delimited record file. I need to extract the first 10column. The following command to cut was not working cut -f 1-10 -d "\t" filename. Could any one keep on this . Thanks in Advance (4 Replies)
Discussion started by: vinod.thayil
4 Replies

9. Shell Programming and Scripting

Cutting Columns and Moving in to a file

Guys, Can any one tell me how can we cut the columns and move each column in to a separate file using awk? I have a tab delimited file as shown below, 1213 wattt werree 2345 skhasdjh aasas I want to output this in to three files named a.txt,b.txt and c.txt say a.txt... (3 Replies)
Discussion started by: Serious Sam
3 Replies

10. Shell Programming and Scripting

Comapring files charecter by charecter using AWK/Shell Script

Hi... I have a requrement to compare two files. for e.g. File 1 2007/08/19 09:48:10 DH-032 $APTA1: Device AATD8029 2007/08/19 09:48:10 DH-045 $APTA1: Device AATD8029 2007/08/19 09:48:10 DH-043 $APTA1: Device AATD8029 File 2 2007-08-19 09:48:10 DH-032... (1 Reply)
Discussion started by: evvander
1 Replies
Login or Register to Ask a Question