0

I need to parse numbers from codes like this:

p1 <- c(1, 2, 100, 23, 0.12, 0.03, 0)
p2 <- c(100, 200, 1, 23, 0.12, 0.03, 0)
p30 <- c(100, 200, 1, 23, 0.12, 0.03, 0)
p300 <- c(100, 200, 1, 23, 2, 1, 0)
more variables starts with p...
other codes...

The results are like this:

1 2 100 23 0.12 0.03 0
100 200 1 23 0.12 0.03 0
100 200 1 23 0.12 0.03 0
100 200 1 23 2 1 0

in which each row represent an array of a variable.

I tried sed and awk, but doesn't work out it. Does anyone have ideas about this? Thanks!

2
  • 1
    Please show what you've tried. It looks to me like it should be trivial with sed -- remove everything up to (, and remove , and ).
    – Barmar
    May 9, 2013 at 14:44
  • I'm not sure what you are trying to do ... do you have text that looks like the first box, and you are trying to use sed or awk to produce the results in the second box? Or do you actually need to parse the numbers?
    – Markku K.
    May 9, 2013 at 14:46

5 Answers 5

2

Using GNU sed (for -r and EREs):

$ cat file
p1 <- c(1, 2, 100, 23, 0.12, 0.03, 0)
p2 <- c(100, 200, 1, 23, 0.12, 0.03, 0)
p30 <- c(100, 200, 1, 23, 0.12, 0.03, 0)
p300 <- c(100, 200, 1, 23, 2, 1, 0)

$ sed -r 's/.*\(|[,)]//g' file
1 2 100 23 0.12 0.03 0
100 200 1 23 0.12 0.03 0
100 200 1 23 0.12 0.03 0
100 200 1 23 2 1 0
2

awk one-liner:

kent$  awk -F'[()]' '{gsub(/, */," ",$2)}$0=$2' file
1 2 100 23 0.12 0.03 0
100 200 1 23 0.12 0.03 0
100 200 1 23 0.12 0.03 0
100 200 1 23 2 1 0
6
  • $0=$2 nice trick! Or an even shorter version: awk -F'[()]' '{$0=$2;gsub(/, */," ")}1'
    – TrueY
    May 9, 2013 at 15:28
  • @TrueY u r a golfer :) awk -F'[()]' '{gsub(/, */," ")}$0=$2'
    – Kent
    May 9, 2013 at 15:47
  • 1
    @TrueY shortest I could get awk -F'(' 'gsub(/,|)/,s)&&$0=$2' with just awk May 10, 2013 at 8:46
  • @sudo_O: Definitely +1!!! I have a two chars shorter version awk -F\( 'gsub(/,|)/,s),$0=$2'. :). Thanks for the |! I thought it works only in (...). Also thanks for the /[][]/!
    – TrueY
    May 10, 2013 at 9:16
  • 1
    @sudo_O I assume that there could be multi spaces after the comma. If we skip that part, just based on the example in the question. the one-liner could be written like this:awk 'gsub(/.*\(|,|)/,l)'
    – Kent
    May 10, 2013 at 9:26
1

With awk and tr for readability ;-)

awk -F'[()]' '{print $2}' file | tr -d ','
0

Assuming you have it in file t.txt:

$ grep ^p t.txt|sed 's/.*[(]//; s/[)].*//; s/,//g'

1 2 100 23 0.12 0.03 0
100 200 1 23 0.12 0.03 0
100 200 1 23 0.12 0.03 0
100 200 1 23 2 1 0
0

You can do it in bash without calling external programmes. A bash one-liner :

#!/usr/bin/bash

while read v; do v=${v#*(}; v=${v%)*}; echo ${v//, / }; done<<XXX
p1 <- c(1, 2, 100, 23, 0.12, 0.03, 0)
p2 <- c(100, 200, 1, 23, 0.12, 0.03, 0)
p30 <- c(100, 200, 1, 23, 0.12, 0.03, 0)
p300 <- c(100, 200, 1, 23, 2, 1, 0)
XXX

Output:

1 2 100 23 0.12 0.03 0
100 200 1 23 0.12 0.03 0
100 200 1 23 0.12 0.03 0
100 200 1 23 2 1 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.