The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Compare multiple fields in file1 to file2 and print line and next line gillesc_mac Shell Programming and Scripting 7 03-16-2009 06:26 AM
Reading a file line by line and processing for each line sagarparadkar Shell Programming and Scripting 6 03-02-2009 11:59 AM
cat file1 read line-per-line then grep -A 15 lines down in fileb irongeekio Shell Programming and Scripting 6 01-28-2009 06:30 AM
I need suggestion on problem read a file line by line and do stuff madi3d8 Shell Programming and Scripting 3 01-15-2009 11:33 AM
SED help (remove line::parse again::add line) Malumake Shell Programming and Scripting 6 10-24-2007 06:02 PM

Reply
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 06-28-2009
web_developer's Avatar
web_developer web_developer is offline
Registered User
  
 

Join Date: Jun 2009
Location: Durham, NC
Posts: 3
how do you parse 1 line at a time of file1 ie. line(n) each line into new file

File 1
<html>ta da....unique file name I want to give file=>343...</html>
<html>da ta 234 </html>
<html>pa da 542 </html>
and so on...

File 2
343
234
542
and so on, each line in File 1 one also corresponds with each line in File 2

I have tried several grep, sed, while .. read, do, done scripts and to no avail.

i need a ksh script that will do the following
(readORawkOR???) 1 line at a time, >OR?? into its corresponding unique identifier in the HTML code, but the unique identifier in the code is not at the beginning or the ending of the line, its in the middle, I have <html> and </html> at the begining and end of each line..

Any example scripts would be great...

Thanks
  #2 (permalink)  
Old 06-28-2009
kshji's Avatar
kshji kshji is offline
Registered User
  
 

Join Date: Jun 2009
Location: Finland
Posts: 236
You can do this many way, but here is one example how to use parameter expansion.
Code:
#!/bin/ksh
# read lines from stdin
while read line
do
        # remove begin of line including <html>
        a1=${line#*<html>}
        # remove end of line including </html>
        a2=${a1%</html>*}
        # remove all char except numbers (replace not numbers with nothing)
        a3=${a2//[^0-9]/}
        print $a3
done
And then run it
Code:
chmod a+x thisfile
cat file1 | ./thisfile > file2
  #3 (permalink)  
Old 06-28-2009
web_developer's Avatar
web_developer web_developer is offline
Registered User
  
 

Join Date: Jun 2009
Location: Durham, NC
Posts: 3
in summary.. SEE Below

In file1, line 1 (<html>...unique identifier23432..</html>) needs to be > to the identifier in line 1 in file2 (creating a NEW file name for each record)(23432).html (creating new file based on unique identifier)

---------- Post updated at 08:51 AM ---------- Previous update was at 08:36 AM ----------

#!/bin/ksh

#create counter
cnt=0
# read lines from stdin
while read line
do
# remove begin of line including <html>
a1=${line#*<html>}
# remove end of line including </html>
a2=${a1%</html>*}
# remove all char except numbers (replace not numbers with nothing)
a3=${a2//[^0-9]/}
print $a3
#increment cnt for testing creation of new unique identifier
cnt=$(($cnt+1))
done > $cnt.html
$ksh test3.ksh

test3.ksh[17]: : bad substitution
$
This 1 file is created:
0 Jun 28 08:41 0.html
blank and no 1,2, 3 and so forth..

Any other ideas?

Quote:
Originally Posted by kshji View Post
You can do this many way, but here is one example how to use parameter expansion.
Code:
#!/bin/ksh
# read lines from stdin
while read line
do
        # remove begin of line including <html>
        a1=${line#*<html>}
        # remove end of line including </html>
        a2=${a1%</html>*}
        # remove all char except numbers (replace not numbers with nothing)
        a3=${a2//[^0-9]/}
        print $a3
done
And then run it
Code:
chmod a+x thisfile
cat file1 | ./thisfile > file2
Thank you but unfortunately, this will not create what I need..

in summary.. SEE Below

In file1, line 1 (<html>...unique identifier23432..</html>) needs to be > to the identifier in line 1 in file2 (creating a NEW file name for each record)(23432).html (creating new file based on unique identifier)

---------- Post updated at 08:51 AM ---------- Previous update was at 08:36 AM ----------

#!/bin/ksh

#create counter
cnt=0
# read lines from stdin
while read line
do
# remove begin of line including <html>
a1=${line#*<html>}
# remove end of line including </html>
a2=${a1%</html>*}
# remove all char except numbers (replace not numbers with nothing)
a3=${a2//[^0-9]/}
print $a3
#increment cnt for testing creation of new unique identifier
cnt=$(($cnt+1))
done > $cnt.html
$ksh test3.ksh

test3.ksh[17]: : bad substitution
$
This 1 file is created:
0 Jun 28 08:41 0.html
blank and no 1,2, 3 and so forth..

Any other ideas?

Quote:
Originally Posted by kshji View Post
You can do this many way, but here is one example how to use parameter expansion.
Code:
#!/bin/ksh
# read lines from stdin
while read line
do
        # remove begin of line including <html>
        a1=${line#*<html>}
        # remove end of line including </html>
        a2=${a1%</html>*}
        # remove all char except numbers (replace not numbers with nothing)
        a3=${a2//[^0-9]/}
        print $a3
done
And then run it
Code:
chmod a+x thisfile
cat file1 | ./thisfile > file2


---------- Post updated at 11:40 AM ---------- Previous update was at 08:51 AM ----------

Thank you but unfortunately, this will not create what I need..
  #4 (permalink)  
Old 06-28-2009
kshji's Avatar
kshji kshji is offline
Registered User
  
 

Join Date: Jun 2009
Location: Finland
Posts: 236
I'm not sure what you are trying, sort input file example and what you like to be result example.
Code:
while ...
do
     # a3 is the key value, look first example script
     > $a3.html
done
Code:
cnt=1
while read line
do
    # create/overwrite empty file using some variable value
    > $cnt.html
    # or put something to file
    print something > $cnt.html
   ((cnt+=1))
done
  #5 (permalink)  
Old 06-28-2009
web_developer's Avatar
web_developer web_developer is offline
Registered User
  
 

Join Date: Jun 2009
Location: Durham, NC
Posts: 3
Input file dfn and Output file(s) dfn

My input file is a list of html code for products that have a unique key as their id numbers in the description of the code..

test1
<html>(the code for product #####)</html> <==a complete webpage
i wanted to use the cnt value to represent a unique means of createing a new and different file fore each line in he test1 file so in essenct, it is creating a new html file for each line which I have tested and verified is seperated by a carrage return and no tabs or carage returns in the line itself.


filename.txt is another possible input file i tried to used a mv script to change the name of the cnt.html files created by the first script
Reply

Bookmarks

Tags
parse lines onto new file

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 09:44 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