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
# 1  
Old 09-12-2006
cutting columns if delimiter has more than one charecter

Hi,

My file looks like

abc$%sdfhs$%sdf$%sdfaf$%

here as seen delimiter is $%...now how cas i take out second field as cut command expect delimiter as single charecter only.....is there is any other way

thanks and regards
mahabunta
# 2  
Old 09-12-2006
Code:
echo "abc$%sdfhs$%sdf$%sdfaf$%" | tr -s '$%' '  ' | cut -d" " -f3

I think you can not use two char as delimiter in cut. What you can do is translate that two char to single char and then cut it
# 3  
Old 09-12-2006
thanks for reply.....

but is there is any other wat wirhout using tr command.......may be in AWk or any other method...
# 4  
Old 09-12-2006
hi,

I guess it can be done using awk scripting....

say my file is delimited by ~%%~ and i need 6th field then following command will give me correct result.......

cat file | awk -F "~%%~" '{print $6}'

sandy
# 5  
Old 09-12-2006
Hmm, that didn't work in my test. Passing more than one character to -F just returned the entire line as argument $1.

Code:
awk -F"\$%" '{print $1}' file

abc$%sdfhs$%sdf$%sdfaf$%
abc$%sdfhs$%sdf$%sdfaf$%
abc$%sdfhs$%sdf$%sdfaf$%
abc$%sdfhs$%sdf$%sdfaf$%
abc$%sdfhs$%sdf$%sdfaf$%
abc$%sdfhs$%sdf$%sdfaf$%
abc$%sdfhs$%sdf$%sdfaf$%

You could use sed to change it:

Code:
sed -e "s/\$%/:/g" file | cut -f3 -d:

sdf
sdf
sdf
sdf
sdf
sdf

Or use tr as noted earlier:

Code:
cat files | tr -s "$%" ":" | cut -f3 -d:

sdf
sdf
sdf
sdf
sdf
sdf

It appears the $% is causing issues with the -F command. If I replace the $% with ", ", I can run

Code:
awk -F", " '{print $2}' files2

sdfhs
sdfhs
sdfhs
sdfhs
sdfhs
sdfhs

And it works. I also tried the $% delimiter in an awk script and it didn't work either. I tried escaping one and both characters without success.

Carl
# 6  
Old 09-12-2006
Escape the backslash, like this...
Code:
$ awk -F '\\$%' '{print $1}' file
abc
abc
abc

# 7  
Old 09-12-2006
Quote:
Originally Posted by mahabunta
Hi,

My file looks like

abc$%sdfhs$%sdf$%sdfaf$%

here as seen delimiter is $%...now how cas i take out second field as cut command expect delimiter as single charecter only.....is there is any other way

thanks and regards
mahabunta
Just an alternative, in Python:
Code:
>>> line = "abc$%sdfhs$%sdf$%sdfaf$%"
>>> secondfield = line.split("$%")[1] #element 1 is second field
>>> print secondfield
sdfhs

 
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