Home » Infrastructure » Unix » How to get a chunk of a file?
How to get a chunk of a file? [message #229290] Fri, 06 April 2007 09:07 Go to next message
lalit.thawani
Messages: 11
Registered: April 2007
Junior Member
Hi All. ! I am stuck in the following
suppose i have

$ cat file

hi. this is the line 1
this is line 2
START This is what
i want.This is what i want
this is what i want
END
scs;jdfsj
jsjdsjk;dk


And i need the output file content as

START This is what
i want.This is what i want
this is what i want
END


Thanks in advance

[Updated on: Fri, 06 April 2007 09:08]

Report message to a moderator

Re: How to get a chunk of a file? [message #229292 is a reply to message #229290] Fri, 06 April 2007 09:21 Go to previous messageGo to next message
Mahesh Rajendran
Messages: 10707
Registered: March 2002
Location: oracleDocoVille
Senior Member
Account Moderator
What is the pattern?
Would there be a START /END always
or
between line3 and line6
or
what?
Re: How to get a chunk of a file? [message #229293 is a reply to message #229292] Fri, 06 April 2007 09:27 Go to previous messageGo to next message
lalit.thawani
Messages: 11
Registered: April 2007
Junior Member
Would there be a START /END always---> Yes,you got it right!

between line3 and line6---> Nope, the pattern can be at any line(but not at the same line)

Regards

Re: How to get a chunk of a file? [message #229331 is a reply to message #229293] Fri, 06 April 2007 12:05 Go to previous messageGo to next message
Mahesh Rajendran
Messages: 10707
Registered: March 2002
Location: oracleDocoVille
Senior Member
Account Moderator
oracle@mutation#cat file
hi. this is the line 1
this is line 2
START This is what
i want.This is what i want
this is what i want
END
scs;jdfsj
jsjdsjk;dk
oracle@mutation#
oracle@mutation#sed -n '/START/,/END/p' file > anotherFile
oracle@mutation#
oracle@mutation#cat anotherFile
START This is what
i want.This is what i want
this is what i want
END

Re: How to get a chunk of a file? [message #229455 is a reply to message #229331] Sun, 08 April 2007 05:06 Go to previous message
lalit.thawani
Messages: 11
Registered: April 2007
Junior Member
Thanks a ton.!

The sed command perfectly solves my purpose.!

I have also worked on an alternative solution.(a script).
The sed command is better offcourse.
*********************************************************
startstring=START #assign the variables
endstring=END #assign the variables
flag=1 #sets the flag on

cat file|while read line
do
if [ $flag -eq 1 ]; #checks for flag which will be ON initially
then
echo $line|grep $startstring
if [ $? -eq 0 ];
then
flag=0 #if got the start string we set the flag OFF
fi

else
echo $line|grep $endstring
if [ $? -eq 0 ];
then
flag=1 #and again ON if get the end string
else
echo $line
fi

fi

done

****************************************************************

Let me know your comments on the script if possible
Thanks again.!
Previous Topic: How to call an sql script from Shell script
Next Topic: How to load data from a .csv file, having different name, on daily basis using sqlldr
Goto Forum:
  


Current Time: Fri Mar 29 08:57:19 CDT 2024