1835259 Members
2672 Online
110078 Solutions
New Discussion

Re: sed command

 
SOLVED
Go to solution
Pavitra
Occasional Advisor

sed command

I Have attached the file. I need the file in the format
value
11 REPLIES 11
Cédric MELLON
Honored Contributor

Re: sed command

Hi,

you can do that with sed and awk
sed : add a separator character before the string "value" => "!value" for exemple
awk : use this character as begin of line and the carriage return as separator of fields

Hope that will helps,
Cedric.
wooot wooot wooot !!!
Peter Godron
Honored Contributor

Re: sed command

Hi,
not in sed/awk, but old-fashioned shell ;-)

#!/usr/bin/sh
a=1
while read record
do
if [ $a -eq 1 ]
then
value="$record"
a=`expr $a + 1`
else
count=`echo "$record" | awk '{print $3}'`
count2="`echo $count2` $count"
if [ $a -le 3 ]
then
a=`expr $a + 1`
else
echo $value $count2
a=1
count2=""
fi
fi
done < a.lis


Please also read:
http://forums1.itrc.hp.com/service/forums/helptips.do?#33 on how to reward any useful answers given to your questions.
Cédric MELLON
Honored Contributor
Solution

Re: sed command

Here is your command, tested ;-p

cat test | sed 's/value/!value/g' | awk 'BEGIN { RS = "!" ; FS = "\n" } {print $1 " " $2 " " $3 " " $4}'

Cedric.
wooot wooot wooot !!!
Dennis Handly
Acclaimed Contributor

Re: sed command

You can use awk. (Your example input contains an initial blank line that causes problems. Did you really want it? The scripts below assume it isn't there.)

awk '
{
s1 = $0
getline
s2 = $0
getline
s3 = $0
getline
print s1, s2, s3, $0
}' 284910.txt

The following sed command will also work:
sed -n 'N;N;N;s/\n/ /g;p;d' 284910.txt
Peter Nikitka
Honored Contributor

Re: sed command

Hi,

I assume you only want to print the real values, no the whole text.
I added a header - if you don't need it, drop the 'BEGIN' section.

awk 'BEGIN {printf("%s\t%s\t%s\t%s\n","value","insert","update","delete")}
NF==1 {if(val) printf ("%s\t%d\t%d\t%d\n",val,ins,upd,del);val=$1}
/^Rows inserted/ {ins=$3}
/^Rows updated/ {upd=$3}
/^Rows deleted/ {del=$3}
END {if(val) printf ("%s\t%d\t%d\t%d\n",val,ins,upd,del)}' yourfile

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Pavitra
Occasional Advisor

Re: sed command

Hey,
Thanz Peter, Cedric,Dennis and Peter..
I got the required output..:-)
Peter Nikitka
Honored Contributor

Re: sed command

Hi,

nice to hear, that our solutions were valuable! Because you are new to this forum - Welcome! - I want to put your attention to its points system:
http://forums1.itrc.hp.com/service/forums/helptips.do?#28

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Sandman!
Honored Contributor

Re: sed command

Yet another way to do the same thing using ex(1):

ex -s file < g/^value/ j 3 | p
EOF
Dennis Handly
Acclaimed Contributor

Re: sed command

>Sandman!: g/^value/ j 3 | p

Thanks, I was never able to figure out how to join multiple lines in vi until I saw this and reread the man page.

Though in visual mode, having to type ":j3" is not much easier than "JJJ"
Pavitra
Occasional Advisor

Re: sed command

thanx
Peter Nikitka
Honored Contributor

Re: sed command

Hi Pavitra,

to make it easier for others to find valuable solutions for their problems, please rate the answers to your questions.

Here you'll find a description about this:
http://forums1.itrc.hp.com/service/forums/helptips.do?#33

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"