Script to check a file and replace some of the contents


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to check a file and replace some of the contents
# 1  
Old 03-23-2009
Script to check a file and replace some of the contents

Hi


I have a file that looks like this:


Code:
 
Line            0
animal         elephant
Line            1
animal         elephant
Line            2
animal         elephant
Line            3
animal         elephant

What i am aiming to do is with a script and an input value of a number between 0-3 is to change the elephant to dog so running the script with ./script.sh 2 will change the file so it looks like this


Code:
 
 
Line            0
animal         elephant
Line            1
animal         elephant
Line            2
animal         dog
Line            3
animal         elephant

So far i have this but it fails on "if [ $i -eq $CHANGE ]; then"


Code:
 
#!/bin/bash
NEXTLINE=0
#change internal field separator 
IFS=$'\n'
for i in $(cat inputfile); 
do
echo $i
if [ $NEXTLINE -eq $CHANGE ]; then
sed s/"animal         elephant"/"animal         dog"/ inputfile
exit 0
fi
if [ $i -eq $CHANGE ]; then
NEXTLINE=1
fi
done

I am not sure how to test if the line with Line 2 actually contains the value 2 in it, i'm guessing grep would be handy but i'm not really a scripter so I am a bit stuck. When the correct line with Line x is found then set NEXTLINE and go to the next line thru the for statement and then use sed to change the value.

I have had the following sugguested
Code:
# awk -v n="2" '/Line/ && $NF==n{print;getline;$NF=n;print;next}1' file

but don't understand how to use it.

Any help would be much appreciated.

Note: I have change the value of the data in the inputfile to something generic.

Thank you
Tara
# 2  
Old 03-23-2009
Code:
awk -v n="2" -v var="dog" '$2 == n {print;getline;sub("elephant","dog")}1' file

Regards
# 3  
Old 03-23-2009
thank you Franklin52

that worked great

cheers Smilie
# 4  
Old 03-24-2009
Hi Franklin,

Could you pls explain a bit on what each component in this line tells,

awk -v n="2" -v var="dog" '$2 == n {print;getline;sub("elephant","dog")}1' file
I know about the -v option, that it assigns a value to program variable but the other components are not clear.
# 5  
Old 03-24-2009
Quote:
Originally Posted by santoshbr4
Hi Franklin,

Could you pls explain a bit on what each component in this line tells,

awk -v n="2" -v var="dog" '$2 == n {print;getline;sub("elephant","dog")}1' file
I know about the -v option, that it assigns a value to program variable but the other components are not clear.
Code:
$2 == n {print;getline;sub("elephant","dog")}1

If the 2nd field is equal to n then print the current line, get the next line and substitute "elephant" with "dog".
The getline function is similar to the next statement but it don't pass the controle back to the top of the script.
The "1" is evaluated as true and the default action of awk is to print the current line, similar to {print}.

Regards
# 6  
Old 03-24-2009
Thank You very much.

Regards,
Santosh
# 7  
Old 03-25-2009
a.sh
Code:
sed -n '/'$1'\s*$/ !{
p
}
/'$1'\s*$/ {
        p
        n
        s/elephant/dog/
        p
}' filename

usage
Code:
./a.sh 2
./a.sh 3

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace contents of a file

Hello, I need help to replace a value by a new one. I've got a script, that will get directory size in ko, then write the size value return in a log file : The patch is given in crontab, and use with $1 in the script. I am looking for help to replace, after: the old value without to... (6 Replies)
Discussion started by: Aswex
6 Replies

2. UNIX for Dummies Questions & Answers

How To Replace Contents in a File?

How can i replace the contents in a particular line of a file. <FOLDERMAP SOURCEFOLDERNAME="FFCB-2012" SOURCEREPOSITORYNAME="Repo_DEV" TARGETFOLDERNAME="TEST" TARGETREPOSITORYNAME="Dev_Repo"/> For Example I want to replace the SOURCEREPOSITORYNAME="Repo_DEV" to... (3 Replies)
Discussion started by: Ariean
3 Replies

3. Shell Programming and Scripting

Replace Contents from One file into another

Hi Friends, I have two input files cat input1 chr1 100 200 chr1 200 300 chr2 300 400 cat input2 chr1 hello monday 10 20 . - . sometext chr1 hello monday 20 30 . - . sometext chr2 hello monday 30 40 . - . sometext Now, I want to replace $1, $4 and $5 of input2 with $1, $2 and... (1 Reply)
Discussion started by: jacobs.smith
1 Replies

4. Shell Programming and Scripting

Shell script to find and replace contents of files in directory

Hi all This is my first post. Please bear with me with all my mistakes. I started learning shell since couple of days now and this might be quite basic for all, i want to search for files in a directory containing specific string and replace it with new string. The code i wrote is quite bulky... (2 Replies)
Discussion started by: theprogrammer
2 Replies

5. Shell Programming and Scripting

Replace file contents from another

Hi Friends, I have a file1 with 5 columns a b c d e f g h i j I have file2 with 3 columns 1 2 3 4 5 6 I want to replace 3rd 4th and 5th columns in file1 with file2 contents, so the output would be a b 1 2 3 f g 4 5 6 Thanks (6 Replies)
Discussion started by: jacobs.smith
6 Replies

6. Shell Programming and Scripting

Replace partial contents of file with contents read from other file

Hi, I am facing issue while reading data from a file in UNIX. my requirement is to compare two files and for the text pattern matching in the 1st file, replace the contents in second file by the contents of first file from start to the end and write the contents to thrid file. i am able to... (2 Replies)
Discussion started by: seeki
2 Replies

7. Shell Programming and Scripting

unzip a file then further check contents inside it

Dear all I need to unzip a file then further it has many folders to perform tak inside each folder. What Task I need to perform I'm able to do it but once I unzip a folder then I'm not able to do cd folder/ into it. I have written following code for it for i in $( ls | grep Run_20111016 ) do... (2 Replies)
Discussion started by: Bhalinder
2 Replies

8. Shell Programming and Scripting

script to grep a pattern from file compare contents with another file and replace

Hi All, Need help on this I have 2 files one file file1 which has several entries as : define service{ hostgroup_name !host1,!host5,!host6,.* service_description check_nrpe } define service{ hostgroup_name !host2,!host4,!host6,.* service_description check_opt } another... (2 Replies)
Discussion started by: namitai
2 Replies

9. Shell Programming and Scripting

How to replace specific contents in a file?

From the existing file, I need to replace specific contents possibly with var every time when the user changes the var. e.g the contents in the file file.txt is 'My name is $n and I am $y years old' and every time user changed the var outside the file, the contents of the file should be created... (4 Replies)
Discussion started by: Emilywu
4 Replies

10. Shell Programming and Scripting

Replace contents of a file

Hi, I want to replace the contents of a file.I tried using : sed 's/01514581/01514582/' $p where 01514581 is the original value 01514582 is the replaced value $p is the file name (captured in a variable).. The output does not recognise $p If you give : sed... (2 Replies)
Discussion started by: shiroh_1982
2 Replies
Login or Register to Ask a Question