Replace tr/sed with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace tr/sed with awk
# 1  
Old 10-24-2007
Question Replace tr/sed with awk

There is a service that runs that we call multi-streaming that calls a shell script multiple times simultaneously. In this shell script is the following line:
Code:
tr '\r' '\n' < $POLLFILE.OUT | sed '/0000000000000016000A/d' > $POLLFILE

When I run this manually it produces the desired results, but when I run it via the service, the script errs at this line with a meaningless error message. I've put in a request to get my development group to improve error messaging, but since that may take too long, I was wondering if there was a way to use awk to do exactly what tr and sed do above.

Any ideas?
# 2  
Old 10-24-2007
If it runs from the command line but not from a 'service', it is probable that the the service is not able to find the tr or the sed utility. Try modifying your script to include the full pathname to tr and sed.
# 3  
Old 10-24-2007
You didn't say what the meaningless error message was...sometimes they only seem meaningless. Smilie

I would generally agree that differences in the PATH are the most common reason for scripts to fail when run under a batch. But "tr" and "sed" are both in /usr/bin, which should really be in the path always. Still, it is a good habit to use absolute paths when scripting.

I would be more concerned that $POLLFILE is not set, or is set to something wonky. Depending on its value, it could cause some very strange responses indeed.
# 4  
Old 10-24-2007
I failed to provide a key piece of info - this is MKS Toolkit, a unix emulator on a windows box.

Here is the error message:
Code:
Error executing sort script D:\nsbwork\SMARTV~1\SRTRAN~1\FILES\unpack4690.bat D:\nsbwork\POLLFILES\TL.10100934.IP\tl003140082160.IP. 
Unexpected return value of 1

Could this be more meaningless?

The unpack4690.bat contains this one line:
Code:
D:\Progra~1\MKSTOO~1\mksnt\sh.exe" D:/nsbwork/smartview/SRTRANSLATE/FILES/unpack4690.sh %1

The unpack4690.sh contains these lines:
Code:
#!D:\Progra~1\MKSTOO~1\mksnt\sh.exe

POLLFILE=$1

cp $POLLFILE `dirname $POLLFILE`/`basename $POLLFILE .IP`.packed
D:/nsbwork/smartview/SRTranslate/FILES/ibm4690 $POLLFILE
chmod 666 $POLLFILE.OUT
tr '\r' '\n' < $POLLFILE.OUT | sed '/0000000000000016000A/d' > $POLLFILE
rm $POLLFILE.OUT

The service that calls the unpack4690.bat provides the file name of the poll file, the windows batch script sends that as the parameter to the shell script, the cp to add packed to the end succeeds and the ibm4690 unpack program succeeds in producing the OUT file which indicates that the POLLFILE variable is working as it should. If I remove the tr line, the unpack4690.sh works perfectly, but then the output file doesn't match the format needed because of the changes that did not take place.

I think I need to push heavily on my dev group to get a better error message and to fix this. I tried specifying the path to tr in the script but I got the same results.

Thanks for the assistance!
# 5  
Old 10-24-2007
Quote:
Originally Posted by philplasma
I was wondering if there was a way to use awk to do exactly what tr and sed do above.
Any ideas?

Code:
awk '!/0000000000000016000A/{ sub("\r","\n")}{print}' "file"

pls double confirm the output..generally, the sub() function (gsub() too) is for substitution purposes.
# 6  
Old 10-25-2007
Quote:
Originally Posted by philplasma
Code:
tr '\r' '\n' < $POLLFILE.OUT | sed '/0000000000000016000A/d' > $POLLFILE

Without being able to reproduce your problem here i'm left to guesses. Still i think quoted line could be rewritten in a simpler way, leaving less room for probable errors. sed provides the same (in fact a superset of) abilities to manipulate text as tr does, so when you have to call sed anyways it can do tr's work too.

I don't know what "\r" and "\n" is on your system, but perhaps you will have to replace it with some native characters. In case you simply do a dos2unix-translation, that is, change CR/LFs to EOLs, the command would be "s/^M$//" on the unix machine, the "^M" being a CTRL-M character literally. However i have no idea how this done in Windows, so you will probably have to twiddle around with that, just replace the "\r" and the "\n" with the correct characters and it should work.

Code:
sed '/0000000000000016000A/d;s/\r/\n/g' $POLLFILE.OUT > $POLLFILE

bakunin
# 7  
Old 10-25-2007
Thanks for the really helpful tips, but so far no luck. I need to use tr because both awk and sed are getting stuck on the line length. The length of the lines in this pollfile is greater than 20,000 characters, the awk command gives me this message:
Code:
awk: line 0 (NR=0): line too long: limit 20000

