Visit Our UNIX and Linux User Community


to join the corresponding lines using shell commands or awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting to join the corresponding lines using shell commands or awk
# 1  
Old 12-10-2007
to join the corresponding lines using shell commands or awk

Suppose u have this file

gi_1 ABCDEFDHIJ KMNOPQRSTU VWXYZABCDE
gi_2 JKLMNOPQRS TUVWXYZABC DEFGHIJKLM
gi_3 PQRSTUVWXY ZABCDEFGHI JKLMNOPQRS
gi_4 CDEFGHIJKL MNOPQRSTUV WXYZABCDEF
gi_5 IJKLMNOPQR STUVWXYZAB CDEFGHIJKLM

FGHIJKLMNO PQRSTUVWXY ZABCDEFABC
NOPQRSTUVW XYZABCDEFG HIJKLMNOPQ
TUVWXYZABC DEFGHIJKLM NPQRSTUVWX
GHIJKLMNOP QRSTUVWXYZ ABCDEFGHIJ
NOPQRSTUVW XYZABCDEFG HIJKLMNOPQ

so the output shud be like that

gi_1 ABCDEFDHIJ KMNOPQRSTU VWXYZABCDE FGHIJKLMNO PQRSTUVWXY ZABCDEFABC
gi_2 JKLMNOPQRS TUVWXYZABC DEFGHIJKLM NOPQRSTUVW XYZABCDEFG HIJKLMNOPQ
gi_3 PQRSTUVWXY ZABCDEFGHI JKLMNOPQRS TUVWXYZABC DEFGHIJKLM NPQRSTUVWX
gi_4 CDEFGHIJKL MNOPQRSTUV WXYZABCDEF GHIJKLMNOP QRSTUVWXYZ ABCDEFGHIJ
gi_5 IJKLMNOPQR STUVWXYZAB CDEFGHIJKLM NOPQRSTUVW XYZABCDEFG HIJKLMNOPQ

And also note that like one have one time color part(5 lines) we have more than 4 -5 time color part so as to join them respectively.

Thanks


thanks
# 2  
Old 12-10-2007
was not something similar answered here?
# 3  
Old 12-10-2007
Quote:
Originally Posted by vgersh99
was not something similar answered here?
Dear vgersh,
nawk -f ag.awk myInputFile

ag.awk:
Code:

BEGIN {
RS=""
}
{
for(i=1; i<=NF; i++)
a[i] = (i in a) ? a[i] OFS $i : $i
}
END {
for(i=1; i in a; i++)
print a[i]
}

Yah exactly it is but i couldn't getdesired results as the result shown below using ur code (ag.awk)
gi_1 FGHIJKLMNO
ABCDEFDHIJ PQRSTUVWXY
KMNOPQRSTU ZABCDEFABC
VWXYZABCDE NOPQRSTUVW
gi_2 XYZABCDEFG
JKLMNOPQRS HIJKLMNOPQ
TUVWXYZABC TUVWXYZABC
DEFGHIJKLM DEFGHIJKLM
gi_3 NPQRSTUVWX
PQRSTUVWXY GHIJKLMNOP
ZABCDEFGHI QRSTUVWXYZ
JKLMNOPQRS ABCDEFGHIJ
gi_4 NOPQRSTUVW
CDEFGHIJKL XYZABCDEFG
MNOPQRSTUV HIJKLMNOPQ
WXYZABCDEF
gi_5
IJKLMNOPQR
STUVWXYZAB
CDEFGHIJKLM

Thanks

Last edited by cdfd123; 12-10-2007 at 02:41 AM.. Reason: writing error
# 4  
Old 12-10-2007
Firstly, pls do use vB Codes when either quoting others and/or posting code - it does make it easier to read the thread and you actually will imrove the chances of somebody actually willing to help you out.

Secondly, change:
Code:
RS=""

TO
Code:
RS=FS=""

Good luck!

Last edited by vgersh99; 12-10-2007 at 03:16 AM..
# 5  
Old 12-10-2007
Quote:
Originally Posted by vgersh99
Firstly, pls do use vB Codes when either quoting others and/or posting code - it does make it easier to read the thread and you actually will imrove the chances of somebody actually willing to help you out.

Secondly, change:
Code:
RS=""

TO
Code:
RS=FS=""

Good luck!
thanks for the instructions actually may be u not have understood the quesion and wht output shud i will like to have may be i will again simplify the example...

