To compare the content of two text files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To compare the content of two text files
# 8  
Old 04-19-2012
Thank you it works fine. Smilie One more thing,
I have a text file as 1.txt with following content,
Code:
q34566788u
w2343556666t

I have used script file sample.sh with content,

Code:
#!/bin/ksh
echo "Enter the Value : "
read value
echo "$value"
sed '1s:\(.\{4\}\)\(.\{3\}\):\1$value:' 1.txt

I used the script file to replace the value 6678 in 1.txt file with the input value on run time which i try to read with variable name value. But its not replacing the value stored on variable instead its replacing $value i.e o/p
Code:
q345$value8u

.
I want the output as
Code:
q345(input value)8u

with input value on runtime in dash places.
Help me out. Smilie

---------- Post updated at 10:37 AM ---------- Previous update was at 10:34 AM ----------

I got the result,
I $value in sed command inside '$value ' it works.
Moderator's Comments:
Mod Comment How to use code tags

Last edited by Scrutinizer; 04-19-2012 at 12:58 PM..
# 9  
Old 04-19-2012
in sed command use a double quote instead a single quote.
# 10  
Old 04-19-2012
Code:
#!/bin/ksh 
mv /int/int1/abc.txt /int/int1/1.txt

echo "Enter the Value : " read value echo "$value" sed '1s:\(.\{4\}\)\(.\{3\}\):\1'$value':' 1.txt


In the above code i try to replace the value in 1.txt file
But its not working. When i use someother file already created in that path, its working.
but when i created the file at run time as in above code its not working.

when i used the file 1.txt its replacing the string and not reading the input value.

Any solutions ????? Smilie

Last edited by Franklin52; 04-20-2012 at 03:13 AM.. Reason: Please use code tags
# 11  
Old 04-19-2012
if you want replace value in the file you need to add
Code:
-i

option to sed. For a correct replacement of the keyword
Code:
$value

you need to change the single quote to double quote:
Code:
sed "1s:\(.\{4\}\)\(.\{3\}\):\1$value:" -i 1.txt

# 12  
Old 04-25-2012
Code:
sed "1s:\(.\{4\}\)\(.\{3\}\):\1$value:" -i 1.txt

The exact command is not working.

but without -i its working as
Code:
sed "1s:\(.\{4\}\)\(.\{3\}\):\1$value:" 1.txt

Thanks

If i want to the output of above command in the same text file
i.e 1.txt means wat is the command?

I tried as
Code:
sed "1s:\(.\{4\}\)\(.\{3\}\):\1$value:" 1.txt > 1.txt

but its not working

i want the result in the same file which is reading file. any help?

---------- Post updated 04-25-12 at 01:47 AM ---------- Previous update was 04-24-12 at 02:07 AM ----------

I am using the following code for replacing the value in the file.

Code:
sed "1s:\(.\{4\}\)\(.\{3\}\):\1$value:" 1.txt

I want the output of above command in the same file i am reading i.e 1.txt.
I tried out as following, its not working
Code:
sed "1s:\(.\{4\}\)\(.\{3\}\):\1$value:" 1.txt > 1.txt

Also tried for
Code:
sed "1s:\(.\{4\}\)\(.\{3\}\):\1$value:" 1.txt >temp.txt; mv temp.txt 1.txt

But No value is replaced.
Any ideas?

Last edited by Franklin52; 04-24-2012 at 04:30 AM.. Reason: Code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare content between two files

I have two files in unix environment with similer type of contain: Example: File1 File2 Milestone1 Milestone1 Milestone2 Milestone12 Milestone3 Milestone13... (11 Replies)
Discussion started by: Mrinal Mondal
11 Replies

2. Shell Programming and Scripting

Compare two fields in text files?

Hi, I have two text files, compare column one in both the files and if it matches then the output should contain the id in column one, the number and the description. Both the files are sorted. Is there a one liner to get this done, kindly help. Thank you File 1: NC_000964 92.33 ... (2 Replies)
Discussion started by: pulikoti
2 Replies

3. Shell Programming and Scripting

Script to compare two text files

i am working on a shell script and need help in the comparing part of it. for e.g. there two text files like this: file1.txt name1 name2 name3 file1 has to be comared with file2 defaultfile.txt name1 name2 name3 name4 and during comparision with defaultfile.txt if... (2 Replies)
Discussion started by: draghun9
2 Replies

4. UNIX for Dummies Questions & Answers

Compare two text files

Hello guys, I have file1 and file2, two text files containing various lines. I'm trying to find a way to compare file1 and file2: If the first 7 characters of a line in file2 match the first 7 characters of a line in file1, then do not do anything. Print out the lines of file1 (in file3,... (3 Replies)
Discussion started by: bobylapointe
3 Replies

5. Shell Programming and Scripting

Script to create a text file whose content is the text of another files

Hello everyone, I work under Ubuntu 11.10 (c-shell) I need a script to create a new text file whose content is the text of another text files that are in the directory $DIRMAIL at this moment. I will show you an example: - On the one hand, there is a directory $DIRMAIL where there are... (1 Reply)
Discussion started by: tenteyu
1 Replies

6. Shell Programming and Scripting

compare 2 files and show count same content.

$ cat File1 Non HTTP response code:java.net.ConnectException225073X 000000005143329Load time: 402335410224 Non HTTP response code: ava.net.ConnectException206423X 000000005143330Load time: 402305687161 Non HTTP response code: ava.net.ConnectException290212X 000000005143331Load time:... (1 Reply)
Discussion started by: ooilinlove
1 Replies

7. Shell Programming and Scripting

How to compare two text files

Hi Team, Could you please help me on below one .. etrademail1.txt etDefaultLogin=pdayanan mail=poojaaragam.dayanand@exchange.etr.comx employeeNumber=31567 etDefaultLogin=sudrupa mail=sudrupa.ayanand@exchange.etr.comx employeeNumber=318967 etDefaultLogin=gurathi (1 Reply)
Discussion started by: nivas_k2006
1 Replies

8. Shell Programming and Scripting

Compare 2 files and output only the different text.

I know the diff does this but it does output more info than just the different text (e.g. $ diff file1 file2 29a30 > /home/alex/Pictures/hello.jpg 1694a1696 > /home/alex/Pictures/hi.jpg ) How can I make it output only /home/alex/Pictures/hello.jpg /home/alex/Pictures/hi.jpg ? thank... (2 Replies)
Discussion started by: hakermania
2 Replies

9. Shell Programming and Scripting

Compare the content of 2 files

Hi Guys, What is the most effecient way to compare the content of 2 seperate files and extract the result of there is a match? We have 2 separate log files and we are trying to find the common errors from the 2 files. Thanks, Odogbolu98 :( (3 Replies)
Discussion started by: odogbolu98
3 Replies

10. UNIX for Dummies Questions & Answers

compare text files

This may be the 3rd time I'm posting this question. I'm so new here that I'm not even sure how to post! I'm trying to compare two files but can't do a line by line comparison so comm and diff are out. I've been told that I would need to use the awk programing language. I've looked up what I... (14 Replies)
Discussion started by: jimmyflip
14 Replies
Login or Register to Ask a Question