while the new and improved sed command gives me this message:
Code:
input line too long

When I run tr manually I get the desired results with the original line of code I started with. When it runs from the service I get the error message, so the problem must be within the service. I've logged a defect with the dev group in my company to see if they can isolate the problem in the service.

Thanks for all of your help, I was hoping awk could bypass the problem, but because of the line length it isn't working.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace my perl with awk or sed

My code below will print only the email address from all lines. I want to convert it with sed or awk.. also what if i just want to find only filenames. cat LIS_EMAIL | perl -wne'while(/+@+\w+/g){print "$&\n"}' Hoping to extract the filename such us .exe, .bin. From file that has scrambled... (8 Replies)
Discussion started by: invinzin21
8 Replies

2. UNIX for Beginners Questions & Answers

Replace using sed or awk

Hi, Need a help to replace a word if a pattern is found between the delimiters preferably using SED or AWK. below is the sample file that iam dealing with, need to match pattern 'application' if found replace the whole word between the delimiters and also print the lines that don't match.... (1 Reply)
Discussion started by: tech_frk
1 Replies

3. Shell Programming and Scripting

sed and awk -Find and Replace

All, I have thousands of lines in a file with following format DATA=_ONE_XXX_YYY_CCC_HHHG_ DATA1=_GGG_JJJJ_HHH_UUU_JJJJ_HHHH_LLL_ DATA3=_MMM_GG_NN_QQQQ_FFF_III_ I want to replace _ with . by ignoring the first (=_) and last (_) So that out put should looks like... (4 Replies)
Discussion started by: baluchen
4 Replies

4. Shell Programming and Scripting

HELP with awk or sed. Need to replace all IP address by 2

Hi, In a file, I have several time <IP>232.0.1.164</IP> ... <IP>232.0.1.135</IP> I need to replace all the random IP addresses , by 239.0.0.1 and 239.0.0.2 , alternatively. I try this grep "<IP>" tsp.xml | awk '{if(NR % 2)print $0}' | cut -d"<" -f2 | cut -d">" -f2 ... (3 Replies)
Discussion started by: FredMo
3 Replies

5. Shell Programming and Scripting

replace using pattern using awk or sed

I have file file_1.txt which contains: file_1.txt <tr> 1 MAIL # 1 TO src_1 </tr> <tr><td class="hcol">col_id</td> <td class="hcol">test_dt</td> <td class="hcol">user_type</td> <td class="hcol">ct</td></tr> <tr><td class="bcol">1</td> <td class="bcol">2012-09-20</td> <td class="bcol">a</td>... (2 Replies)
Discussion started by: sol_nov
2 Replies

6. Shell Programming and Scripting

sed or awk to replace a value in a certain line.

I have an input like following. *DEFINE_CURVE_TITLE Force for tool binder $# lcid sidr sfa sfo offa offo dattyp 3 0 1 .000000 125.00000 0.000 0.000 0 $# a1 ... (5 Replies)
Discussion started by: hamnsan
5 Replies

7. Shell Programming and Scripting

Pattern Replace using sed or awk

Hi , My file have data like 4:ALMOST NEVER PR 1925836 5:NEVER PR W DDA 5857610 6:NEVER PR WO DDA 26770205 but i want to replace the spaces before last numric digits out put should be like this 4:ALMOST NEVER PR=1925836 5:NEVER PR W DDA=5857610 6:NEVER PR WO... (7 Replies)
Discussion started by: max_hammer
7 Replies

8. UNIX for Dummies Questions & Answers

replace 0.00 with awk/sed

Hi I have a problem when i use awk or sed to replace characters in file. For example when I want to replace line like this : 00000O120100512 1.70 1.59 0.00 +7.280 I want to get a new line : 0000000O120100512 1.70 1.59 13.56 +7.280 In ksh : awk... (1 Reply)
Discussion started by: Artur
1 Replies

9. Shell Programming and Scripting

Replace Strings with sed or awk

Hello i need some help with the usage of sed. Situation : 2 textfiles, file.in , file.out In the first textfile which is called file.in are the words for the substitution. Every word is in a new-line like : Firstsub Secondsub Thridsub ... In the second textflie wich is called file.out is... (5 Replies)
Discussion started by: Kingbruce
5 Replies

10. Shell Programming and Scripting

Sed help to replace and then awk

Sed help echo "(200 rows affected)" | sed -e '/\(//p' | sed -e '/\)//p' | awk '{print $1}' I want output as "200" Please help me correct (2 Replies)
Discussion started by: pinnacle
2 Replies
Login or Register to Ask a Question