Linux shell programming performance issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Linux shell programming performance issue
# 1  
Old 08-30-2014
Hammer & Screwdriver Linux shell programming performance issue

Hi All,

can any one help me on this please.

Replace sting in FILE1.txt with FILE2.txt. FILE1.txt record must have at least one state is repeated once.But need to replace only from second occurrence in record in FILE1.txt

Condition: order of searching the records in FILE2.txt is impartent.

So when the FILE2.txt record entry is matched with FILE1.txt then break the loop and don't search again the FILE2.txt next record. see below exampe.

FILE1.txt
----------
Code:
TEXAS CALIFORNIA TEXAS
DALLAS CALIFORNIA CALIFORNIA DALLAS DALLAS TEXAS

FILE2.txt
------------
Code:
TEXAS,TX
DALLAS,DA
CALIFORNIA,CA
NEWYORK,NY


output:
-------
Code:
TEXAS CALIFORNIA TX

(TEXAS is matched so replaced TEXAS with TX in 2nd occurrence)
Code:
DALLAS CALIFORNIA CALIFORNIA DA DA TEXAS

(DALLAS and CALIFORNIA is matched more than once but as the order in FILE2.txt is impartent so DALLAS is coming first than CALIFORNIA so replacing DALLS only and breaking the loop and not searching again for CALIFORNIA)

I have implemented this using while loop and its working as expected but as the FILE1.txt have Millions of records and FILE2.txt have 50 records, so its taking Hours to complete. Any AWK solution for this to speed up the performance please ?

Last edited by Don Cragun; 08-30-2014 at 04:06 AM.. Reason: Remove QUOTE tags; add CODE tags.
# 2  
Old 08-30-2014
Please show us your shell script.
# 3  
Old 08-31-2014
Maybe use perl instead, uses hash arrays use unique to remove redundant elements.
# 4  
Old 08-31-2014
Hi,thanks for looking in to this. Here is the code.

Code:
echo "Replace the string matches only once or except FIRST occurence replace ALL."
        
	tot_cnt=`wc -l < $REP_FILE_PATH/$REP_FILE`

 	while IFS='' read -r line; do          (to preserve leading and trailing  spacees used IFS='' read -r )
  		i=0
        	while read line_1; do
           	 field[1]=`cut -d',' -f1 <<<"$line_1"`
            	field[2]="`cut -d',' -f2 <<<"$line_1"`
            	cnt=`echo -n "$line" | grep -o "${field[1]}" | wc -l`
           
            	if [[ "$cnt" -gt 1 ]] ; then
            	sed -e "s/"${field[1]}"/"${field[2]}"/2g"  <<<"$line" >> tmp.txt
            	break
            	fi
        	let i++
            	done < file2.txt
        done< file1.txt

# 5  
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

# 6  
Old 09-03-2014
Assuming GNU sed....

Create the sed file using FILE2.txt:
Code:
sed 's#\([^,]*\),\(.*\)#s/\1/\2/2g;t#' <FILE2.txt >FILE2.sed

Use that FILE2.sed as the commands for sed:
Code:
sed -f FILE2.sed <FILE1.txt >RESULT.txt

This User Gave Thanks to cjcox For This Post:
# 7  
Old 09-04-2014
Previous solution optimized:
Code:
sed 's#.*#s,&,2g#' FILE2.txt >FILE2.sed
sed -f FILE2.sed FILE1.txt >RESULT.txt

Now an awk solution that does not do a RE substitution, but a substitution word by word:
Code:
awk 'NR==FNR {r[$1]=$2; next} ($1 in r) {for (i=2; i<=NF; i++) if ($i==$1) $i=r[$i]} 1' FS=, FILE2.txt FS=" " FILE1.txt

This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question