# Solved: ksh sed question



## surfnschultz (Nov 17, 2003)

I have a job that runs a line like this:

run "ds09082^M3^M3^M68190226^M68190227^My^M

The ^M is a return I want to repalce the digits after the 3rd ^M and 4th ^M with new numbers. I want to create a long list of these run statements and then execute the list once. I am trying to remove inventory itmes from our POS. I have a list with hundreds of line items that need to be run. I do not want to hand edit this. How can I use sed,awk,grep in ksh to make it happen. Here is what I am trying but to no avail.

While read line_by_line
do

sed s/^M/${line_by_line}/3
sed s/^M/${line_by_line}/4

done < delinv

In the delinv file is the line above that I want to modify. I get this error when I run the script
/run cannot be parsed. s/ ( i get ti twice since I use sed twice in the script. The ^M are real one charater control M's I could not reproduce them with VI so I cut and pasted a control m out of the file.

Not a clue as to what the error is saying or telling me other than it can't parse the line. Any help would be very much appreciated.

OK the race to solution is on.


----------



## surfnschultz (Nov 17, 2003)

OK I was able to sed the line and make changes, BUT I know I am not doing it correctly. I want to replace the string of number after the 3rd and 4th control M's. I want to keep all Control M's as they are part of the string.

well back at it.


----------



## Squashman (Apr 4, 2003)

Seems like you could do this real easy in Excel and then export that file from there. Been a while since I have played with SED and I am not quite sure what you need done.


----------



## surfnschultz (Nov 17, 2003)

Yeah the idea struck me too about excel, but the problem is excel interprets ^M as a carraige return and doesn't create the file correctly.

What I am trying to do is take a long list of numbers like 68192227 and wrap around that number the job string. So it looks like this

run "ds09082^M3^M3^M68190227^M68190227^My^M

I was able to the get the first part to work with

sed s/^/run "ds09082^M3^M3^M/

But now I need to add a control M and the number again followed by ^MY^M

I am also trying to do it by reading the data file and modifying it and write it out to a new file. So in the end I have my original list of item number and a seperate file with all the job lines in it, then I could submitthe jobs to our POS system and delete the inventory items in the POS system.


----------



## Squashman (Apr 4, 2003)

Gonna have to think about this one. I just spent the last 4 days with my kids and their grandparents. I am now thinking like a 4 year old instead of a 33 year old. Maybe I will be coherent tommorow.


----------



## Squashman (Apr 4, 2003)

The *$* represents the end of a line. So if you need to add text to the end of a line you could just use that in another sed statement.

sed s/$/68190227^M68190227^My^M/


----------



## tsunam (Sep 14, 2003)

sed s/^M/${line_by_line}/3

Well yes that one won't work because it'll find all ^m and append to the end of it.

For the ending you can do very similar to what LwdSquash said and do a

sed s/$/^My^M/ as a seperate sed funtion. That'll get the end

For the Control Characters try


```
[SIZE=2]sed s/^M/^M3/n3
sed s/^m/^M3/n4
sed s/$/^My^M/[/SIZE]
```
No guarentee to work because I am not at a linux machine right now....My laptop is at home. I forgot it today =/. The n(number) should be a replace the Nth repetition of the source with the replacement.


----------



## surfnschultz (Nov 17, 2003)

ok first off thanks for the help, but I need one or two more nugget of information.

sed script:
s/^/run "ds09082^M3^M3^M/
s/$/^M/
s/$/1/ (here is where I need help)
s/$/^MY^M^M/ (not sure why last ^M didn't show up)

sed script produces:
run "ds09082^M3^M3^M68190224^M1^MY^M

The problem is I need to place the same number in the string again. I thought the 3rd line with a 1 stdin would work. NOT! so how do I take the number that is in the data file and put it in the second position?

Where the 1 is after ^M I wanted the 1 to the number 68190224 from stdin, but I know this is wrong.

Also not sure why the last ^M didn't appear.

ok still going going going.......


----------



## tsunam (Sep 14, 2003)

awk $1 | sed s/$/^M/ | sed s/[\^M]/^M$1/ <---think this might work not positive (again no testing bed =/)(what i'm trying to have it to do is escape on the ^M so it replaces it with the info that is needed) Assuming that its only the item number in the file to begin with if not then its a bit more complicated
s/^/s/^/run "ds09082^M3^M3^M/
s/$/^MY^M^M/

Once i'm home (in about 2 hours I should be able to get it fully working fairly quickly)


----------



## surfnschultz (Nov 17, 2003)

I am about out of here too. I'll check back in the morning. basically I am trying to create a long list of delete jobs for our POS. Using item numbers. I have 340 items to remove. SO I was thinking if I could just run though each line and make the job line I would be good to go. always easier then it sound. I really appreicate your help alot. Each line would have two itme numbers and they would be the same number twice on the same line as shown above in previous part of thread.


----------



## Squashman (Apr 4, 2003)

Would it be easier to just create two files and paste them together. One with the first half of the command and one with the second half. I have done this in the past when I couldn't figure out ways to do it with sed.


----------



## surfnschultz (Nov 17, 2003)

I could not use the awk in my sed script.

I an doing a sed script that I run on the comand line like this:
sed -f sedtest data > dataout

sedtest looks like this:
s/^/run "ds09082^M3^M3^M/
s/$/^M/
s/$/$1/
s/$/^MY^M^M/

data looks like: (there are going to hundreds of single line items in the file)
68200001
68200068

dataout looks like:
run "ds09082^M3^M3^M68200001^M$1^MY^M
run "ds09082^M3^M3^M68200068^M$1^MY^M


What I need to do is:

create lines that look like this
run "ds09082^M3^M3^M68200001^M68200001^MY^M^M

So I am stuck at this point. I need to be able to put the number in where the $1 is and I need to get the last ^M added to the line.

OK I have a meeting urggggg for the next hour or so, I'll check back after that.

Again thanks for the help Tsunam and LWDsquid you guys ROCK!!!


----------



## surfnschultz (Nov 17, 2003)

It's NOT LWDsquid, it is LWDsquashman. Squashman rocks squid are ocean living creatures, that I love to eat.


----------



## Squashman (Apr 4, 2003)

Paste is a great option for this instead of using SED. Hopefully you have it on your system.


----------



## surfnschultz (Nov 17, 2003)

I don't do paste  This is AIX not sure about paste with unix. Anyway still searching for solution. so close, I'll get it sooner or later I'll finger this one out.


----------



## Squashman (Apr 4, 2003)

I use it on linux alot and I think it will work.

Just create the first part of your command to make your output file look like this.

run "ds09082^M3^M3^M68200001^M
run "ds09082^M3^M3^M68200068^M
etc....

Then use the paste command to paste the dataout to your data file

paste -d "" dataout data >> dataout2

This will make it look like this.
run "ds09082^M3^M3^M68200001^M68200001
run "ds09082^M3^M3^M68200068^M68200068
etc....

Then run another sed command to add on the:
^MY^M

Of course you would have to do this all within a shell script.


----------



## surfnschultz (Nov 17, 2003)

Cool let me try that out.


----------



## surfnschultz (Nov 17, 2003)

LWDsquashman you are the MAN!!!!  It worked like a charm. When I read the man apge on paste I was like oh yeah I do remember this command, it's just been 10 years since I used it. I have gotten so used to cut/copy/paste in microsoft land that it is messing me up.

OH well as I get help and work on this stuff it is all coming back to me.


So stoked get to sleep tonight without this problem going over and over in my head.

Tsunam you rock too bro, thanks for your help.


----------



## Squashman (Apr 4, 2003)

I am still trying to figure out how to do it with SED. I have not given up on that.


----------

