Visit Our UNIX and Linux User Community


Replacing line 'i' of file1 with line 'j' of file 2


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing line 'i' of file1 with line 'j' of file 2
# 8  
Old 06-09-2011
the below line is wrong,
Code:
awk '{if(NR!=v1 ){print;next}system("head -v2  v3|tail -1")}' v1=${POSITIONS_CASE5[${j}]} v2=${B} v3=file02.dat

Instead of this, try this one

Code:
awk -v v1=${POSITIONS_CASE5[${j}]} v2=${B} v3=file02.dat  '{if(NR!=v1 ){print;next}system("head -"v2"  "v3"|tail -1")}'  ${TMPFILE}1 > ${TMPFILE}2

THis should work
This User Gave Thanks to kumaran_5555 For This Post:
# 9  
Old 06-09-2011
I tried you suggestion but I get this error message

Code:
awk: cmd. line:1: fatal: cannot open file `{if(NR!=v1 ){print;next}system("head -"v2"  "v3"|tail -1")}' for reading (No such file or directory)

# 10  
Old 06-09-2011
Quote:
Originally Posted by kumaran_5555
the below line is wrong,
Code:
awk '{if(NR!=v1 ){print;next}system("head -v2  v3|tail -1")}' v1=${POSITIONS_CASE5[${j}]} v2=${B} v3=file02.dat

Instead of this, try this one

Code:
awk -v v1=${POSITIONS_CASE5[${j}]} v2=${B} v3=file02.dat  '{if(NR!=v1 ){print;next}system("head -"v2"  "v3"|tail -1")}'  ${TMPFILE}1 > ${TMPFILE}2

THis should work

Try this,
Code:
awk -v v1=${POSITIONS_CASE5[${j}]}-v v2=${B}-v v3=file02.dat   '{if(NR!=v1 ){print;next}system("head -"v2"  "v3"|tail -1")}'   ${TMPFILE}1 > ${TMPFILE}2

This User Gave Thanks to kumaran_5555 For This Post:
# 11  
Old 06-09-2011
Sorry, I get the same error message...
# 12  
Old 06-09-2011
Here is the output i have got, using solaris with nawk. Can you check are we on the same page.
Code:
user@tonga> (/home/user) $ echo $a $b $f
2 1 test.txt
user@tonga> (/home/user) $ cat test.txt
TABLE1
TABLE1 A
TABLE2
TABLE2 B
TABLE3
TABLE4
TABLE4 C
TABLE4
user@tonga> (/home/user) $ /bin/nawk -v v1=$a -v v2=$b -v v3=$f '{if(NR==v1){system("head -"v2" "v3"|tail -1") }}' test.txt
TABLE1
user@tonga> (/home/user) $

This User Gave Thanks to kumaran_5555 For This Post:
# 13  
Old 06-09-2011
I tried to add a space before '-v'

Code:
    awk -v v1=${POSITIONS_CASE5[${j}]} -v v2=${B} -v v3=file02.dat   '{if(NR!=v1 ){print;next}system("head -"v2"  "v3"|tail -1")}'   ${TMPFILE}1 > ${TMPFILE}2

now it works.
Thanks a lot

Previous Thread | Next Thread
Test Your Knowledge in Computers #405
Difficulty: Medium
Cygwin consists of three parts: a dynamic-link library (DLL) as an API compatibility layer in the form of a C standard library providing a substantial part of the POSIX API functionality, an extensive collection of software tools and applications that provide a Unix-like look and feel, and an MOTIF-based window manager.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pick a line in file 2 basing on array in file1

Dear friends, I have two files. One with all IDs(in a single field) . And another with data(of which say field 5 is ID). I want to create an array of IDs using first file and while reading second file if the ID appears in the array I need to print $0 else skip. After a long gap I am... (6 Replies)
Discussion started by: paresh n doshi
6 Replies

2. Shell Programming and Scripting

Using regex's from file1, print line and line after matches in file2

Good day, I have a list of regular expressions in file1. For each match in file2, print the containing line and the line after. file1: file2: Output: I can match a regex and print the line and line after awk '{lines = $0} /Macrosiphum_rosae/ {print lines ; print lines } ' ... (1 Reply)
Discussion started by: pathunkathunk
1 Replies

3. Shell Programming and Scripting

Replacing a line in a file

Hi all, I need to replace a line export TZ=xxxxxxxx with the line export TZ=$1 Now, "xxxxxxxx" in the above line is some unknown string and $1 is a parameter. I want the content of $1 to be replaced with "xxxxxxxx". Kindly help me how to do this in the shell scripting. (5 Replies)
Discussion started by: ddeeps2610
5 Replies

4. Shell Programming and Scripting

Replacing a line in a file using sed

I have a file which has a list in it pop triangle people slow fast What I want to do is search this list and replace people with humans do the list looks like this: pop triangle human slow fast I think i use something like this.... if cat /list.txt | grep -q 'people' ; then (9 Replies)
Discussion started by: digitalviking
9 Replies

5. UNIX for Dummies Questions & Answers

Replacing first line of file by >filename

Hi All, I have a set of files named S5_SK1.chr01 S5_SK1.chr02 S5_SK1.chr03 ..... and the first line of these files is >SK1.chr01 >SK1.chr02 >SK1.chr03 ..... Can anyone suggest how I can change the first line of all these files with the filename itself? So my expected output for the first lines of... (14 Replies)
Discussion started by: pawannoel
14 Replies

6. UNIX for Advanced & Expert Users

how do you parse 1 line at a time of file1 ie. line(n) each line into new file

File 1 <html>ta da....unique file name I want to give file=>343...</html> <html>da ta 234 </html> <html>pa da 542 </html> and so on... File 2 343 234 542 and so on, each line in File 1 one also corresponds with each line in File 2 I have tried several grep, sed, while .. read, do,... (4 Replies)
Discussion started by: web_developer
4 Replies

7. Shell Programming and Scripting

Compare multiple fields in file1 to file2 and print line and next line

Hello, I have two files that I need to compare and print out the line from file2 that has the first 6 fields matching the first 6 fields in file1. Complicating this are the following restrictions 1. file1 is only a few thousand lines at most and file2 is greater than 2 million 2. I need to... (7 Replies)
Discussion started by: gillesc_mac
7 Replies

8. Shell Programming and Scripting

cat file1 read line-per-line then grep -A 15 lines down in fileb

STEP 1 # Set variable FILE=/tmp/mainfile SEARCHFILE =/tmp/searchfile # THIS IS THE MAIN FILE. cat /tmp/mainfile Interface Ethernet0/0 "outside", is up, line protocol is up Hardware is i82546GB rev03, BW 100 Mbps Full-Duplex(Full-duplex), 100 Mbps(100 Mbps) MAC address... (6 Replies)
Discussion started by: irongeekio
6 Replies

9. Shell Programming and Scripting

Replacing a line in a file - HELP!!!

I have a problem in the following code ... while read line do #Get Line Number OLDLINE=`sed -n $Lineno $filename` echo "Un Changed Line : "$OLDLINE echo "Enter a New Pattern : " read NewPattern <&1 echo "NewPattern :"$NewPattern NEWLINE=`cat $filename | sed -n... (1 Reply)
Discussion started by: maxmave
1 Replies

10. UNIX for Advanced & Expert Users

replacing first line or lines in a file

hey guys, how do i replace only a line within a file without messing up the rest of the contents of the file? see, if possible can you guys give me a straight forward way to do this. i dont want a complex command. what i mean is i know i can accomplish this by using sed, well, i think i can,... (3 Replies)
Discussion started by: Terrible
3 Replies

Featured Tech Videos