Optimizing sed command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Optimizing sed command
# 1  
Old 09-20-2008
Optimizing sed command

Hi,

I would like to know if there is a command faster then sed, or a way to optimize my code...

Here is the description of my problem:
I have a file "file1" that is composed of 10 columns. The first one contain a list of ID, the second a list of number associated to the firs list of ID. The third one contains ID and the fourth the number associated, etc...

So I have:
Code:
ID  numb  ID  numb  ID   numb   ID   numb   ID   numb
ID  numb  ID  numb  ID   numb   ID   numb   ID   numb
...
...

The total number of ID is around 8000.

I want to find all the ID starting by a given string (eg. "460...") and extract the associated number. To do that I am simply using a grep command associated with cut and it is going pretty well.

Then I am doing an operation on the numbers extracted (MATLAB) and then I need to put the new values back in place in the file with 10 columns. To do that I am using:
Code:
x=file1
count=1
for k in $listID
do
  VALUE=`head -$count newval | tail -1`
  sed 's/'$k'  .\.....E.../'$k'  '$VALUE'/g' $x > extps
  \mv extps $x
  count=`expr $count + 1`
done

Where "newval" is the list of the new values computed, "listID" is the list of all the ID for which I need to change the associated number and "file1" is hte file with the 10 columns.

This code is working perfectly, but because I have a bunch of ID to change the repeted use of sed is taking A LOT of time (among 8000 ID I need to change about 500).

Does anybody know how could I speed up this code?

Thanks
# 2  
Old 09-21-2008
This should be faster. Assuming the fields are separated by 2 spaces and you have the listID's separated with a space like something as below:

Code:
#!/bin/sh

listID="123 789"
VALUE="your_new_value"

awk -v l="$listID" -v var="$VALUE" '
BEGIN{FS=OFS="  ";split(l,arr," ");for(i in arr){a[arr[i]]=arr[i]}}
{for(i=1;i<10;i+=2)if($i in a){$(i+1)=var}}
{print}' file1

If you have the listID's line by line in a file (should be a better choice):

Code:
#!/bin/sh

VALUE="your_new_value"

awk -v var="$VALUE" 'BEGIN{FS=OFS="  "}
NR==FNR{a[$0]=$0; next}
{for(i=1;i<10;i+=2)if($i in a){$(i+1)=var}}
{print}' file_of_listIDs file1

If the output is correct you can redirect the output to a temporary file and replace file1 with the temporary file.

Use nawk, gawk or /usr/xpg4/bin/awk on Solaris.

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Web Development

Optimizing JS and CSS

Yes. Got few suggestions. - How about minifying resources - mod_expires - Service workers setup https://www.unix.com/attachments/web-programming/7709d1550557731-sneak-preview-new-unix-com-usercp-vuejs-demo-screenshot-png (8 Replies)
Discussion started by: Akshay Hegde
8 Replies

2. Shell Programming and Scripting

Optimizing bash script

any way the following code can be optimized? FIRSTIN=$( HKIPP=$(echo ${TMFR} | egrep -v "mo|MO|Mo" | egrep "m |M ") HRAMH=$(echo ${TMFR} | egrep "h|H") HRAMD=$(echo ${TMFR} | egrep "d|D") HRAMW=$(echo ${TMFR} | egrep "w|W") HKIPPO=$(echo ${TMFR} |... (5 Replies)
Discussion started by: SkySmart
5 Replies

3. Shell Programming and Scripting

sed and awk giving error ./sample.sh: line 13: sed: command not found

Hi, I am running a script sample.sh in bash environment .In the script i am using sed and awk commands which when executed individually from terminal they are getting executed normally but when i give these sed and awk commands in the script it is giving the below errors :- ./sample.sh: line... (12 Replies)
Discussion started by: satishmallidi
12 Replies

4. Shell Programming and Scripting

Optimizing awk script

Can this awk statement be optimized? i ask because log.txt is a giant file with several hundred thousands of lines of records. myscript.sh: while read line do searchterm="${1}" datecurr=$(date +%s) file=$(awk 'BEGIN{split(ARGV,var,",");print var}' $line) ... (3 Replies)
Discussion started by: SkySmart
3 Replies

5. Shell Programming and Scripting

Optimizing the code

Hi, I have two files in the format listed below. I need to find out all values from field 12 to field 20 present in file 2 and list them in file3(format as file2) File1 : FEIN,CHRISTA... (2 Replies)
Discussion started by: nua7
2 Replies

6. Shell Programming and Scripting

Loop with sed command to replace line with sed command in it

Okay, title is kind of confusion, but basically, I have a lot of scripts on a server that I need to replace a ps command, however, the new ps command I'm trying to replace the current one with pipes to sed at one point. So now I am attempting to create another script that replaces that line. ... (1 Reply)
Discussion started by: cbo0485
1 Replies

7. OS X (Apple)

Optimizing OSX

Hi forum, I'm administrating a workstation/server for my lab and I was wondering how to optimize OSX. I was wondering what unnecessary background tasks I could kick off the system so I free up as much memory and cpu power. Other optimization tips are also welcome (HD parameters, memory... (2 Replies)
Discussion started by: deiphon
2 Replies

8. UNIX and Linux Applications

Optimizing query

Hi All, My first thread to this sub-forum and first thread of this sub-forum :) Here it is, Am trying to delete duplicates from a table retaining just 1 duplicate value out of the duplicate records for example : from n records of a table out of which x are duplicates, I want to remove x... (15 Replies)
Discussion started by: matrixmadhan
15 Replies

9. Shell Programming and Scripting

Optimizing for a Speed-up

How would one go about optimizing this current .sh program so it works at a more minimal time. Such as is there a better way to count what I need than what I have done or better way to match patterns in the file? Thanks, #declare variables to be used. help=-1 count=0 JanCount=0 FebCount=0... (3 Replies)
Discussion started by: switch
3 Replies

10. Filesystems, Disks and Memory

optimizing disk performance

I have some questions regarding disk perfomance, and what I can do to make it just a little (or much :)) more faster. From what I've heard the first partitions will be faster than the later ones because tracks at the outer edges of a hard drive platter simply moves faster. But I've also read in... (4 Replies)
Discussion started by: J.P
4 Replies
Login or Register to Ask a Question