RE or Sed to manipulate a document


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting RE or Sed to manipulate a document
# 1  
Old 11-28-2008
RE or Sed to manipulate a document

I have a file with a list of about 2500 lines that I'd like to reformat using some regex in notepad++ or sed from my box (FreeBSD 6.3-RELEASE i386) to do the following:

convert from:
Code:
mycommand > mydocument0.htmlbcfn
mycommand > mydocument1.htmlcdcsfn
mycommand > mydocument2.htmldechfn
mycommand > mydocument3.htmlefchfsn
mycommand > mydocument4.htmlfpchn

convert it into:
Code:
mycommand bcfn > mydocument0.html ;
mycommand cdcsfn > mydocument1.html ;
mycommand dechfn > mydocument2.html ;
mycommand efchfsn > mydocument3.html ;
mycommand fpchn > mydocument4.html ;

Could someone help me with this please?
# 2  
Old 11-29-2008
Create a script named seddata:
Code:
#!/bin/sed -f
s/> mydocument\(.*\).html\(.*\)$/\2 > mydocument\1.html ;/

and, assuming your data is in input.txt, run this command:
Code:
seddata input.txt > output.txt

# 3  
Old 11-29-2008
Thanks Ken but I think I oversimplified my example which is why this might not work. Totally not your fault but also, I think you missed that I wanted to throw those characters into a variable and use sed again to append it to my command like so...
Quote:
mycommand > bcfn.htmlbcfn
mycommand > cdcsfn.htmlcdcsfn
mycommand > dechfn.1.htmldechfn
mycommand > efchfsn.htmlefchfsn
mycommand > fpchn.6.htmlfpchn

mycommand bcfn > bcfn.html ;
mycommand cdcsfn > cdcsfn.html ;
mycommand dechfn > dechfn.1.html ;
mycommand efchfsn > efchfsn.html ;
mycommand fpchn > fpchn.6.html ;
Eventually I want to convert it into a script #!/bin/sh and this is why additionally, I wanted to terminate each line (not sure if it's even necessary though). I have two of my own scripts that I made waaaay long ago that'll get the same job done...

Code:
#!/bin/sh
#Search and replace using sed. 
#Output live with cat.
echo "Syntax is as follows:
# file arg1 arg2"
read FILE RPL1 RPL2
cat $FILE | sed s/$RPL1/$RPL2/ ;
#!/bin/sh
#Search and replace using sed. 
#No output
echo "Syntax is as follows: 
# file arg1 arg2"
read FILE RPL1 RPL2
touch tmp.Ps23zp2s.2-Fpps3-wmmm0dss3
cat $FILE | sed s/$RPL1/$RPL2/ > tmp.Ps23zp2s.2-Fpps3-wmmm0dss3 ;
mv tmp.Ps23zp2s.2-Fpps3-wmmm0dss3 $FILE
echo "task completed"
#


-except.. I need to somehow have the characters thrown into a variable somehow to use sed again to append it as part of the command. I think the best way to do this is to have a sed sequence that works to do this then loop it through the file. Heck with that, given that a smaller test batch works, I can throw my command into the loop to get it all done under one script instead of two.

I'm still learning but what I think is relevant for all to know is that the characters after .html will be alphanumeric ([a-zA-Z0-9_] or \w) as I understand.


-----------------------------------------

What the heck.. here is the below so that all may follow what I'm trying to do...


Code:
--cut for brevity
man2web > apmd.8.htmlapmd
man2web > apropos.1.htmlapropos
man2web > apxs.8.htmlapxs
man2web > ar.1.htmlar
man2web > arch.1.htmlarch
man2web > arp.8.htmlarp
man2web > as.1.htmlas
man2web > ascii-xfr.1.htmlascii-xfr
man2web > ascii.7.htmlascii
man2web > asciitopgm.1.htmlasciitopgm
man2web > asctime.3.htmlasctime
man2web > ash.1.htmlash
man2web > asin.3.htmlasin
man2web > asinh.3.htmlasinh
man2web > assert.3.htmlassert
--cut for brevity


Last edited by phpfreak; 11-29-2008 at 04:36 AM..
# 4  
Old 12-02-2008
anybody know of how to do this to help me from having to edit each of the over 2500 lines?
# 5  
Old 12-02-2008
Quote:
Originally Posted by phpfreak
...
What the heck.. here is the below so that all may follow what I'm trying to do...


Code:
--cut for brevity
man2web > apmd.8.htmlapmd
man2web > apropos.1.htmlapropos
man2web > apxs.8.htmlapxs
man2web > ar.1.htmlar
man2web > arch.1.htmlarch
man2web > arp.8.htmlarp
man2web > as.1.htmlas
man2web > ascii-xfr.1.htmlascii-xfr
man2web > ascii.7.htmlascii
man2web > asciitopgm.1.htmlasciitopgm
man2web > asctime.3.htmlasctime
man2web > ash.1.htmlash
man2web > asin.3.htmlasin
man2web > asinh.3.htmlasinh
man2web > assert.3.htmlassert
--cut for brevity


Try it with awk,


Code:
awk -F'html| '  '$0=$1" "$NF" "$2" "$(NF-1)"html ;"' file > new_file



Code:
$ cat new_file
man2web apmd > apmd.8.html ;
man2web apropos > apropos.1.html ;
man2web apxs > apxs.8.html ;
man2web ar > ar.1.html ;
man2web arch > arch.1.html ;
man2web arp > arp.8.html ;
man2web as > as.1.html ;
man2web ascii-xfr > ascii-xfr.1.html ;
man2web ascii > ascii.7.html ;
man2web asciitopgm > asciitopgm.1.html ;
man2web asctime > asctime.3.html ;
man2web ash > ash.1.html ;
man2web asin > asin.3.html ;
man2web asinh > asinh.3.html ;
man2web assert > assert.3.html ;



From your sample I'm assuming that html pattern is constant in every record of your file ( as shown ).
# 6  
Old 12-03-2008
Code:
sed 's/\(.*\)\( > \)\(.*html\)\(.*\)/\1 \4\2\3 ;/' filename

# 7  
Old 12-04-2008
Found that both worked to allow me to create a script out of the file. Much appreciated rubin and summer.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Manipulate "&" in sed

Is it possible to manipulate the ampersand in sed? I want to sum +1 to all numbers in a file. Example that doesn't work:sed "s/\{1,2\}/$(expr & + 1)/g" filenameAlso, how to return 02 instead of 2 from expr? (8 Replies)
Discussion started by: teresaejunior
8 Replies

2. Shell Programming and Scripting

Manipulate columns using sed

Hello, I would like to remove the first column of lines beginning by a character (in my case is an open square bracket) and finishing by a space (or any other delimiter). For example: string1 string2 string3 to string2 string3 I found this previous topic: ... (1 Reply)
Discussion started by: stoyanova
1 Replies

3. Shell Programming and Scripting

Here document inside a here document?

Can we use a here document inside a here document? Something like this ssh user@remotehost << REMOTE sudo vserver vsernamename enter << VSERVER perform actions on vserver. VSERVER REMOTE (6 Replies)
Discussion started by: mnanavati
6 Replies

4. Shell Programming and Scripting

sed script to manipulate the /etc/passwd file

Hi. Can anybody help me with a script to extract usernames, shell and home_directory from the /etc/passwd file. I am stuck (2 Replies)
Discussion started by: Pauline mugisha
2 Replies

5. Shell Programming and Scripting

Manipulate lines with sed/awk

Hey All, I need to reorganize a file's text. Here is the source: host John_Doe filename "config.cfg"; hardware ethernet 98:10:3d:13:8f:98; fixed-address 10.10.10.29; } host Jane_Doe filename "config.cfg"; hardware ethernet 98:13:11:fd:5a:57; fixed-address 10.10.5.24; } host... (2 Replies)
Discussion started by: TheBigAmbulance
2 Replies

6. Shell Programming and Scripting

manipulate postscript via sed

dear all, on solaris10 for x86 i am trying to modify the creation date of a postscript file with sed in a csh script. sed is driving me crazy though...i think due to the spaces in the string i am trying to substitute?? part of the postscript file: %!PS-Adobe-3.0... (3 Replies)
Discussion started by: lada niva
3 Replies

7. Shell Programming and Scripting

Sed command to find, manipulate and replace a number

Hi, Im very new to the world of sed so I'm really not even sure if this is possible. What i need to do is read from a flat file and every time i see this line: VAL=123,456 I need to change 456 to 457 for every occurence of this line in the file. The numbers 123 and 456 are different for... (6 Replies)
Discussion started by: LT_2008
6 Replies

8. UNIX for Dummies Questions & Answers

using sed to manipulate text in files

Hi, I have a slight problem in trying to manipulate the text within a file using the "sed" command in that the text i need changed has "/" slashes in. I have a .sh script that scans the "/db/sybbackup/" directories for any .dmp file older than 2 days and then to >> the information to a file called... (3 Replies)
Discussion started by: Jefferson333
3 Replies

9. Shell Programming and Scripting

sed or other tool to manipulate data, including email addresses

I have a list of names and email addresses, like this. The <tab> markers are actually tabs. joe.blow <tab> joe.blow@wherever.com tom.t.hall <tab> tom.t.hall@wherever.com john.r.smith <tab> john.r.smith@wherever.com sally.jones <tab> sally.jones@state.or.us I want to parse the data so that... (3 Replies)
Discussion started by: manouche
3 Replies

10. Shell Programming and Scripting

Manipulate files

Hi everybody: I have a problem. I have a output files which have this pattern: number1 --space block1a - 7rows/10columns/65elements --space block1b - 7rows/10columns/65elements --space block1c - 7rows/10columns/65elements --space number2 --space block2a - 7rows/10columns/65elements... (0 Replies)
Discussion started by: tonet
0 Replies
Login or Register to Ask a Question