Sponsored Content
Top Forums Shell Programming and Scripting Search field in text file and replace value Post 302769896 by doom4 on Thursday 14th of February 2013 12:09:02 AM
Old 02-14-2013
Quote:
Originally Posted by Chubler_XL
Danger with your code is that if the ID appears in another field (like serial number, location or name) It will replace that instead.
yeah you are right. I just work this out too

---------- Post updated 02-14-13 at 12:09 AM ---------- Previous update was 02-13-13 at 08:03 PM ----------

I am pretty sure that my code is horrible and that there are 1000x better ways to do what I did but this works now. I took into consideration that the number could appear in another spot so now it is impossible. I thought I just share maybe somebody else get inspired or wants to tell me how to solve it in a more appropriate manor.

Code:
while getopts ":f:o:c:" opt; do
  flags=1
  case $opt in
    f)
      inputfile=$OPTARG
      ;;
    o)
      outputfile=$OPTARG
      ;;
    c)
      if [[ $OPTARG == "" ]]
      then
      	consec=0
      else
      consec=$OPTARG
      fi
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      exit
      ;;
  esac
done


if [[ $flags == "" ]]
then
	echo "Usage: PDBid_change -f -o [-c]"
	echo "-f input file name"
	echo "-o output file name"
	echo "-c start id from 0 or value"
	exit
fi

if [[ $inputfile == "" ]]
then
	echo "Please provide an input file name (-f filename)"
	exit
else
	if ! [[ -e "$inputfile" ]]
	then
		echo "Input file does not exits"
		exit
	fi
fi

if [[ $outputfile == "" ]]
then
	echo "Please provide an output file name (-o filename)"
	exit
fi

cat /dev/null > $outputfile


function write_pdb() {
if  [[ ${#newid} == ${#position} ]]
then
	if  [[ ${#newid} == 1 ]]
	then
		echo "$line" | sed "s/A   $id/A   $newid/" >> $1
	elif [[ ${#newid} == 2 ]]
	then
		echo "$line" | sed "s/A  $id/A  $newid/" >> $1
	elif [[ ${#newid} == 3 ]]
	then
		echo "$line" | sed "s/A $id/A $newid/" >> $1
	else
		echo "$line" | sed "s/A$id/A$newid/" >> $1
	fi
elif [[ ${#newid} == 1 && ${#position} == 2 ]]
then
	echo "$line" | sed "s/A  $id/A   $newid/" >> $1
elif [[ ${#newid} == 1 && ${#position} == 3 ]]
then
	echo "$line" | sed "s/A $id/A   $newid/" >> $1
elif [[ ${#newid} == 1 && ${#position} == 4 ]]
then
	echo "$line" | sed "s/A$id/A   $newid/" >> $1
elif [[ ${#newid} == 2 && ${#position} == 3 ]]
then
	echo "$line" | sed "s/A $id/A  $newid/" >> $1
elif [[ ${#newid} == 2 && ${#position} == 4 ]]
then
	echo "$line" | sed "s/A$id/A  $newid/" >> $1
else
	echo "$line" | sed "s/A$id/A $newid/" >> $1
fi
}


cat $inputfile | while read line; do 

	atom=`echo $line | awk '{ print $1 }'`
	if  [[ "$atom" != "ATOM" && "$atom" != "TER" ]] 
	then
		echo "$line" >> $outputfile
	else
	 	
	 	if  [[ "$position" == "" ]]
	  	then
	  	position=`echo $line | awk '{ print $6 }'`
	  		if [[ "$consec" != "" ]]
	  		then
      		previous=$consec
      		else
      		previous=`echo $line | awk '{ print $6 }'`-5
      		fi      		
     	fi
     	
     	id=`echo $line | awk '{ print $6 }'`
     	if  [[ "$position" == "$id" ]]
     	then
     		newid=$[$previous+1]
     		write_pdb $outputfile
       	elif [ $atom == "TER" ]
     	then
     		id=`echo $line | awk '{ print $5 }'`
     		write_pdb $outputfile
      	else
      		previous=$newid
      		position=$id
      		newid=$[$previous+1]
      		write_pdb $outputfile     		
      	fi
	fi
done

 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
All times are GMT -4. The time now is 12:02 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy