Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Linux and Unix
Tag Cloud
access acer asus bios bsod computer crash desktop driver drivers error ethernet excel freeze gaming hard drive hardware hdmi internet laptop malware memory modem monitor motherboard network printer problem ram registry router security slow software sound toshiba trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Operating Systems > Linux and Unix >
Solved: ksh sed question

Reply  
Thread Tools
surfnschultz's Avatar
Member with 191 posts.
 
Join Date: Nov 2003
01-Mar-2005, 05:25 PM #1
Solved: ksh sed question
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's Avatar
Member with 191 posts.
 
Join Date: Nov 2003
01-Mar-2005, 05:43 PM #2
tried something now got a new question to add
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's Avatar
Trusted Advisor with 18,706 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
01-Mar-2005, 07:41 PM #3
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's Avatar
Member with 191 posts.
 
Join Date: Nov 2003
01-Mar-2005, 08:01 PM #4
excel won't do it
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's Avatar
Trusted Advisor with 18,706 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
01-Mar-2005, 08:09 PM #5
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's Avatar
Trusted Advisor with 18,706 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
02-Mar-2005, 02:57 PM #6
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's Avatar
Senior Member with 1,246 posts.
 
Join Date: Sep 2003
Experience: Linux~su
02-Mar-2005, 05:46 PM #7
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

Code:
sed s/^M/^M3/n3
sed s/^m/^M3/n4
sed s/$/^My^M/ 

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.
__________________
Gentoo Developer, and 64bit os user

"In feeding Mother Nature, you are fed in return" - Tsunam (2005). Concerning water conservation, and raising water tables.
surfnschultz's Avatar
Member with 191 posts.
 
Join Date: Nov 2003
02-Mar-2005, 07:15 PM #8
Almost home
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's Avatar
Senior Member with 1,246 posts.
 
Join Date: Sep 2003
Experience: Linux~su
02-Mar-2005, 08:32 PM #9
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)
__________________
Gentoo Developer, and 64bit os user

"In feeding Mother Nature, you are fed in return" - Tsunam (2005). Concerning water conservation, and raising water tables.
surfnschultz's Avatar
Member with 191 posts.
 
Join Date: Nov 2003
02-Mar-2005, 08:45 PM #10
good info
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's Avatar
Trusted Advisor with 18,706 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
03-Mar-2005, 11:19 AM #11
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's Avatar
Member with 191 posts.
 
Join Date: Nov 2003
03-Mar-2005, 01:59 PM #12
awk problem
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's Avatar
Member with 191 posts.
 
Join Date: Nov 2003
03-Mar-2005, 02:01 PM #13
Opps
It's NOT LWDsquid, it is LWDsquashman. Squashman rocks squid are ocean living creatures, that I love to eat.
Squashman's Avatar
Trusted Advisor with 18,706 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
03-Mar-2005, 03:23 PM #14
Paste is a great option for this instead of using SED. Hopefully you have it on your system.
surfnschultz's Avatar
Member with 191 posts.
 
Join Date: Nov 2003
03-Mar-2005, 03:35 PM #15
paste is that unix command
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.
Reply

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 10:13 PM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.