If I have a set of strings,
C21
F231
H42
1C10
1F113
and I want to isolate the ints following the char, what would the sed string be to find numbers after letters?
If I do,
[a-zA-Z]*[0-9], I will get numbers after letters, but I am looking to do something like,
sed 's/[a-zA-Z]*[0-9]/\t[a-zA-Z]*[0-9]/g'
this will give me both the letters and numbers in the new col,
C21 C21
F231 F231
H42 H42
1C10 C10
1F113 F113
when I want just,
C21 21
F231 231
H42 42
1C10 10
1F113 113
How do I identify numbers following letters without selecting the letters along with the numbers? I suppose I could do a second run like,
sed 's/\t[a-zA-Z]//g'
but I' not sure that would work either.
LMHmedchem
cgkmal
March 27, 2011, 4:28pm
2
Hi LMHmedchem,
If you want to do it with sed you could use Regex back reference feature within sed as follow:
echo "C21
F231
H42
1C10
1F113" | sed 's/\(.*[A-Z]\)\([0-9]*\)/\1\2 \2/'
C21 21
F231 231
H42 42
1C10 10
1F113 113
Hope it helps,
Best regards
sed 's_\([0-9][a-zA-Z]\)*\([0-9][0-9]*\)_\1\2\^\2_g' <chars | tr '^' '\t'
C21 21
F231 231
H42 42
1C10 10
1F113 113
If sed doesn't take the \t as a tab then pipe it through tr for character translation (see above)
ctsgnb
March 27, 2011, 5:45pm
4
sed 's/[0-9]*$/& &/' infile
$ echo "C21
F231
H42
1C10
1F113" | sed 's/[0-9]*$/& &/'
C21 21
F231 231
H42 42
1C10 10
1F113 113
$
---------- Post updated at 11:45 PM ---------- Previous update was at 11:41 PM ----------
or
sed 's/[0-9][0-9]*$/& &/' infile
echo "C21
F231
H42
1C10
1F113" |sed -r 's/.*/&\t&/;s/([0-9])?[A-Z]//2'
C21 21
F231 231
H42 42
1C10 10
1F113 113
tene
March 28, 2011, 2:39am
6
echo "C21
F231
H42
1C10
1F113" | sed 's/\(.*[a-zA-Z]\)\([0-9]*\)/& \2/g'
21
231
42
10
113
ctsgnb
March 28, 2011, 4:01am
7
@tene ,
You can get the same result with more simple statement
sed 's/.*[^0-9]//' infile
tene
March 28, 2011, 4:26am
8
ctsgnb:
@tene ,
You can get the same result with more simple statement
sed 's/[0-9][0-9]*$/&/' infile
or even ( a bit less strict)
sed 's/[0-9]*$/&/' infile
Both these commands are not working.There is no change in output.
ctsgnb
March 28, 2011, 4:42am
9
Which OS / shell do you run ?
ctsgnb
March 28, 2011, 4:58am
11
@tene
Oooops, you true, lol! ... i mixed with the initial statement,
but what you propose can still be simplified this way:
sed 's/.*[^0-9]//' infile
tene
March 28, 2011, 5:02am
12
Yes right, but here we cannot print the original string.
Anyways, I was just trying out the \2 thing in my command..
I am just exploring different ways.
ctsgnb
March 28, 2011, 5:23am
13
I think the best way to do it is what i proposed in post #4
( the one that cgkmal comments in detail in his post #10 )
kato
March 28, 2011, 6:15am
14
There must be a dearth of decent sed/awk problems around at the moment; you guys need more complex problems to solve...