0

I need to keep only lines that start with 'ab'; and have no letter between 'ab' and 'cd'. From these lines, I need to remove 'ab' and 'cd'.

Input:

abI am jhoncd
ab32.58cd
abI live in USAcd
ab22. I was born in NYcd
ab58.2cd
ef

output:

32.58
58.2

thanx guys!

2
  • 1
    Do you truly mean "have no letter" or do you actually mean "have only digits and periods"? Really think about what might be non-trivial input cases to handle and post those. For example: ab#cd, xab3cd, abab3cd, ab3cdcd, ab3ef, ef3cd, ab.cd, etc... It's not clear what your expected output would be for some of those so chances are most of our answers wouldn't do what you want with them. The more interesting/difficult input you can come up with the better chance there is that you will get a solution that works for your real input instead of just the sample you've posted so far.
    – Ed Morton
    Jul 27, 2014 at 13:34
  • looks like homework Apr 10, 2017 at 20:45

4 Answers 4

1
$ sed -n -r 's/^ab([^[:alpha:]]+)cd/\1/p' file
32.58
58.2

$ awk '/^ab([^[:alpha:]]+)cd/{gsub(/ab|cd/,""); print}' file
32.58
58.2

I suspect what you really want, though, is:

$ sed -n -r 's/ab([[:digit:].]+)cd/\1/p' file
32.58
58.2

$ awk '/ab([[:digit:].]+)cd/{gsub(/ab|cd/,""); print}' file
32.58
58.2

i.e. instead of removing lines that contain letters as requested, select lines that only contain digits and periods between ab and cd.

2
  • 1
    +1 for the first sed solution. Although (as Ed points out in his comment) the question is not well specified and it is not clear if this produces the desired output for a line of the form ab123cdblah I suspect the pattern should end with a $ anchor. Jul 27, 2014 at 13:38
  • I considered that but while the OP says the line should start with ab he doesn't say it should END with cd he just said he wants only lines that start with 'ab'; and have no letter between 'ab' and 'cd'. From these lines, I need to remove 'ab' and 'cd'. so thats what the above scripts do. The OP really needs to clarify his requirements and post some more non-trivial sample input to get a robust answer.
    – Ed Morton
    Jul 27, 2014 at 13:40
0

Through awk,

$ awk '/^ab[^a-z]+cd$/{gsub(/ab/,""); gsub(/cd/,""); print}' file

OR

$ awk '/^ab[^[:alpha:]]+cd$/{gsub(/ab/,""); gsub(/cd/,""); print}' file
32.58
58.2

The above awk command searches for the lines which starts with ab followed by not of letter(or alphabetic) character one or more again followed by the string cd at the last. If it finds any, then it removes the string ab and cd through awk's built-in gsub function. Finally the result was redirected to standard output.

Pattern explanation:

  • ^ Asserts that we are at the start of the line.
  • ab Matches the string ab
  • [^a-z]+ Matches any character not of a-z one or more times.
  • cd Matches the string cd
  • $ End of the line.
7
  • thanks Avinash; could you please explain your solution?
    – blue_xylo
    Jul 27, 2014 at 12:29
  • Note that this will fail in some locales due to using [a-z] instead of [[:alpha:]] because it's trying to only look for lower case letters and in some locales [a-z] will contain most upper case letters and in others it won't contain any.
    – Ed Morton
    Jul 27, 2014 at 12:58
  • yep. If op wants any chars not of lowercase, uppercase a-z then he may try [^a-zA-Z] regex. Jul 27, 2014 at 13:00
  • No. a-z ONLY means lower case in some locales. In others the chars are ordered aAbBcC..zZ so a-z means all letters except Z. In other locales it means other things. There may even be locales where letters are inter-mixed with non-letters. The character class [:lower:] means lower case, and [:upper:] means upper case and [:alpha:] means letters (alphabetic characters).
    – Ed Morton
    Jul 27, 2014 at 13:07
  • @AvinashRaj in my country I get the problems ED writes about. Due to local settings this [^a-zA-Z] will fail, but not this [[:alpha:]], so -1 until its fixed.
    – Jotne
    Jul 27, 2014 at 16:04
0

Use grep:

grep -oE '[0-9]+[.][0-9]+' file

Other forms:

grep -oE '[[:digit:]]+[.][[:digit:]]+' file
grep -o '[0-9]\+[.][0-9]\+' file

Another for awk:

awk '{ while (match($0, /[0-9]+[.][0-9]+/)) { print substr($0, RSTART, RLENGTH); $0 = substr($0, RSTART + RLENGTH) } }' file

Update

grep -oE '[0-9]+([.][0-9]+)?' file

awk '{ while (match($0, /[0-9]+([.][0-9]+)?/)) { print substr($0, RSTART, RLENGTH); $0 = substr($0, RSTART + RLENGTH) } }' file
3
  • your grep solutions only work when we have digit.digit format. My apologies, I did not explain the problem very well. I put an introduction on purpose "keep only lines that start with 'ab'; and have no letter between 'ab' and 'cd' " (otherwise, I only would have put input and output files). Here there is no specification regarding the string length and format. So we can have: digit or digit.digit or digit.digit.digit ...Sorry about that. as for the awk solution, could you please explain?
    – blue_xylo
    Jul 27, 2014 at 12:51
  • @blue_xylo I added an update. The awk solution uses match() and make it look for a match in every line. When a match is found, it prints it and updates the line to run a test again. This would continue until no match is found.
    – konsolebox
    Jul 27, 2014 at 12:59
  • Also, I suggest that you update your post to make it more clear what output you really want.
    – konsolebox
    Jul 27, 2014 at 13:10
0

This awk may do:

awk -F"ab|cd" '$2~/^[0-9.]+$/ {print $2}' file
32.58
58.2

Edit The awk above will fail if you have ab45.23ab etc. This gnu awk would be better:

awk -F"cd" -v RS="ab" 'NF==2 && $1~/^[0-9.]+$/ {print $1}' file
32.58
58.2

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.