input
gi_1 ABCDEFDHIJ KMNOPQRSTU VWXYZABCDE
gi_2 JKLMNOPQRS TUVWXYZABC DEFGHIJKLM
gi_3 PQRSTUVWXY ZABCDEFGHI JKLMNOPQRS
gi_4 CDEFGHIJKL MNOPQRSTUV WXYZABCDEF
gi_5 IJKLMNOPQR STUVWXYZAB CDEFGHIJKLM

FGHIJKLMNO PQRSTUVWXY ZABCDEFABC
NOPQRSTUVW XYZABCDEFG HIJKLMNOPQ
TUVWXYZABC DEFGHIJKLM NPQRSTUVWX
GHIJKLMNOP QRSTUVWXYZ ABCDEFGHIJ
NOPQRSTUVW XYZABCDEFG HIJKLMNOPQ



Output
gi_1 ABCDEFDHIJ KMNOPQRSTU VWXYZABCDE FGHIJKLMNO PQRSTUVWXY ZABCDEFABC
gi_2 JKLMNOPQRS TUVWXYZABC DEFGHIJKLM NOPQRSTUVW XYZABCDEFG HIJKLMNOPQ
gi_3 PQRSTUVWXY ZABCDEFGHI JKLMNOPQRS TUVWXYZABC DEFGHIJKLM NPQRSTUVWX
gi_4 CDEFGHIJKL MNOPQRSTUV WXYZABCDEF GHIJKLMNOP QRSTUVWXYZ ABCDEFGHIJ
gi_5 IJKLMNOPQR STUVWXYZAB CDEFGHIJKLM NOPQRSTUVW XYZABCDEFG HIJKLMNOPQ

thanks
# 6  
Old 12-10-2007
Given your 'simplified' sample input above, this is what I get with the script:
Code:
gi_1 ABCDEFDHIJ KMNOPQRSTU VWXYZABCDE FGHIJKLMNO PQRSTUVWXY ZABCDEFABC
gi_2 JKLMNOPQRS TUVWXYZABC DEFGHIJKLM NOPQRSTUVW XYZABCDEFG HIJKLMNOPQ
gi_3 PQRSTUVWXY ZABCDEFGHI JKLMNOPQRS TUVWXYZABC DEFGHIJKLM NPQRSTUVWX
gi_4 CDEFGHIJKL MNOPQRSTUV WXYZABCDEF GHIJKLMNOP QRSTUVWXYZ ABCDEFGHIJ
gi_5 IJKLMNOPQR STUVWXYZAB CDEFGHIJKLM NOPQRSTUVW XYZABCDEFG HIJKLMNOPQ

If you're not seeing the same results, try changing:
Code:
RS=""

TO
Code:
FS=RS
RS=""

Your awk might be different from mine.........

Last edited by vgersh99; 12-10-2007 at 03:34 AM..
# 7  
Old 12-10-2007
Tools

Quote:
Originally Posted by vgersh99
Given your 'simplified' sample input above, this is what I get with the script:
Code:
gi_1 ABCDEFDHIJ KMNOPQRSTU VWXYZABCDE FGHIJKLMNO PQRSTUVWXY ZABCDEFABC
gi_2 JKLMNOPQRS TUVWXYZABC DEFGHIJKLM NOPQRSTUVW XYZABCDEFG HIJKLMNOPQ
gi_3 PQRSTUVWXY ZABCDEFGHI JKLMNOPQRS TUVWXYZABC DEFGHIJKLM NPQRSTUVWX
gi_4 CDEFGHIJKL MNOPQRSTUV WXYZABCDEF GHIJKLMNOP QRSTUVWXYZ ABCDEFGHIJ
gi_5 IJKLMNOPQR STUVWXYZAB CDEFGHIJKLM NOPQRSTUVW XYZABCDEFG HIJKLMNOPQ

If you're not seeing the same results, try changing:
Code:
RS=""

TO
Code:
FS=RS
RS=""

Your awk might be different from mine.........

No .....the awk is same for both of us
only this code
FS=RS
RS=""
work wonderfully
Thanks...

Previous Thread | Next Thread
Test Your Knowledge in Computers #721
Difficulty: Easy
In 2011, Alan Minsky was inducted into IEEE Intelligent Systems AI Hall of Fame for the "significant contributions to the field of AI and intelligent systems&".
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Join, merge, fill NULL the void columns of multiples files like sql "LEFT JOIN" by using awk

