Seaching and appeding a line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Seaching and appeding a line
# 1  
Old 07-15-2008
Seaching and appeding a line

Hi,

File1.txt(tab seperated)
id val1 val2
1 abc q,w,e,r,t
2 cde l,m,n,o
...

file2.txt(csv format)
id .. .. .. .. .. .. .. ..
1 .. .. .. .. .. .. .. ..
2 .. .. .. .. .. .. .. ..
3 .. .. .. .. .. .. .. ..

Now i want to the following,
seach by "id" from file1.txt in file2.txt and then whereever it finds id in file2.txt,appned the val1 and val2 field to the existing line of file2.txt at the end.
So the final output would look like this.

file3.txt(csv format)
id .. .. .. .. .. .. .. .. val1 val2
1 .. .. .. .. .. .. .. .. abc q,w,e,r,t
2 .. .. .. .. .. .. .. .. cde l,m,n,o
3 .. .. .. .. .. .. .. ..


thanks.
# 2  
Old 07-16-2008
Code:
root@isau02:/data/tmp/testfeld> cat file1
id      val1    val2
1       abc     q,w,e,r,t
2       cde     l,m,n,o
root@isau02:/data/tmp/testfeld> cat file2
id .. .. .. .. .. .. .. ..
1 .. .. .. .. .. .. .. ..
2 .. .. .. .. .. .. .. ..
root@isau02:/data/tmp/testfeld> join -1 1 -2 1 -o 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 1.2 1.3 file1 file2
id .. .. .. .. .. .. .. .. val1 val2
1 .. .. .. .. .. .. .. .. abc q,w,e,r,t
2 .. .. .. .. .. .. .. .. cde l,m,n,o

IIRC, the files normally have to be sorted when using join which makes it easy with the increasing ID column.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

How to read file line by line and compare subset of 1st line with 2nd?

Hi all, I have a log file say Test.log that gets updated continuously and it has data in pipe separated format. A sample log file would look like: <date1>|<data1>|<url1>|<result1> <date2>|<data2>|<url2>|<result2> <date3>|<data3>|<url3>|<result3> <date4>|<data4>|<url4>|<result4> What I... (3 Replies)
Discussion started by: pat_pramod
3 Replies

3. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

4. 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

5. UNIX for Advanced & Expert Users

Tcl script for seaching a string ignoring white spaces

Hi , I want to search a string in a file ignoring white spaces in TCL. My string is as follows Ouput-Maps " 1 1 1 0 " 1i am doing following set a *1*1*1*0* " }1 abc.log}] but it is not working. What is the wrong with the tcl script Thanks Gouranga Video... (0 Replies)
Discussion started by: mybapa3000@gmai
0 Replies

6. Shell Programming and Scripting

how to read the contents of two files line by line and compare the line by line?

Hi All, I'm trying to figure out which are the trusted-ips and which are not using a script file.. I have a file named 'ip-list.txt' which contains some ip addresses and another file named 'trusted-ip-list.txt' which also contains some ip addresses. I want to read a line from... (4 Replies)
Discussion started by: mjavalkar
4 Replies

7. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

8. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

9. UNIX for Dummies Questions & Answers

seaching field and getting complete record

hi, I have a file..... 1|3|4|5|6 1|3|4|4|5 now i ahave to search for value 4 in forth field and write that output to a file. if i do grep 4 file1 both lines are coming to output. can somebody help me building command. thanks and regards sandeep (4 Replies)
Discussion started by: mahabunta
4 Replies
Login or Register to Ask a Question