echo is too slow. HELP with Awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers echo is too slow. HELP with Awk
# 1  
Old 05-10-2007
echo is too slow. HELP with Awk

Hello All,

Below is a simple script i worte to find the 208th char in a file. If the char = "C" then I re-direct the line to a file called change.txt. If it is not "C" then I re-direct it to a file called delete.txt.

My problem is I have a file 0f 500K lines. this script is very slow. I am sure awk works faster. I jsut cant seem to get the awk syntax corect. I know I can call subtr in awk and bsed upon the answer tell it to print the line but I cant get it to work. Below is my original SLOW script.

Any help would be great!!!

HTML Code:
find_043()
{
read sLine
   status=$?
   if [ $status -ne 0 ]; then
      EOF="Y"
   else
      EOF=
   fi

   while [ ! "$EOF" ]
   do
      StatusFlg==`echo $sLine | cut -c208`
      
  if [ $StatusFlg == 'C' ]; then
   echo $sLine >> change.txt
else
   echo $sLine >> delete.txt
fi
read sLine
      status=$?
      if [ $status -ne 0 ]; then
         EOF="Y"
         else
         EOF=
      fi
done
}
cat FBU_043.dat | find_043
# 2  
Old 05-10-2007
nawk -f eja.awk myFile.txt

eja.awk:
Code:
BEGIN {
  FILE_change="change.txt"
  FILE_delete="delete.txt"
}

{ print >> ((substr($0,208,1) == "C") ? FILE_change : FILE_delete)}

# 3  
Old 05-10-2007
Thank you very much this works just fine.
# 4  
Old 05-12-2007
Quote:
Originally Posted by vgersh99
nawk -f eja.awk myFile.txt
Code:
[...]
{ print >> ((substr($0,208,1) == "C") ? FILE_change : FILE_delete)}
        ^^

In awk you don't need the ">>" operator to append.
You need it *only* if you want to preserve the content of a preexisting file.
# 5  
Old 05-14-2007
Thank you.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. OS X (Apple)

Slow FFT in ksh93 and awk.

Well i set myself a challenge to have an FFT function using nothing but ksh93 and awk. It took some serious jiggery pokery and concentration with all the brackets and '$' characters but here is the result. It is RADIX 2 only, but hey, show me another UNIX shell script that does it. It IS SLOW but... (17 Replies)
Discussion started by: wisecracker
17 Replies

2. Shell Programming and Scripting

Echo awk output from its variable

Stumped with the formatting of the awk output when used with variables, e.g.: awk -F, 'BEGIN {OFS=","} print {$2,$3,$4}' $infile1 produces the desired output (with rows), but when echoing the variable below, the output is one continuous line var1=$(awk -F, 'BEGIN {OFS=","} print... (4 Replies)
Discussion started by: ux4me
4 Replies

3. Shell Programming and Scripting

Combining echo and awk

i have a script that has many lines similar to: echo $var | awk -F"--" '{print $2}' as you can see, two commands are being run here. echo and awk. id like to combine this into one awk statement. i tried: awk -F"--" "BEGIN{print $var; print $2}" but i get error messages. (10 Replies)
Discussion started by: SkySmart
10 Replies

4. Shell Programming and Scripting

Problem with writing to output - awk, echo

Hello all, I wrote this command line for some calculation on my given input files based on another input file which is a txt file. while read BAM REGION; do samtools view $BAM $REGION | awk '{if ($2==0) print $0}' | wc -l >>log.txt; echo "$REGION"; done >> log.txt <regions.txt It takes... (4 Replies)
Discussion started by: @man
4 Replies

5. Shell Programming and Scripting

Making a faster alternative to a slow awk command

Hi, I have a large number of input files with two columns of numbers. For example: 83 1453 99 3255 99 8482 99 7372 83 175 I only wish to retain lines where the numbers fullfil two requirements. E.g: =83 1000<=<=2000 To do this I use the following... (10 Replies)
Discussion started by: s052866
10 Replies

6. Shell Programming and Scripting

Improve performance of echo |awk

Hi, I have a script which looks like this. Input file data1^20 data2^30 #!/bin/sh file"/home/Test.txt" while read line do echo $line |awk 'BEGIN { FS = "^" } ; { print $2 }' echo $line |awk 'BEGIN { FS = "^" } ; { print $1 }' | gzip | wc -c done <"$file" How can i... (4 Replies)
Discussion started by: chetan.c
4 Replies

7. Shell Programming and Scripting

cut, sed, awk too slow to retrieve line - other options?

Hi, I have a script that, basically, has two input files of this type: file1 key1=value1_1_1 key2=value1_2_1 key4=value1_4_1 ... file2 key2=value2_2_1 key2=value2_2_2 key3=value2_3_1 key4=value2_4_1 ... My files are 10k lines big each (approx). The keys are strings that don't... (7 Replies)
Discussion started by: fzd
7 Replies

8. Shell Programming and Scripting

The builtin split function in AWK is too slow

I have a text file that contains 4 million lines, each line contains 2 fields(colon as field separator). as shown: 123:444,555,666,777,888,345 233:5444,555,666,777,888,345 623:454,585,664,773,888,345 ...... Here I have to split the second field(can be up to 40,000 fields) by comma into an... (14 Replies)
Discussion started by: kevintse
14 Replies

9. Shell Programming and Scripting

Using echo in AWK

Hello, I have written the script below to extract specific data from a text file and then use the data extracted as parameters for another shell script call 'loto_tsim'. Everytime I run my script it complains about the 'echo' line. Am I missing something? I have spent hours and still cannot solve... (10 Replies)
Discussion started by: jermaine4ever
10 Replies

10. Shell Programming and Scripting

awk to echo ???

Hi All, I have the below command. awk 'BEGIN {printf("%1s","cat")}' > temp.txt can i do the same using echo ?If So, how? Thanks JS (2 Replies)
Discussion started by: jisha
2 Replies
Login or Register to Ask a Question

Featured Tech Videos