1831294 Members
3508 Online
110022 Solutions
New Discussion

Script

 
SOLVED
Go to solution
SAM_24
Frequent Advisor

Script

Hi,

I have a requirement. I want to grep the lines which contains SEV word and from that output I want to cut SEV to end of the line.
Ex:

SEV word1 word2
Word1 SEV Word2
Word1 word2 SEV word4 word4
word word word without SEV

Thanks.
Never quit
7 REPLIES 7
curt larson_1
Honored Contributor
Solution

Re: Script

how about using sed

sed 's/\(.*\)\(SEV\)\(.*\)/\2\3/p' < inputfile

but if there is more then one occurance of this will print from the last occurance to the end of the line.
curt larson_1
Honored Contributor

Re: Script

or in ksh

grep SEV inputfile |
while read line
do
line=${line#*SEV}
print "SEV $line"
done

this will print from the first occurance of SEV
curt larson_1
Honored Contributor

Re: Script

or in ksh

grep SEV inputfile |
while read line
do
line=${line#*SEV}
print "SEV $line"
done

this will print from the first occurance of SEV
SAM_24
Frequent Advisor

Re: Script

Hi curt,

Thanks a lot.
I tried first option. It is working fine. I had to add -n option with sed.
If possible can you tell what sed command does.
I am not getting.

Raj
Never quit
curt larson_1
Honored Contributor

Re: Script

s does a substitution. s/pattern/replacement/flags
this substitutes 'replacement' for 'pattern'. the p flag prints the line if a successful substitution is done.

the regular expersion used for the pattern consists of:
'.' which matches any single character except newline.
'*' which matches any number (including zero) of the single character (including a character specified by a regular expression) that immediately precedes it.

\(\) saves the pattern enclosed between '\(' and '\)' into a special holding space (to a maximum of nine). They can be replayed in substitutions by the escape sequences \1 to \9.

\n matches the nth pattern previously saved by '\(' and '\)', where the previously saved patterns are counted from the left on the line.

so this substitution breaks the line into 3 patterns. .* for everything up to SEV. (this does means the line cannot begin with SEV). Then SEV and lastly everything after SEV (has to have at least one character). So, for the substitution to be performed the line must have a least one character before SEV and at least one character after. In which case the patterns 2 (SEV) and # (everything after SEV) will be printed.

a more exact sed command would be:
sed -e '/^SEV .*/p' -e 's/\(.*\)\(SEV .*\)/\2/p'

this will print the line if it starts with SEV, a blank and at least one character. The substitution will print from SEV to the end of the line only if SEV is followed by a blank space and at least one more character.

my previous sed command
sed 's/\(.*\)\(SEV\)\(.*\)/\2\3/p' sort of cheats a bit SEV followed by just a blank character will be printed. The cheat assumes that if there is a blank space it will be followed by a word.
SAM_24
Frequent Advisor

Re: Script

Thank you. Greatly appreciated.
Never quit
H.Merijn Brand (procura
Honored Contributor

Re: Script

Appreciation is expressed in points :)

N/A
Enjoy, Have FUN! H.Merijn