Help on Sed/awk/getting line number from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help on Sed/awk/getting line number from file
# 1  
Old 04-07-2011
Java Help on Sed/awk/getting line number from file

I Have file1 with below lines :

#HostNameSelection=0 :NotUsed
#HostNameSelection=1 :Automatic
#HostNameSelection=3 :NotForced

I have file2 which has similar lines but with different values

I want to copy the changes from file1 to file2 ,line by line only if line begins with '#'.
for other lines i am doing pattern matching of LHS of file1 with LHS of file2 and copying new Values to RHS of file2.

Please Help.
# 2  
Old 04-07-2011
Code:
man comm
man diff

Code:
egrep -e '^#' file1 >f1
egrep -e '^#' file2 >f2
diff f1 f2

?
# 3  
Old 04-07-2011
this will give me the difference in the values of lines beginning with # , how to copy the value at same line?
# 4  
Old 04-07-2011
Please give more clue about how do your 2 files look like and how should your output look like
# 5  
Old 04-07-2011
reffile:
IP = 10.10.10.102;
#HostNameSelection = 0 ;
#DomainName = abc.com;#url1
Targetfile:
IP = 127.0.0;
#HostNameSelection = 1 ;
#DomainName = xyz.com;#url2

Targetfile should be modified as per the content of Reffile after running Shellscript of upgrade.
# 6  
Old 04-07-2011
You don't want just copy since you only want #line changes?

file1 is reffile
file2 is Targetfile

Code:
egrep -ve '^#' file2 >file2.tmp
egrep -e '^#' file1 >>file2.tmp
cat file2.tmp >file2
rm file2.tmp

# 7  
Old 04-07-2011
I dont want to disturb the file2,changes should be done at the same place as they appear in file2.

The modified # lines should not be appended at the end .

Targetfile:
IP = 127.0.0;
#HostNameSelection = 1 ;
host = abbb;
#DomainName = xyz.com;#url2
destination = annana;
userid = 123;


The Line & ordering should remain intact.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

2. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

3. Shell Programming and Scripting

Replace a pattern in a file with a generated number using sed or awk

my file has thousands of line but let me show what i want to achieve... here is one line from that file cat fileName.txt (2,'','user3002,user3003','USER_DATA_SINGLE',1,0,0,'BACKUP',2,NULL,0,450,NULL,NULL,'','2011-05-10... (13 Replies)
Discussion started by: vivek d r
13 Replies

4. Shell Programming and Scripting

AWK-grep from line number to the end of file

Does anyone know how to use awk to act like grep from a particular line number to the end of file? I am using Solaris 10 and I don't have any GNU products installed. Say I want to print all occurrences of red starting at line 3 to the end of file. EXAMPLE FILE: red green red red... (1 Reply)
Discussion started by: thibodc
1 Replies

5. Shell Programming and Scripting

Extract a number from a line in a file and sed in another copied file

Dear all, I am trying to extract a number from a line in one file (task 1), duplicate another file (task 2) and replace all instances of the strings 300, in duplicated with the extracted number (task 3). Here is what I have tried so far: for ((k=1;k<4;k++)); do temp=`sed -n "${k}p"... (2 Replies)
Discussion started by: mnaqvi
2 Replies

6. Shell Programming and Scripting

awk script: print line number n of another file

Hi, I wrote an awk script to analyse file A. I call the script with files A and B. File A has lines like: 000000033100001 000000036100001 000000039100001 The first 9 characters are interpreted as a line number; for each line number found I want to output this line number of file B. ... (13 Replies)
Discussion started by: kpg
13 Replies

7. UNIX for Dummies Questions & Answers

Using awk to get a line number to delete, piping through sed

Alright, I'm sure there's a more efficient way to do this... I'm not an expert by any means. What I'm trying to do is search a file for lines that match the two input words (first name, last name) in order to remove that line. The removal part is what I'm struggling with. Here is my code: echo... (4 Replies)
Discussion started by: lazypeterson
4 Replies

8. Shell Programming and Scripting

awk or sed for finding closest pattern to a line number

hi guys, I want to do pattern matching with awk or sed but I don't know how. here's what I want: I have a line number for a pattern that I have already found using grep, and I know a pattern like "---" that happens a few lines above that certain line number. I want to print out the chunk... (1 Reply)
Discussion started by: alirezan
1 Replies

9. Shell Programming and Scripting

sed/awk to insert comment at defined line number

Hi there, may someone easily help me on this : I want to insert a text in a specific line number like : linenumb2start=`cat memory_map.dld | nl -ba | egrep -i "label" | cut -f1` line2insert=`expr $linenumb2start + 2` and now I need to replace something like {} with {comment} at... (8 Replies)
Discussion started by: homefp
8 Replies

10. Shell Programming and Scripting

SED: Update Last Line with Number Lines in File

Hi, I have to update last line of a text file with the number of lines in that file. This last line will have text such as 0.0000 and I should replace this with number lines. If lines are 20 then it should be replaced with 00020. Any sed or awk cmd help would be appreciated (3 Replies)
Discussion started by: bmkux
3 Replies
Login or Register to Ask a Question