Replace same line in file 1 from file 2 with conditon
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
File 1:
19a9s
c9019
5777
File 2:
99a9s
89019
10919
Expected output
19a9s
89019
5777
So in file 1
on line#2
it's start with letter c
. which i would like to use it as condition if the line start with letter c
so i want to replace it from the second file which is file 2
I Tried with the following but failed to give me the expected output:
awk '
NR == FNR #for lines in first file
S[NR] = $0 #put line in array `S` with row number as index
next #starts script from the beginning
/^c/$0=S[FNR] #for line stared with `c`
$0=S[++count] #replace line by corresponded array element
1 #alias for `print $0`
' file2 file1
awk sed regular-expression
|
show 2 more comments
up vote
0
down vote
favorite
File 1:
19a9s
c9019
5777
File 2:
99a9s
89019
10919
Expected output
19a9s
89019
5777
So in file 1
on line#2
it's start with letter c
. which i would like to use it as condition if the line start with letter c
so i want to replace it from the second file which is file 2
I Tried with the following but failed to give me the expected output:
awk '
NR == FNR #for lines in first file
S[NR] = $0 #put line in array `S` with row number as index
next #starts script from the beginning
/^c/$0=S[FNR] #for line stared with `c`
$0=S[++count] #replace line by corresponded array element
1 #alias for `print $0`
' file2 file1
awk sed regular-expression
@don_crissti i don't know from where to start :(. sorry
– αԋɱҽԃ αмєяιcαη
Nov 17 at 19:44
@don_crissti sorry for touting my own garbage, but I don't think it's advisable and/or necessary to save an entire file in an array.awk
is perfectly able to process two files in parallel, line by line
– mosvy
Nov 17 at 19:53
@mosvy - no problem, while you're right I think the array method is easier to digest for someone not knowing awk but wanting to start learning.
– don_crissti
Nov 17 at 20:03
@don_crissti i edited the code but the same :S
– αԋɱҽԃ αмєяιcαη
Nov 17 at 20:43
@don_crissti same !
– αԋɱҽԃ αмєяιcαη
Nov 17 at 20:47
|
show 2 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
File 1:
19a9s
c9019
5777
File 2:
99a9s
89019
10919
Expected output
19a9s
89019
5777
So in file 1
on line#2
it's start with letter c
. which i would like to use it as condition if the line start with letter c
so i want to replace it from the second file which is file 2
I Tried with the following but failed to give me the expected output:
awk '
NR == FNR #for lines in first file
S[NR] = $0 #put line in array `S` with row number as index
next #starts script from the beginning
/^c/$0=S[FNR] #for line stared with `c`
$0=S[++count] #replace line by corresponded array element
1 #alias for `print $0`
' file2 file1
awk sed regular-expression
File 1:
19a9s
c9019
5777
File 2:
99a9s
89019
10919
Expected output
19a9s
89019
5777
So in file 1
on line#2
it's start with letter c
. which i would like to use it as condition if the line start with letter c
so i want to replace it from the second file which is file 2
I Tried with the following but failed to give me the expected output:
awk '
NR == FNR #for lines in first file
S[NR] = $0 #put line in array `S` with row number as index
next #starts script from the beginning
/^c/$0=S[FNR] #for line stared with `c`
$0=S[++count] #replace line by corresponded array element
1 #alias for `print $0`
' file2 file1
awk sed regular-expression
awk sed regular-expression
edited Nov 17 at 20:47
asked Nov 17 at 19:37
αԋɱҽԃ αмєяιcαη
439421
439421
@don_crissti i don't know from where to start :(. sorry
– αԋɱҽԃ αмєяιcαη
Nov 17 at 19:44
@don_crissti sorry for touting my own garbage, but I don't think it's advisable and/or necessary to save an entire file in an array.awk
is perfectly able to process two files in parallel, line by line
– mosvy
Nov 17 at 19:53
@mosvy - no problem, while you're right I think the array method is easier to digest for someone not knowing awk but wanting to start learning.
– don_crissti
Nov 17 at 20:03
@don_crissti i edited the code but the same :S
– αԋɱҽԃ αмєяιcαη
Nov 17 at 20:43
@don_crissti same !
– αԋɱҽԃ αмєяιcαη
Nov 17 at 20:47
|
show 2 more comments
@don_crissti i don't know from where to start :(. sorry
– αԋɱҽԃ αмєяιcαη
Nov 17 at 19:44
@don_crissti sorry for touting my own garbage, but I don't think it's advisable and/or necessary to save an entire file in an array.awk
is perfectly able to process two files in parallel, line by line
– mosvy
Nov 17 at 19:53
@mosvy - no problem, while you're right I think the array method is easier to digest for someone not knowing awk but wanting to start learning.
– don_crissti
Nov 17 at 20:03
@don_crissti i edited the code but the same :S
– αԋɱҽԃ αмєяιcαη
Nov 17 at 20:43
@don_crissti same !
– αԋɱҽԃ αмєяιcαη
Nov 17 at 20:47
@don_crissti i don't know from where to start :(. sorry
– αԋɱҽԃ αмєяιcαη
Nov 17 at 19:44
@don_crissti i don't know from where to start :(. sorry
– αԋɱҽԃ αмєяιcαη
Nov 17 at 19:44
@don_crissti sorry for touting my own garbage, but I don't think it's advisable and/or necessary to save an entire file in an array.
awk
is perfectly able to process two files in parallel, line by line– mosvy
Nov 17 at 19:53
@don_crissti sorry for touting my own garbage, but I don't think it's advisable and/or necessary to save an entire file in an array.
awk
is perfectly able to process two files in parallel, line by line– mosvy
Nov 17 at 19:53
@mosvy - no problem, while you're right I think the array method is easier to digest for someone not knowing awk but wanting to start learning.
– don_crissti
Nov 17 at 20:03
@mosvy - no problem, while you're right I think the array method is easier to digest for someone not knowing awk but wanting to start learning.
– don_crissti
Nov 17 at 20:03
@don_crissti i edited the code but the same :S
– αԋɱҽԃ αмєяιcαη
Nov 17 at 20:43
@don_crissti i edited the code but the same :S
– αԋɱҽԃ αмєяιcαη
Nov 17 at 20:43
@don_crissti same !
– αԋɱҽԃ αмєяιcαη
Nov 17 at 20:47
@don_crissti same !
– αԋɱҽԃ αмєяιcαη
Nov 17 at 20:47
|
show 2 more comments
2 Answers
2
active
oldest
votes
up vote
1
down vote
awk '
NR == FNR #for lines in first file
S[NR] = $0 #put line in array `S` with row number as index
next #starts script from the beginning
/^c/$0=S[FNR] #for line stared with `c`
1 #alias for `print $0`
' file2 file1
add a comment |
up vote
0
down vote
How far would
paste file[12] | sed 's/^c[^ ]* //; t; s/ .*$//;'
19a9s
89019
5777
get you?
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
awk '
NR == FNR #for lines in first file
S[NR] = $0 #put line in array `S` with row number as index
next #starts script from the beginning
/^c/$0=S[FNR] #for line stared with `c`
1 #alias for `print $0`
' file2 file1
add a comment |
up vote
1
down vote
awk '
NR == FNR #for lines in first file
S[NR] = $0 #put line in array `S` with row number as index
next #starts script from the beginning
/^c/$0=S[FNR] #for line stared with `c`
1 #alias for `print $0`
' file2 file1
add a comment |
up vote
1
down vote
up vote
1
down vote
awk '
NR == FNR #for lines in first file
S[NR] = $0 #put line in array `S` with row number as index
next #starts script from the beginning
/^c/$0=S[FNR] #for line stared with `c`
1 #alias for `print $0`
' file2 file1
awk '
NR == FNR #for lines in first file
S[NR] = $0 #put line in array `S` with row number as index
next #starts script from the beginning
/^c/$0=S[FNR] #for line stared with `c`
1 #alias for `print $0`
' file2 file1
edited Nov 17 at 20:59
answered Nov 17 at 20:09
αԋɱҽԃ αмєяιcαη
439421
439421
add a comment |
add a comment |
up vote
0
down vote
How far would
paste file[12] | sed 's/^c[^ ]* //; t; s/ .*$//;'
19a9s
89019
5777
get you?
add a comment |
up vote
0
down vote
How far would
paste file[12] | sed 's/^c[^ ]* //; t; s/ .*$//;'
19a9s
89019
5777
get you?
add a comment |
up vote
0
down vote
up vote
0
down vote
How far would
paste file[12] | sed 's/^c[^ ]* //; t; s/ .*$//;'
19a9s
89019
5777
get you?
How far would
paste file[12] | sed 's/^c[^ ]* //; t; s/ .*$//;'
19a9s
89019
5777
get you?
answered Nov 17 at 22:24
RudiC
3,0911211
3,0911211
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f482386%2freplace-same-line-in-file-1-from-file-2-with-conditon%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
@don_crissti i don't know from where to start :(. sorry
– αԋɱҽԃ αмєяιcαη
Nov 17 at 19:44
@don_crissti sorry for touting my own garbage, but I don't think it's advisable and/or necessary to save an entire file in an array.
awk
is perfectly able to process two files in parallel, line by line– mosvy
Nov 17 at 19:53
@mosvy - no problem, while you're right I think the array method is easier to digest for someone not knowing awk but wanting to start learning.
– don_crissti
Nov 17 at 20:03
@don_crissti i edited the code but the same :S
– αԋɱҽԃ αмєяιcαη
Nov 17 at 20:43
@don_crissti same !
– αԋɱҽԃ αмєяιcαη
Nov 17 at 20:47