The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help me with parsing this file eamani_sun Shell Programming and Scripting 2 05-16-2008 04:39 PM
awk and file parsing devtakh Shell Programming and Scripting 4 05-06-2008 12:13 PM
Parsing a csv file chiru_h Shell Programming and Scripting 6 02-12-2008 09:33 AM
File Parsing jsusheel Shell Programming and Scripting 5 09-25-2007 11:25 AM
parsing file through awk bbeugie Shell Programming and Scripting 13 08-22-2006 02:21 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-11-2008
aol12123 aol12123 is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 6
Parsing a file

Hi,
Please help in parsing the following file and write separate files by parsing the file for the new file's content.


Main File:
------------

BEGIN
FileName: FirstFile.txt
Content of the File Start
AAAAAAAA
BBBBBBB
Content of the File End
END
BEGIN
FileName: SecondFile.txt
Content of the File Start
CCCCCC
DDDDDD
Content of the File End
END
BEGIN
FileName: ThirdFile.txt
Content of the File Start
EEEEEEE
FFFFFFFF
Content of the File End
END
BEGIN
FileName: FourthFile.txt
Content of the File Start
YYYYYYYY
ZZZZZZZZ
Content of the File End
END

OUTPUT:
Four files with the file names FirstFile.txt,SecondFile.txt,ThirdFile.txt and FourthFile.txt
and content of the files will be between BEGIN and END except FileName
eg:
FirstFile.txt:
##################
Content of the File Start
AAAAAAAA
BBBBBBB
Content of the File End
##################


Thanks in Advance
  #2 (permalink)  
Old 03-11-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 753
Code:
awk '/END/ {flag = 0}
     flag  {print > of}
     /FileName/ {flag = 1;of = $2}' file
  #3 (permalink)  
Old 03-12-2008
aajan aajan is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 80
Hope this Should Work!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

#!/usr/bin/ksh

i=1
j=1
cat samp | while read line
do
val="END"
value="BEGIN"
if [ $j -le 7 ]
then
if [[ $line != $val && $line != $value ]]
then
echo $line
echo $line >> File$i.txt
j=`expr $j + 1`
fi
if [ $j -eq 7 ]
then
j=0
i=`expr $i + 1`
fi
fi
done

Regards,
Anand
  #4 (permalink)  
Old 03-14-2008
aol12123 aol12123 is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 6
Thanks for Shamrock & Anand for the quick response and sorry for the late acknowledgement
  #5 (permalink)  
Old 03-24-2008
aol12123 aol12123 is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 6
Quick Question

Hi Shamrock & Anand,
I am trying to parse the similar (below mentioned sample.txt) file using the following script. But I am having the following issue:
1. Whitespaces of the file are not maintained.
2. Text (****************************************) is not getting printed.Its getting translated to the script file name.


Script:
#!/usr/bin/sh
i=1
cat sample.txt | while read line
do
var1="END"
var2="BEGIN"
toPrint=`echo $line | cut -c1-7`
toPrintText=`echo $line | cut -c8-`
elif [[ $toPrint == 'TOPRINT' ]]
then
echo toPrintText
elif [[ $line != $var1 && $line != $var2 ]]
then
echo $line >> File$i.txt
elif [[ $line == $var1 ]]
then
i=`expr $i + 1`
fi
done




=========
sample.txt
=========
BEGIN
First File Content Starts here
****************************************

Something or the other here
TOPRINT=This is the text to print on the consolee....
This is the end of the File content
END
BEGIN
Second File Content Starts here
****************************************

Something or the other here
TOPRINT=This is the text to print on the consolee....
This is the end of the File content
END
BEGIN
Third File Content Starts here
****************************************

Something or the other here
TOPRINT=This is the text to print on the consolee....
This is the end of the File content
END



Appreciate your response!

Thanks
  #6 (permalink)  
Old 03-24-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Quote:
Originally Posted by aol12123 View Post
1. Whitespaces of the file are not maintained.
2. Text (****************************************) is not getting printed.Its getting translated to the script file name.
Both of these are symptoms of inadequate quoting. The asterisk is a wildcard which gets expanded to a list of file names unless you quote it; perhaps it would be wise to use a different separator.

Quote:
#!/usr/bin/sh
i=1
cat sample.txt | while read line
do
(This, of course, is a Useless Use of Cat. The recommended idiom is
Code:
while read line
do
   : ...
done <sample.txt
... But don't worry too much about this stylistic detail.)

Quote:
var1="END"
var2="BEGIN"
toPrint=`echo $line | cut -c1-7`
toPrintText=`echo $line | cut -c8-`
elif [[ $toPrint == 'TOPRINT' ]]
then
echo toPrintText
See how far you can get just by judiciously quoting everything. The use of echo inside backticks is probably going to ruin some of your whitespace anyway -- quite frankly, I would not use a shell script for this. But try modifications like

Code:
var1="END"
var2="BEGIN"
toPrint="`echo "$line" | cut -c1-7`" 
toPrintText="`echo "$line" | cut -c8-`" 
elif [[ "$toPrint" == 'TOPRINT' ]]
	then
	echo "$toPrintText"
(I also added the missing dollar sign on the last line.)

Your "elif" seems to be missing the initial "if" but that's trivial, of course.

Last edited by era; 03-24-2008 at 03:28 AM..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:59 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0