Hello, This post is already here but want to do this with another way Merge multiples files with multiples duplicates keys by filling "NULL" the void columns for anothers joinning files file1.csv: 1|abc 1|def 2|ghi 2|jkl 3|mno 3|pqr file2.csv: 1|123|jojo 1|NULL|bibi... (2 Replies)
Discussion started by: yjacknewton
2 Replies

2. Shell Programming and Scripting

awk or a combination of commands to read and calculate nth lines from pattern

Two numerical lines, found by either header line, need to be added and the total placed in a new-header section. Also the total should should be rounded or cut to a two decimal anynumber.XX format with the AB string added on the end. For example: The numerical lines from headers 2 and 3 are... (3 Replies)
Discussion started by: jessandr
3 Replies

3. UNIX for Beginners Questions & Answers

Sed/awk join lines once pattern found

Hi all OS - RHEL6.4 I have input file -f1.txt I need to search line which starts with \Start and read next line till it gets blank line and join them all. I need to trim any trailing spaces for each line.So output.txt should be.. \Start\now\fine stepwatch this space for toolsends... (7 Replies)
Discussion started by: krsnadasa
7 Replies

4. Shell Programming and Scripting

awk join lines based on keyword

Hello , I will need your help once again. I have the following file: cat file02.txt PATTERN XXX.YYY.ZZZ. 500 ROW01 aaa. 300 XS 14 ROW 45 29 AS XD.FD. PATTERN 500 ZZYN002 ROW gdf gsste ALT 267 fhhfe.ddgdg. PATTERN ERE.MAY. 280 PATTERRNTH 5000 rt.rt. ROW SO a 678 PATTERN... (2 Replies)
Discussion started by: alex2005
2 Replies

5. Shell Programming and Scripting

Join lines using sed or awk

Hi, I have text file that looks like this: blabla bla PATTERN LINE1 LINE2 bla bla bla PATTERN LINE1 LINE2 bla PATTERN LINE1 LINE2 bla (9 Replies)
Discussion started by: hench
9 Replies

6. UNIX for Dummies Questions & Answers

How to use shell commands within awk?

Hey guys i want to use shell commands like ls, find, cd and more with in awk statements with inputs from the awk variables. Like in the below code how can i change the directory using the value of path. Please suggest awk '{ while (i<NR) { i++; percentage = $5; path = $6; ... (2 Replies)
Discussion started by: rishi90
2 Replies

7. Shell Programming and Scripting

Using shell commands in awk

Hi, I've searched this site and the wider web and have not found anything (that I can understand..) that helps me. I've used shell commands in awk fine in the past, the difference is that I want to pass the shell command a field variable within awk from the current input. A simple example... (3 Replies)
Discussion started by: Ronnie717
3 Replies

8. Shell Programming and Scripting

How to get awk to edit in place and join all lines in text file

Hi, I lack the utter fundamentals on how to craft an awk script. I have hundreds of text files that were mangled by .doc format so all the lines are broken up so I need to join all of the lines of text into a single line. Normally I use vim command "ggVGJ" to join all lines but with so many... (3 Replies)
Discussion started by: n00ti
3 Replies

9. UNIX for Dummies Questions & Answers

Join 2 files with multiple columns: awk/grep/join?

Hello, My apologies if this has been posted elsewhere, I have had a look at several threads but I am still confused how to use these functions. I have two files, each with 5 columns: File A: (tab-delimited) PDB CHAIN Start End Fragment 1avq A 171 176 awyfan 1avq A 172 177 wyfany 1c7k A 2 7... (3 Replies)
Discussion started by: InfoSeeker
3 Replies

10. Shell Programming and Scripting

Awk Join multiple lines

Hi, I have data with broken lines: Sample data: "12"|"25"|"a"|"b"|"c"|"d"|"e"|"f"|"2453748"|"08:10:50" "16"|"25"|"a"|"b"|"c"|"d"|"e"|"f"|" 2453748"|"08:15:50" "16"|"25"|"a"|"b"|" c"|"d"|"e"|"f"|"2453748"|"08:19:50" "16"|"25"|"a"|"b"|"c"|"d"|"e"|"f"|"2453748"|"08:19:50" In the... (5 Replies)
Discussion started by: hitmansilentass
5 Replies

Featured Tech Videos