Remembering patterns and printing only those patterns using sed
Hi there, I am looking for some advice with 'sed'. I basically need to remember certain parts of each line in a text file and print only those parts. I can create a regular expression for this but am not sure how to remember and print these parts.
The part i want to remember is i a string of spaces, A-Za-z and commas ','. and then a one or two digit number. Thanks in advance. |
I'm not completely sure what you mean but if I understand correctly, you can use the -o option with grep. This way only that part of the text that matches your regular expression is shown on the output.
Hope this helps |
Yep that does help. Basically to make it more clear I want to remember two parts of an expression and print only those two parts. I think I need to do something with the ampersand operator or using the numbers 1-9 on the right hand side of the expression.
e.g. David Warner 123456789 0 5 50 Michael FitzPatrick 234567891 1 4 45 Hans Williamson 345678912 0 3 44 I just want to print the long string of numbers and the last number using sed. If this isn't clear enough I apologise but thanks for looking anyway. Cheers. |
If there's no guys with middle-names/initials and always
the same number of fields you could use awk '{print $3 $4 $5 $6}' file If neither is static something like awk '{for (i=1; i<NF; i++){ if( $i ~ /[[:digit:]]+/) printf "%d ", $i} print ""}' file should work. Cheers, Tink |
If you want to use sed:
Code:
cat $FILE | sed -r -e "s/(^[^[:digit:]]*)([[:digit:]]*)( [[:digit:]] [[:digit:]] )([[:digit:]]{2}$)/\2 \4/" PS: You might need to remove the leading '^' and trailing '$' (they represent the empty space at the beginning and the end of the line) to get it to work. Hope this helps |
Hi Bernie82,
Just my two cents. I did write a sed without the option '-r'. It was the form we learned before Open Software has come. Code:
cat $FILE | sed -e "s/^[^0-9]*\([0-9][0-9]*\) .*[^0-9]\([0-9]\{2\}\)$/\1 \2/" However you must select the lines which can be printed before: if the regular expression is not found, sed passes the entire line to the output. You can copy the expression from the sed to create a grep on the pipe before sed. |
All times are GMT -5. The time now is 01:14 AM. |