Search field in text file and replace value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search field in text file and replace value
# 8  
Old 02-14-2013
Yes, sorry about the missing quotes around $line. I posted the suggestion "on the run" and didn't have time to try it out.

Here is a quick explanation of what is happening.

$0 = Current input line
substr(string, start, len) extract a substring starting at character position start for a length of len if len is not supplied it goes to end of string.

substr($0,23,4)+0==F
This get chars 23-26 of the input line
Adds zero (this converts the value to an integer which is needed as " 21" <> 21)
Compare this value with variable F if it's a match the code in {} is processed


{$0=substr($0,1,22) sprintf("%4d",T) substr($0,27)}
This rebuilds the input line with chars 1-22 + T (padded to 4 chars wide) + chars 27-end

1
This value is not zero in awk that is a true (not false) expression so the code in {} that follows is executed. In this case no code follows so the default action is performed which is "print the input line". Note that we may have changed the input line in the statement above (or we might not have all depending on the contents of T chars 23-26 of the input line)

This could also be expanded to:

Code:
awk -vF=$id -vT=$newid '
{ 
  id=substr($0,23,4)
  sub(/^ */,"",id)
  if(id == F) print substr($0,1,22) sprintf("%4d",T) substr($0,27)
  else print $0                                                   
}'

Because your using bash you could also use bash substrings (remember index starts at zero not 1), this solution will be much faster as it doesn't need to load and execute awk for each line processed:

Code:
function write_pdb() {
    ln=${line:22:4}
    ln=${ln##* }
    [ "$ln" = "$id" ] && line="${line:0:22}$(printf "%4d" $newid)${line:26}"
    echo "$line" >> $1
}


Last edited by Chubler_XL; 02-14-2013 at 05:31 PM..
# 9  
Old 02-14-2013
you are awesome man! thank you!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk search/replace specific field, using variables for regexp & subsitution then overwrite file

Hello, I'm trying the solve the following problem. I have a file which I intend to use as a csv called master.csv The columns are separated by commas. I want to change the text on a specific row in either column 3,4,5 or 6 from xxx to yyy depending upon if column 1 matches a specified pattern.... (3 Replies)
Discussion started by: cyphex
3 Replies

2. Emergency UNIX and Linux Support

Search and replace in text file

Hi, I have gigabytes of text files that I need to search for "&" and replace with "&amp". Is there a way to do this efficiently (like sed command)? Hope you could help. Thanks. (17 Replies)
Discussion started by: daytripper1021
17 Replies

3. Shell Programming and Scripting

Awk Search text string in field, not all in field.

Hello, I am using awk to match text in a tab separated field and am able to do so when matching the exact word. My problem is that I would like to match any sequence of text in the tab-separated field without having to match it all. Any help will be appreciated. Please see the code below. awk... (3 Replies)
Discussion started by: rocket_dog
3 Replies

4. Shell Programming and Scripting

Search replace strings between single quotes in a text file

Hi There... I need to serach and replace a strings in a text file. My file has; books.amazon='Let me read' and the output needed is books.amazon=NONFOUND pls if anybody know this can be done in script sed or awk.. i have a list of different strings to be repced by NONFOUND.... (7 Replies)
Discussion started by: Hiano
7 Replies

5. Shell Programming and Scripting

replace 3rd field of space delimited text file

how to replace the 3rd colum? Each line begins similarly, but they all ends variously. XX YY 03 variable text here XX YY 03 more variable text here XX YY 03 even more variable text here really long setence XX YY 03 variable numbers also appear 03 11. 123 456 XX YY 03 the occasional comma,... (4 Replies)
Discussion started by: ajp7701
4 Replies

6. Shell Programming and Scripting

text file search and replace with awk

hello all greeting for the day i have a text file as the following text.xml abcd<FIELD>123.456</FIELD>efgh i need to replace the value between <FIELD> and </FIELD> by using awk command. please throw some light on this. thank you very very much Erik (5 Replies)
Discussion started by: erikshek
5 Replies

7. Shell Programming and Scripting

search and replace a text in a file

Hi all, I have a requirement where i have to search data between strings 'SELECT' and ';' and replace this text as "SELECT.....;" so that i can export this extracted string into a excel cell. Please suggest on this. (5 Replies)
Discussion started by: goutam_igate
5 Replies

8. UNIX for Dummies Questions & Answers

search and replace a specific text in text file?

I have a text file with following content (3 lines) filename : output.txt first line:12/12/2008 second line:12/12/2008 third line:Y I would like to know how we can replace 'Y' with 'N' in the 3rd line keeping 1st and 2nd lines same as what it was before. I tried using cat output.txt... (4 Replies)
Discussion started by: santosham
4 Replies

9. Shell Programming and Scripting

How to search and replace text in same file

script is as below v_process_run=5 typeset -i p_cnt=0 pdata=/home/proc_data.log while do # execute script in background dummy_test.sh "a1" "a2" & p_cnt=$p_cnt+1 echo "data : $p_cnt : Y" >> $pdata done file created with following data in... (1 Reply)
Discussion started by: Vrgurav
1 Replies

10. Shell Programming and Scripting

automating file search and replace text

Hi, I am trying something like this: Let's say I have a file called File1 with contents: x=-0.3 y=2.1 z=9.0 I have another file, File2, with contents: xx= yy= zz= (nothing after "="). What I want to do is get the value of x in File1 and set it to xx in File2, i.e., xx=-0.3. And the... (3 Replies)
Discussion started by: ommatidia
3 Replies
Login or Register to Ask a Question