please help to merge file with awk or sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting please help to merge file with awk or sed
# 1  
Old 03-19-2010
please help to merge file with awk or sed

hi experts

please help me,thanks in advance

file1
Code:
arch : x86
install : pass 
make os : pass
make build kernel : pass=100 failed=45
usb storage pass  : The Linux Kernel Archives


file2
Code:
arch : ppc
install : failed 
make os : http://kernel.org
make build kernel : pass=104 failed=43
usb storage pass  : testing

file3
Code:
arch : arm
install : pass 
make os : pass
make build kernel : pass=200 failed=42
usb storage pass  : not test

file4 .....


I want to merge to file

Code:
| x86 | pass | pass | pass=100 failed=45 | http://kernel.org |
| ppc | failed | http://kernel.org | pass=104 failed=43 | testing |
| arm | pass | pass | pass=200 failed=42 | not test |

# 2  
Old 03-19-2010
Try this:
Code:
awk -F: '
f!=FILENAME{if(s)print s ;f=FILENAME;s="|" $NF "|";next}
{s=s $NF " |"}
END{print s}'  file1 file2 file3

# 3  
Old 03-19-2010
Code:
awk -F " : " '/^arch/ {print ""} {printf " | %s", $2}' file*

# 4  
Old 03-19-2010
Quote:
Originally Posted by Franklin52
Try this:
Code:
awk -F: '
f!=FILENAME{if(s)print s ;f=FILENAME;s="|" $NF "|";next}
{s=s $NF " |"}
END{print s}'  file1 file2 file3


not the right answer,thanks all the same

$ awk -F: 'f!=FILENAME{if(s)print s ;f=FILENAME;s="|" $NF "|";next}{s=s $NF " |"}END{print s}' file1 file2 file3
| x86| pass | pass | pass=100 failed=45 | The Linux Kernel Archives |
| ppc| failed |//kernel.org | pass=104 failed=43 | testing |
| arm| pass | pass | pass=200 failed=42 | not test |

---------- Post updated at 05:48 AM ---------- Previous update was at 05:47 AM ----------

Quote:
Originally Posted by rdcwayx
Code:
awk -F " : " '/^arch/ {print ""} {printf " | %s", $2}' file*


not the right answer,thanks all the same

$ awk -F " : " '/^arch/ {print ""} {printf " | %s", $2}' file1 file2 file3
| x86 | pass | pass | pass=100 failed=45 | The Linux Kernel Archives
| ppc | failed | The Linux Kernel Archives | pass=104 failed=43 | testing
| arm | pass | pass | pass=200 failed=42 | not test

---------- Post updated at 07:57 AM ---------- Previous update was at 05:48 AM ----------

any answer?
# 5  
Old 03-19-2010
In the following sed command, there are two tabs which were typed with control-v followed by the tab key (alternatively, you can use control-v control-i). The two tabs are underlined in red.
Code:
paste -s file1 file2 file3 | sed 's/[^:]*: *\([^____]*\)____*/| \1 /g; s/$/|/; s/  *|  */ | /g'

I did not see any tabs in your sample data. If they do occur, then that solution will not work. If needed, you can switch the delimiter used by the paste command with the "-d delimiter" option and change both occurrences of tab in sed to match it.

Regards,
Alister

---------- Post updated at 12:08 PM ---------- Previous update was at 11:50 AM ----------

Alternatively, without using paste and without depending on a tab delimiter (my personal preference):
Code:
for f in file1 file2 file3; do
    sed -n 's/^[^:]*: *//; s/ *$/ |/; H; ${x;s/\n/| /;y/\n/ /;p;}' "$f"
done


Last edited by alister; 03-19-2010 at 01:19 PM..
# 6  
Old 03-19-2010
Just modify Franklin52 solution.
Code:
# awk -F" :" '
f!=FILENAME{if(s)print s ;f=FILENAME;s="|" $NF " |";next}
{s=s $NF " |"}
END{print s}'  file1 file2 file3

If you don't like the output check your source file.
# 7  
Old 03-22-2010
Code:
for i in *.txt;do
	perl -ne '{chomp;my @t = split(":",$_,2); print $t[1]," | ";}' $i
	echo
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk can't open file; file merge attempt

Dear all, I have an AWK related issue. I have two data files; the first, FileA has fewer lines, the second, FileB has more. FileA is a subset of FileB . Both files are tab delimited. What I want to do? When the first two columns for FileA match the first two columns of FileB, I want to... (4 Replies)
Discussion started by: A_Human_Person
4 Replies

2. Shell Programming and Scripting

Merge values from multiple directories into one file in awk or bash

I am trying to merge or combine all $1 values in validation.txt from multiple directories into one new file and output it here tab-delimited:/home/cmccabe/Desktop/20x/total/total.txt. Each $2 value and the header would then be a new field in total.txt. I am not sure how to go about this as cat is... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

How to merge fields in a single file using awk ?

Hi, From a file, using: awk -F" " '{ if (NF == 6) print $1, $2, $3, $4, $5, $6; if (NF == 5) print $1, $2, $3, $4, $5; }' i printed out the required output. But i'm trying to merge the columns. Please look at the desired output. Any suggestions? Thanks Output: 00015 PSA1 ... (5 Replies)
Discussion started by: web2moha
5 Replies

4. Shell Programming and Scripting

Merge files and copy some data using sed or awk

Copy data from other file to paste cat file1: Name: server1.data.com data1 server1 running Name: server3.data.com data3 server3 running cat file2: server1 good server2 bad network not ok server3 good Output: (10 Replies)
Discussion started by: kenshinhimura
10 Replies

5. Shell Programming and Scripting

Merge 2 last columns in a csv file using sed or perl

Hello everyone, want to merge 2 last columns 3rd and 4rd (if it exists). I have this 1;2;1;1 2;3;1;1 1;1;2 1;2;3;4 1;1;2 1;2;3;1 Desired output: 1;2;1 1 2;3;1 1 1;1;2 1;2;3 4 1;1;2 1;2;3 1 (3 Replies)
Discussion started by: satir
3 Replies

6. UNIX for Dummies Questions & Answers

merge files along with file names (awk)?

Dear programmers, I have a question about conditionally merging multiple files and having their file names in the first column. Input files: file.1.extension file.2.extension file.3.extension file.4.extension ... file.1000.extension where each file looks like this (with multiple lines):... (5 Replies)
Discussion started by: wei.deng
5 Replies

7. UNIX for Dummies Questions & Answers

file merge using awk

Hi All, I have 2 csv files. 1st file has 10 columns and the 2nd file has 12 columns. The requirement is, if the 4th column of file1 matches with the 4th column of file2, then append the 12th column of file2 with file1. Both files have equal number of lines and the 4th column values are... (1 Reply)
Discussion started by: ganesh_248
1 Replies

8. Shell Programming and Scripting

two file merge with awk

Help I read a file that has 2 fields. look for in a second file the first field and update it with the second field of first file. file1 1131518fat11416.txt ../newaod/2001/04/2001-04-00129233-1.txt file2 INSERT INTO tabric VALUES... (2 Replies)
Discussion started by: mcarlo65
2 Replies

9. Shell Programming and Scripting

Merge lines in a file with Awk - incorrect output

Hi, I would like: FastEthernet0/0 is up, line protocol is up 0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored 0 output errors, 0 collisions, 0 interface resets Serial1/0:0 is up, line protocol is up 0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored, 0 abort 0... (14 Replies)
Discussion started by: mv652
14 Replies

10. Shell Programming and Scripting

merge two files into one file use awk

Hi, guys. I have one question: I have two files: passwd and shadow (the number of records in these files are not equal)the contents of them are below: passwd: ************** ftp:x:24:24: sshd:x:71:65: uucp:x:10:14: brownj:x:5005:1000: sherrys: x :5006:1000: ... ************* ... (2 Replies)
Discussion started by: daikeyang
2 Replies
Login or Register to Ask a Question