Cut based on Two Delimiters at one go


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cut based on Two Delimiters at one go
# 1  
Old 01-18-2007
Cut based on Two Delimiters at one go

Hi

I wanted to cut the feilds comming after % and After $ at one go

can we do some thing like this cut -f 2 -d "%|$" (But it doesnot work)



Input File

BWPG %TCPRP1 $SCSPR000
BWPH %TCPRP1 $SCSPR003
BWPI %TRTYUP ResourceDescription="IMPRIMANTE " $BWOPTY
BWPJ %ZOMBIE ResourceDescription="IMPRIMANTE " $XENTER
CWPJ %INTRA ResourceDescription="CAM 1ST 40121" $MENTER
CWPK %INTER ResourceDescription="CAM 2ND 40500" $SOZXI

Output

TCPRP1 SCSPR000
TCPRP1 SCSPR003
TRTYUP BWOPTY
ZOMBIE XENTER
INTRA MENTER
INTER SOZXI

Thanks
# 2  
Old 01-18-2007
Based on the input data provided:
Code:
$ sed 's/^.*%\([^ ]*\) .*\$\([^$]*\)$/\1 \2/' infile
TCPRP1 SCSPR000
TCPRP1 SCSPR003
TRTYUP BWOPTY
ZOMBIE XENTER
INTRA MENTER
INTER SOZXI

Cheers
ZB
# 3  
Old 01-18-2007
Modified,

Code:
sed 's/^.*%\([^ ]*\) .*\$\([^ ]*\)/\1 \2/' file

Code:
sed 's/^.*%\([^ ]*\) .*\$\([^$]*\)$/\1 \2/' file

Could you pleae explain why is '$' included in the block ?

It should work without that and it works ! Smilie
# 4  
Old 01-18-2007
[^$]* - Any number of "not"-dollars
$ - End of line

Unless further fields follow it makes no difference.
# 5  
Old 01-18-2007
If you have Python, here's a alternative:

Code:
#!/usr/bin/python
for line in open("file"):
  dollar_index = line.index("$")
  percent_index = line.index("%")
  print line[ percent_index+1 : dollar_index], line[dollar_index+1:]

output:
Code:
TCPRP1  SCSPR000
TCPRP1  SCSPR003
TRTYUP ResourceDescription="IMPRIMANTE "  BWOPTY
ZOMBIE ResourceDescription="IMPRIMANTE "  XENTER
INTRA ResourceDescription="CAM 1ST 40121"  MENTER
INTER ResourceDescription="CAM 2ND 40500"  SOZXI

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cut between two delimiters, / and .

BASH : I have a very long list I am parsing through: 10/10/19... (5 Replies)
Discussion started by: jeffs42885
5 Replies

2. Shell Programming and Scripting

Cut counting consecutive delimiters as fields

When cut encounters consecutive delimiters it seems to count each instance as a field, at least with spaces. Is this typical behavior for any delimiter? #:~$ ifconfig eth0 | grep HWaddr eth0 Link encap:Ethernet HWaddr 94:de:80:a7:6d:e1 #:~$ ifconfig eth0 | grep HWaddr | cut -d " " -f... (6 Replies)
Discussion started by: Riker1204
6 Replies

3. Shell Programming and Scripting

Insert Columns before the last Column based on the Count of Delimiters

Hi, I have a requirement where in I need to insert delimiters before the last column of the total delimiters is less than a specified number. Say if the delimiters is less than 139, I need to insert 2 columns ( with blanks) before the last field awk -F 'Ç' '{ if (NF-1 < 139)} END { "Insert 2... (5 Replies)
Discussion started by: arunkesi
5 Replies

4. UNIX for Dummies Questions & Answers

Cut fields between delimiters

I'm having bother getting both lines contained in a file to output as the same value. A simple example: john:123456:123:456:doe john:123456:123:doe cut -d: -f1,4 input file john:456 john:doe ^ first line should be same as second. trick one for me, i know why it's because of the... (4 Replies)
Discussion started by: landofus
4 Replies

5. Shell Programming and Scripting

To cut a string based on last index of identifier

here below is sample string null pointer dereference of 'resourceList' where null is returned from a method/opt/bld/fetch/ds/interzone/notification/LocalLineStatusNotificationListener.java:79 null pointer dereference of 'reList' where null is returned from a... (3 Replies)
Discussion started by: vivek d r
3 Replies

6. UNIX for Dummies Questions & Answers

how to cut based on a string

Hi I have some data where each line will look something like this: Time, name, i.d number, RB: 0.9949; RMQA: 0.0005; RB: 0.9951; RRA: 0.3; RA: 0.995; RA: 0.996; EA: 0.99105 etc. I want to cut out all the RB: and RA:'s with the numbers after. so in the above example i'd be left... (3 Replies)
Discussion started by: gvc
3 Replies

7. Shell Programming and Scripting

Concatinating the lines based on number of delimiters

Hi, I have a problem to concatenate the lines based on number of delimiters (if the delimiter count is 9 then concatenate all the fields & remove the new line char bw delimiters and then write the following data into second line) in a file. my input file content is Title| ID| Owner|... (4 Replies)
Discussion started by: bi.infa
4 Replies

8. Shell Programming and Scripting

Sorting based on multiple delimiters

Hello, I have data where words are separated by a delimiter. In this case "=" The number of delimiters in a line can vary from 4to 8. The norm is 4. Is it possible to have a script where the file could be separated starting with highest number of delimiters and ending with the lowest An... (8 Replies)
Discussion started by: gimley
8 Replies

9. UNIX for Advanced & Expert Users

cut and append the file based on id

Hi, I have a file which have set of rows and has to create separate files based on the id. Eg: 001_AHaris020 001_ATony030 002_AChris090 002_ASmit060 003_AJhon001 Output: I want three files like 001_A.txt, 002_A.txt and 003_A.txt. 001_A.txt should have ... (1 Reply)
Discussion started by: techmoris
1 Replies

10. Shell Programming and Scripting

cut -- line with no delimiters

I just discovered, to my dismay, the following part of the cut man page: -f, --fields=LIST select only these fields; also print any line that contains no delimiter character, unless the -s option is specified The -s option toggles the printing of lines with no delimiters. In most... (3 Replies)
Discussion started by: chlorine
3 Replies
Login or Register to Ask a Question