Sponsored Content
Top Forums Shell Programming and Scripting Linux shell programming performance issue Post 302915099 by RudiC on Sunday 31st of August 2014 04:00:15 PM
Old 08-31-2014
You don't need awk (or similar) to improve the performance of your script. Just by the look on it, it can be seen that you run six commands (= six new processes) in the inner loop, times 50 for the lines in file 2, times millions for the lines in file1 (opening file2 millions times (even though buffered/cached)).

With your input data, and after cleaning out a few quirks in your code snippet, I find
Code:
time . XX
real    0m0.308s
user    0m0.192s
sys    0m0.119s

, while
Code:
time . YY
real    0m0.014s
user    0m0.012s
sys    0m0.000s

with YY being
Code:
while IFS='' read -r line
         do     while IFS=, read field1 field2
                        do      TMP=${line//$field1}
                                if [ $(( (${#line}- ${#TMP}) / ${#field1} )) -gt 1 ]
                                        then    sed  "s/"$field1"/"$field2"/2g"  <<<"$line" >> tmp.txt
                                        break
                                fi
                        done < file2
        done < file1
cat tmp.txt
TEXAS CALIFORNIA TX
DALLAS CALIFORNIA CALIFORNIA DA DA TEXAS

An even faster solution might be to use an array to hold file2's contents, and have the outer loop read file1, and an inner loop to iterate through the array doing the comparisons/modifications.

---------- Post updated at 22:00 ---------- Previous update was at 21:36 ----------

Modification using arrays; adapt to taste...:
Code:
unset i
while IFS=, read field1[++i] field2[i]; do : ; done < file2
while IFS='' read -r line
         do     for (( i=1; i<=${#field1[@]}; i++ ))
                        do      TMP=${line//${field1[$i]}}
                                if [ $(( (${#line}- ${#TMP}) / ${#field1[$i]} )) -gt 1 ]
                                        then    sed "s/"${field1[$i]}"/"${field2[$i]}"/2g"  <<<"$line" >> tmp.txt
                                        break
                                fi
                        done
        done < file1

Timing is similar to the first version; looks like the disk cache is quite powerful:
Code:
time . ZZ

real    0m0.015s
user    0m0.003s
sys    0m0.013s

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Can somebody advise any free Linux sever for shell programming?

Hi, everybody. I just wonder whether there are a couple of free Linux servers running as terminals where people can practice Unix Shell Programming? I'd like to set up one myself but unfortunatly can't do it. I can't switch to Linux now coz I run a couple of servers on my machine. Cygwin is... (3 Replies)
Discussion started by: belgampaul
3 Replies

2. News, Links, Events and Announcements

Announcing collectl - new performance linux performance monitor

About 4 years ago I wrote this tool inspired by Rob Urban's collect tool for DEC's Tru64 Unix. What makes this tool as different as collect was in its day is its ability to run at a low overhead and collect tons of stuff. I've expanded the general concept and even include data not available in... (0 Replies)
Discussion started by: MarkSeger
0 Replies

3. Shell Programming and Scripting

Sed issue in K Shell programming

I am doing the following script in k shell sed -i 's/FILENAME/$i/g' TEST/test$j.ctl > TEST/control$j.ctl In the file it replaces $i for all FILENAME, it doesnot replace with the value of i. I put single quotes like below sed -i 's/FILENAME/'$i'/g' TEST/test$j.ctl > TEST/control$j.ctl I... (9 Replies)
Discussion started by: toshidas2000
9 Replies

4. UNIX for Advanced & Expert Users

run win app on Linux -performance issue

We develop software for diagnostic tools for cars. we a use a portable PC(x86) runs Win98 to run our applications. Hence the working environment in the company is Windows, specifically we use BASIC to develop the GUI, communication functions, DLL, etc. and run them on the Win98 PC. We suggested... (1 Reply)
Discussion started by: raedbenz
1 Replies

5. UNIX for Advanced & Expert Users

FTP-Shell Script-Performance issue

Hello All, Request any one of Unix/Linux masters to clarify on the below. How far it is feasible to open a new ftp connection for transferring each file when there are multiple files to be sent. I have developed shell script to send all files at single stretch but some how it doesnt suit to... (3 Replies)
Discussion started by: RSC1985
3 Replies

6. UNIX for Dummies Questions & Answers

Linux machine performance issue.

One of our database server is suddenly became very slow and i have no clue what to do .Please help. I m sharing the performance inforamtion regarding cpu,harddisk,ram . ########CPU Information######## Machine Uptime Information: uptime 10:25:06 up 16:50, 1 user, load average: 5.84, 5.65,... (10 Replies)
Discussion started by: pinga123
10 Replies

7. Shell Programming and Scripting

Shell programming in Linux

Hi, I have been working on Sun Solaris since a long time. Recently I got to work on RH Linux. My Linux version details are: Linux 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:48 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux I have a simple command in my shell script: export BKPTAG=`date... (3 Replies)
Discussion started by: sagarparadkar
3 Replies

8. Red Hat

Performance issue in Linux

IN solaris, for network high-availability we are using IPMP concept, can u tell me in REDHAT LINUX what we are using... also pls share good step to read & understand the that concept... Also performance issue in linux what are step & cmd can u tell me??? (2 Replies)
Discussion started by: tiger09
2 Replies

9. Shell Programming and Scripting

Performance issue in shell script

Hi All, I am facing performance issue while rinning the LINUX shell script. I have file1 and file 2. File one is the source file and file 2 is lookup file. Need to replace if the pattern is matching in file1 with file2. The order of lookup file is important as if any match then exit... (8 Replies)
Discussion started by: ureddy
8 Replies

10. Shell Programming and Scripting

Performance Issue - Shell Script

Hi, I am beginner in shell scripting. I have written a script to parse file(s) having large number of lines each having multiple comma separated strings. But it seems like script is very slow. It took more than 30mins to parse a file with size 120MB (523564 lines), below is the script code ... (4 Replies)
Discussion started by: imrandec85
4 Replies
unix2dos(1)						      General Commands Manual						       unix2dos(1)

NAME
unix2dos - UNIX to DOS text file format converter SYNOPSYS
unix2dos [options] [-c convmode] [-o file ...] [-n infile outfile ...] Options: [-hkqV] [--help] [--keepdate] [--quiet] [--version] DESCRIPTION
This manual page documents unix2dos, the program that converts text files in UNIX format to DOS format. OPTIONS
The following options are available: -h --help Print online help. -k --keepdate Keep the date stamp of output file same as input file. -q --quiet Quiet mode. Suppress all warning and messages. -V --version Prints version information. -c --convmode convmode Sets conversion mode. Simulates unix2dos under SunOS. -o --oldfile file ... Old file mode. Convert the file and write output to it. The program default to run in this mode. Wildcard names may be used. -n --newfile infile outfile ... New file mode. Convert the infile and write output to outfile. File names must be given in pairs and wildcard names should NOT be used or you WILL lost your files. EXAMPLES
Get input from stdin and write output to stdout. unix2dos Convert and replace a.txt. Convert and replace b.txt. unix2dos a.txt b.txt unix2dos -o a.txt b.txt Convert and replace a.txt in ASCII conversion mode. Convert and replace b.txt in ISO conversion mode. unix2dos a.txt -c iso b.txt unix2dos -c ascii a.txt -c iso b.txt Convert and replace a.txt while keeping original date stamp. unix2dos -k a.txt unix2dos -k -o a.txt Convert a.txt and write to e.txt. unix2dos -n a.txt e.txt Convert a.txt and write to e.txt, keep date stamp of e.txt same as a.txt. unix2dos -k -n a.txt e.txt Convert and replace a.txt. Convert b.txt and write to e.txt. unix2dos a.txt -n b.txt e.txt unix2dos -o a.txt -n b.txt e.txt Convert c.txt and write to e.txt. Convert and replace a.txt. Convert and replace b.txt. Convert d.txt and write to f.txt. unix2dos -n c.txt e.txt -o a.txt b.txt -n d.txt f.txt DIAGNOSTICS
BUGS
The program does not work properly under MSDOS in stdio processing mode. If you know why is that so, please tell me. AUTHOR
Benjamin Lin - ( blin@socs.uts.edu.au ) MISCELLANY
Tested environment: Linux 1.2.0 with GNU C 2.5.8 SunOS 4.1.3 with GNU C 2.6.3 MS-DOS 6.20 with Borland C++ 4.02 Suggestions and bug reports are welcome. SEE ALSO
dos2unix(1) 1995.03.31 unix2dos v2.2 unix2dos(1)
All times are GMT -4. The time now is 01:24 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy