syntax for IF test or AWK for octal


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting syntax for IF test or AWK for octal
# 1  
Old 03-02-2012
syntax for IF test or AWK for octal

Using korn shell. I am reading a file line by line. If a record has a carriage return (octal 015) then I append the second record to the first record. Not all records have a carriage return. I have the unix shell script working with grep, but when my file has +100,000 records it runs slow. I would like to see if the shell script would run faster with an IF Test or AWK. I can't get the syntax right for the IF test. I don't know AWK, if you think this would run faster I would appreciate know how to code it.


1) I was doing some time testing to see where my shell script was slow. This runs fast.
Code:
#!/bin/ksh
file_in=file.txt
file_out=file.txt_out
cnt_in=0
while read LINE
do
   cnt_in=$(( cnt_in + 1 ))
   echo $LINE >> $file_out
done < $file_in

2) This runs slow
#!/bin/ksh
dir1=/tdcexports/db2/stage
file_in=file.txt
file_out=file.txt_out
cnt_in=0
while read LINE
do
   cnt_in=$(( cnt_in + 1 ))
   ck_carriage=`echo $LINE | grep -c ^M`
   echo $LINE >> $file_out
done < $file_in


3) I've tried various IF but didn't find the right syntax yet. This doesn't work properly:
Code:
if [ $LINE  = '\015' ]
     then echo equal
     else echo no equal
   fi


Thank you for your help.

Last edited by radoulov; 03-03-2012 at 03:11 AM.. Reason: Code tags!
# 2  
Old 03-03-2012
Just post a sample of your input file and an example of the desired/expected output.
# 3  
Old 03-03-2012
You can test with case:
Code:
case $line in 
  *^M) echo "$line ends in a carriage return"
esac

^M is entered as CTRL-V <RETURN>

in ksh93 / bash
Code:
case $line in 
  *$'\015') echo "$line ends in a carriage return"
esac

You can probably also use ksh/bash style conditionals:
Code:
if [[ "$line" = *^M ]]; then

echo "$line ends in a carriage return"
fi

---------- Post updated at 11:01 ---------- Previous update was at 10:36 ----------

Try this awk to glue together:
Code:
awk '{ while (sub(/\r$/,x) && getline p) $0=$0p }1' infile

# 4  
Old 03-03-2012
Quote:
If a record has a carriage return (octal 015) then I append the second record to the first record.
Please explain this process in minute detail and include before and after sample data which shows where the carriage-return character(s) may be found in a record and whether you need to RETAIN the carriage-return characters in the output or REMOVE them.

Is this one of those cases where carriage-return characters are valid in a string within a database record?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with gstat and octal value

Here are a few lines from my script. The problem I am having is that the statement for gstat returns this error: line 43: The statement is coming from gstat. Is there a way to fix it? Apparently -eq 02 is coming up as some octal value, I need it to be recognized as 02. Apparently in... (4 Replies)
Discussion started by: newbie2010
4 Replies

2. UNIX for Dummies Questions & Answers

Problem with test syntax in script

Hi guys here i'm again with more question The code below try to find an user and write everything about him if exist, so my problem appear on line of test, where the program test if the user has secondary groups related. The rest it's clear # usugrup.sh lista todas las caracteristicas de un... (3 Replies)
Discussion started by: Newer
3 Replies

3. UNIX for Dummies Questions & Answers

Deleting octal values

I have some junk values in my files 鵶„‰¼±¤¡ad. am able to find the octal values as below by using od command. 303 251 265 266 204 211 274 261 244 241 141 144 i want to know how to delete the octal this values . (5 Replies)
Discussion started by: vino.paal
5 Replies

4. Shell Programming and Scripting

How to check weather a string is like test* or test* ot *test* in if condition

How to check weather a string is like test* or test* ot *test* in if condition (5 Replies)
Discussion started by: johnjerome
5 Replies

5. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

6. Shell Programming and Scripting

syntax error for if statment test expression

Hi what's the correct way of writing if 1)if "$time_diff" -gt 5 then echo "killing hung process \n" fi 2)if test $time_diff -gt 5 then echo "killing hung process \n" fi where -time_diff=$(($Sam - $current_min)) and current_min=`date +%M` infact both are giving Syntax... (1 Reply)
Discussion started by: Anteus
1 Replies

7. Shell Programming and Scripting

[bash]printf octal instead of decimal

Hello everybody, I would like to understand why the printf function is returning me an octal value with this command : printf %4.4d 0010 returns 0008 printf %4.4d 10 returns 0010 Thanks for help. (3 Replies)
Discussion started by: dolphin06
3 Replies

8. Shell Programming and Scripting

syntax error in shell test: unknown operator

Hi All, can some one figure out the syntax issue here. How to overcome this? #!/bin/sh $ HFR_MAIL=NO $ PRP_MAIL=NO $ MC_MAIL=NO $ if && && ]; then > echo "NO " > else > echo "YES" > fi test: unknown operator NO $ if && && ]; then > echo "NO" > else > echo "YES" >... (4 Replies)
Discussion started by: shellscripter
4 Replies

9. UNIX for Dummies Questions & Answers

Display permissions in octal format

Hi, Is there any way to display the permissions in octal format rather than "rwxrwxrwx" format. I have a file and i want to see the permissions in octal format. Please help. (11 Replies)
Discussion started by: venkatesht
11 Replies

10. Programming

octal and strings

hi i have a peculiar problem...i have a number of stirngs separated by a '/0'. it looks somethings like: char test="abk\0jsdhj\01234\0" actually i will be reading this from a file or something... im supposed to change the \0 to a ',' but in C the octal representation starts with a '\'...... (1 Reply)
Discussion started by: strider
1 Replies
Login or Register to Ask a Question