Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

check var has chars in it - i want int only 1

Status
Not open for further replies.
Feb 12, 2002
80
NO
Hi,

I am using the 'cut' command to take a column of data out of a file and then need to process the results.

However the results need to be integers, and some of the values have text characters in.

I need to take all integers from the left of my output.

Example output:
Code:
88PL  69
88PL  70
88PL  71
 ...[skip]
88PL1098
88PL1099
88PL1100

Example required:
Code:
69
70
71
 ...[skip]
1098
1099
1100


I have tried using typeset but this does not seem to work - it just gives a syntax error.

I can not edit the file before hand, and I cannot change my cut command to cut from the 'correct' column.

Any ideas?

Thanks.
 
Hi

Code:
                 [red]s[/red]/[green].[/green][blue]*[/blue][red]?[/red][green]([/green][blue]\d[/blue][red]+[/red][green])[/green][blue]$[/blue]/[red]$1[/red]/
                 [red]|[/red] [green]|[/green][blue]|[/blue][red]|[/red][green]|[/green] [blue]|[/blue][red]|[/red][green]|[/green][blue]|[/blue] [red]|[/red]
[red]substitute ------+[/red] [green]|[/green][blue]|[/blue][red]|[/red][green]|[/green] [blue]|[/blue][red]|[/red][green]|[/green][blue]|[/blue] [red]|[/red]
[green]any character -----+[/green][blue]|[/blue][red]|[/red][green]|[/green] [blue]|[/blue][red]|[/red][green]|[/green][blue]|[/blue] [red]|[/red]
[blue]0 or more[/blue] * [blue]--------+[/blue][red]|[/red][green]|[/green] [blue]|[/blue][red]|[/red][green]|[/green][blue]|[/blue] [red]|[/red]
[red]do not be greedy[/red] * [red]--+[/red][green]|[/green] [blue]|[/blue][red]|[/red][green]|[/green][blue]|[/blue] [red]|[/red]
[green]capture from... ------+[/green] [blue]|[/blue][red]|[/red][green]|[/green][blue]|[/blue] [red]|[/red]
[blue]digit ------------------+[/blue][red]|[/red][green]|[/green][blue]|[/blue] [red]|[/red]
[red]1 or more[/red] * [red]-------------+[/red][green]|[/green][blue]|[/blue] [red]|[/red]
[green]... capture to -----------+[/green][blue]|[/blue] [red]|[/red]
[blue]at the end ----------------+[/blue] [red]|[/red]
[red]first captured --------------+[/red]
* - quantifier of the previous character

Feherke.
 
Why not posting your actual cut command call ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Very colourful Feherke ... unfortuntly it doesn;t seem to work!

My cut command is simple:

cat ${file}.tmp | cut -c$linerange | uniq > line.range.cut

where
file.tmp is a tmp file created from my amain input
linerange is a predefined variable, e.g. 5-13

Examples of results and desired results:
Code:
Current      --> Desired
Q97BL 998    -->  998
Q97BL 999    -->  999
Q97BL1000    --> 1000
Q97BL1001    --> 1001

97BX 950     -->  950
97BX 951     -->  951
97BX 952     -->  952
97BX 953     -->  953
97BX 954     -->  954
...
97BX1000     --> 1000
97BX1001     --> 1001
97BX1002     --> 1002

Is that clearer?


 
By the way - the original output could also equally be

Code:
Q     9998
Q     9999
Q    10000
[\code]

basically, I do not know which column the numbers I want are in. All I know is that I want the numbers on the right of my cut, going left, until I hit a non-numeric character.
 
Hi

I think it works on my machine. Or I misunderstand you.
Code:
[blue]master #[/blue] cat littleIdiot
Q97BL 998
Q97BL 999
Q97BL1000
Q97BL1001

97BX 950
97BX 951
97BX 952
97BX 953
97BX 954
...
97BX1000
97BX1001
97BX1002

[blue]master #[/blue] perl -pe 's/.*?(\d+)$/$1/' littleIdiot
998
999
1000
1001

950
951
952
953
954
...
1000
1001
1002

[blue]master #[/blue] perl --version

This is perl, v5.8.0 built for i586-linux-thread-multi

Copyright 1987-2002, Larry Wall

Feherke.
 
Hmmmm .... I'll keep fiddling ...

Code:
namsx16% cat littleIdiot
Q97BL 998
Q97BL 999
Q97BL1000
Q97BL1001

97BX 950
97BX 951
97BX 952
97BX 953
97BX 954
...
97BX1000
97BX1001
97BX1002
namsx16% perl -pe 's/.*?(\d+)$/$1/' littleIdiot
998
999
1000
1001

950
951
952
953
954
...
1000
1001
1002
namsx16% perl --version

This is perl, version 5.005_03 built for sun4-solaris

Copyright 1987-1999, Larry Wall
[\code]
 
A sed way:
cut -c$linerange ${file}.tmp | sed -n 's!.*[^0-9]\([0-9][0-9]*\)$!\1!p' | sort -u > line.range.cut

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi

Is still not clear for me, but if you want to keep the spaces in front of the numbers, to have them padded ( exactly as in your Desired column in your example ) :
Code:
perl -pe 's/.*?([red] *[/red]\d+)$/$1/' littleIdiot

Feherke.
 
Thanks Ferheke,

I'm not bothered about padding - ideally not padded actually.

After a bit of tinkering, I see that if I have white space at the end of tme input to the perl command it does not work - see last 4 lines below:

Code:
namsx16% cat littleIdiot2
Q97BL 998
Q97BL 999
Q97BL1000
Q97BL1001

97BX 950
97BX 951
97BX 952
97BX 953
97BX 954
...
97BX1000
97BX1001
97BX1002

Q97BL 001
Q97BL1002    
Q97BL2971    
Q97BL2971    
Q97BL2972    
namsx16% perl -pe 's/.*?(\d+)$/$1/' littleIdiot2
998
999
1000
1001

950
951
952
953
954
...
1000
1001
1002

001
Q97BL1002    
Q97BL2971    
Q97BL2971    
Q97BL2972    
namsx16% perl --version

This is perl, version 5.005_03 built for sun4-solaris

Copyright 1987-1999, Larry Wall

So how do I loose the white space?

Nearly there ....!
 
cut -c$linerange ${file}.tmp | sed -n 's!.*[^0-9]\([0-9][0-9]*\) *$!\1!p' | sort -u > line.range.cut

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
feherke

Nice breakdown of the command & good use of colour.

Mike

"Whenever I dwell for any length of time on my own shortcomings, they gradually begin to seem mild, harmless, rather engaging little things, not at all like the staring defects in other people's characters."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top