1834368 Members
2255 Online
110066 Solutions
New Discussion

Re: sed command help!!!

 
SOLVED
Go to solution
Cesar Fernandes
New Member

sed command help!!!

I am trying to perform an action and a subset of data in a line. I got it add what I wanted but it also added it before the day in the date.

Why does this command work?

sed 's/\(TAPES\:.*\) \(FILES\:.*\)/{s/ 0/
0//}g'

Data Before:
DATE: MAR 03 2004 CLASS: UX_PROD TAPES: 030043 030260 030233 030237 FILES: /oracle* /oracle1* /oracle2*

DATE: MAR 04 2004 CLASS: UX_PROD_OTHER TAPES: 030127 FILES: /sybdump

What I am trying to do DATA AFTER:

DATE: MAR 03 2004 CLASS: UX_PROD TAPES:
030043
030260
030233
030237 FILES: /oracle* /oracle1* /oracle2*

DATE: MAR 04 2004 CLASS: UX_PROD_OTHER TAPES:
030127 FILES: /sybdump

5 REPLIES 5
Jean-Luc Oudart
Honored Contributor
Solution

Re: sed command help!!!

Can we try awk ?

#!/bin/sh

awk '
{
flg=0;
n1=split($0,tab);
for(i=1; i <= n1;i++) {
if(tab[i]=="FILES:") flg=0;
if(flg==1) printf("
");
printf("%s ",tab[i]);
if(tab[i]=="TAPES:") flg=1;
}
printf("\n");
}'

Regards,
Jean-Luc
fiat lux
David Burgess
Esteemed Contributor

Re: sed command help!!!

Hi,

This uses awk rather than sed, but gets the result you wanted.

I've created 2 files. The input file is test.in and contains the following :-

DATE: MAR 03 2004 CLASS: UX_PROD TAPES: 030043 030260 030233 030237 FILES:
/oracle* /oracle1* /oracle2*

DATE: MAR 04 2004 CLASS: UX_PROD_OTHER TAPES: 030127 FILES: /sybdump

test.ksh contains :-


cat ./test.in | awk -F" " ' \
BEGIN {}
/UX_PROD TAPES/ {print $1" "$2" "$3" "$4" "$5" "$6" "$7"
"$8"
"$9"

"$10"
"$11" "$12" "$13" "$14" "$15}
/UX_PROD_OTHER TAPES/ {print $1" "$2" "$3" "$4" "$5" "$6" "$7"
"$8" "$9"
"$10" "$11" "$12" "$13" "$14" "$15}'

Run ksh ./test.ksh and the output is as you wanted :-

DATE: MAR 03 2004 CLASS: UX_PROD TAPES:
030043
030260
030233
030237 FILES: /oracle* /oracle1* /oracle2*
DATE: MAR 04 2004 CLASS: UX_PROD_OTHER TAPES:
030127 FILES: /sybdump

Regards,

Dave.
Mark Grant
Honored Contributor

Re: sed command help!!!

Or some nasty "perl" that Merijn could do in about nine bytes.

perl -ne '($s,$m,$e)=(/(.+TAPES:)(.+)(FILES.+)/);$m=~s/ /
/g;print "${s}${m}${e}\n"'
Never preceed any demonstration with anything more predictive than "watch this"
Cesar Fernandes
New Member

Re: sed command help!!!

Hey Jean-Luc,

Your awk works great!! But how can I make it so the
is after the numbers not before as I showed incorrectly.

Cesar Fernandes
New Member

Re: sed command help!!!

Hey Jean-Luc,

Never mind I got it.

awk '
{
flg=0;
n1=split($0,tab);
for(i=1; i <= n1;i++) {
if(tab[i]=="FILES:") flg=0;
if(flg==2) printf("
");
if(flg==1) flg=2;
printf("%s ",tab[i]);
if(tab[i]=="TAPES:") flg=1;
}
printf("\n");
}' $INPUT.tmp3 > $INPUT.tmp2


THANKS ALOT!!!

And Thanks to all who answered.