How to read different lines into column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read different lines into column
# 1  
Old 04-06-2010
How to read different lines into column

Dear experts,

Need your great help !

The input file is as follows:

Code:
Name:    test01
Name UID:     C7:9D:79:52:2A:3F:DF:11:A8:64:00:60:16:36:04:02
Shareable:             YES

HBA/SP Pairs:
  HBA UID                                          SP Name     SPPort
  -------                                          -------     ------ 
  20:00:00:1B:32:92:32:B6:21:00:00:1B:32:92:32:B6   SP B         0
Host name:             test01.com
  20:00:00:1B:32:92:32:B6:21:00:00:1B:32:92:32:B6   SP A         1
Host name:             test02.com
  20:00:00:1B:32:92:32:B6:21:00:00:1B:32:92:32:B6   SP B         2
Host name:             test01.com
  20:00:00:1B:32:92:32:B6:21:00:00:1B:32:92:32:B6   SP A         2
Host name:             test02.com
  20:00:00:1B:32:92:1D:60:21:00:00:1B:32:92:1D:60   SP A         0
Host name:             test01.com
  20:00:00:1B:32:92:1D:60:21:00:00:1B:32:92:1D:60   SP B         1
Host name:             test02.com
 
Primary/Secondary Pairs:
  Pri Number     Sec Number
  ----------     ----------
    8               7
    10              8
    9               9
    7               10

Shareable:             YES

Name:    test02
Name UID:     C4:9D:79:52:2A:3F:DF:11:A8:64:00:60:16:36:04:02
Shareable:             YES

HBA/SP Pairs:
  HBA UID                                          SP Name     SPPort
  -------                                          -------     ------ 
  20:00:00:1B:32:92:32:B6:21:00:00:1B:32:92:32:B6   SP B         0
Host name:             test01.com
  20:00:00:1B:32:92:32:B6:21:00:00:1B:32:92:32:B6   SP A         1
Host name:             test02.com
  20:00:00:1B:32:92:32:B6:21:00:00:1B:32:92:32:B6   SP B         2
Host name:             test01.com
  20:00:00:1B:32:92:32:B6:21:00:00:1B:32:92:32:B6   SP A         2
Host name:             test02.com
  20:00:00:1B:32:92:1D:60:21:00:00:1B:32:92:1D:60   SP A         0
Host name:             test01.com
  20:00:00:1B:32:92:1D:60:21:00:00:1B:32:92:1D:60   SP B         1
Host name:             test02.com
 
Primary/Secondary Pairs:
  Pri Number     Sec Number
  ----------     ----------
    0               6
    2               1
    1               0
    4               3
    3               2
    6               5
    5               4
Shareable:             YES

What I want to get is like:

Name Name UID Host Name Pri Number Sec Number

Thanks so much !

---------- Post updated at 12:00 PM ---------- Previous update was at 11:29 AM ----------

Can anybody help ?

Last edited by tojzz; 04-06-2010 at 01:52 PM.. Reason: Add extra information
# 2  
Old 04-06-2010
Bumping up posts or double posting is not permitted in these forums.

Please read the rules, which you agreed to when you registered, if you have not already done so.

You may receive an infraction for this. If so, don't worry, just try to follow the rules more carefully. The infraction will expire in the near future

Thank You.

The UNIX and Linux Forums.
# 3  
Old 04-06-2010
Quote:
Originally Posted by tojzz
...
The input file is as follows:

Code:
Name:    test01
Name UID:     C7:9D:79:52:2A:3F:DF:11:A8:64:00:60:16:36:04:02
Shareable:             YES
 
HBA/SP Pairs:
  HBA UID                                          SP Name     SPPort
  -------                                          -------     ------ 
  20:00:00:1B:32:92:32:B6:21:00:00:1B:32:92:32:B6   SP B         0
Host name:             test01.com
  20:00:00:1B:32:92:32:B6:21:00:00:1B:32:92:32:B6   SP A         1
Host name:             test02.com
  20:00:00:1B:32:92:32:B6:21:00:00:1B:32:92:32:B6   SP B         2
Host name:             test01.com
  20:00:00:1B:32:92:32:B6:21:00:00:1B:32:92:32:B6   SP A         2
Host name:             test02.com
  20:00:00:1B:32:92:1D:60:21:00:00:1B:32:92:1D:60   SP A         0
Host name:             test01.com
  20:00:00:1B:32:92:1D:60:21:00:00:1B:32:92:1D:60   SP B         1
Host name:             test02.com
 
Primary/Secondary Pairs:
  Pri Number     Sec Number
  ----------     ----------
    8               7
    10              8
    9               9
    7               10
 
Shareable:             YES
 
Name:    test02
Name UID:     C4:9D:79:52:2A:3F:DF:11:A8:64:00:60:16:36:04:02
Shareable:             YES
 
HBA/SP Pairs:
  HBA UID                                          SP Name     SPPort
  -------                                          -------     ------ 
  20:00:00:1B:32:92:32:B6:21:00:00:1B:32:92:32:B6   SP B         0
Host name:             test01.com
  20:00:00:1B:32:92:32:B6:21:00:00:1B:32:92:32:B6   SP A         1
Host name:             test02.com
  20:00:00:1B:32:92:32:B6:21:00:00:1B:32:92:32:B6   SP B         2
Host name:             test01.com
  20:00:00:1B:32:92:32:B6:21:00:00:1B:32:92:32:B6   SP A         2
Host name:             test02.com
  20:00:00:1B:32:92:1D:60:21:00:00:1B:32:92:1D:60   SP A         0
Host name:             test01.com
  20:00:00:1B:32:92:1D:60:21:00:00:1B:32:92:1D:60   SP B         1
Host name:             test02.com
 
Primary/Secondary Pairs:
  Pri Number     Sec Number
  ----------     ----------
    0               6
    2               1
    1               0
    4               3
    3               2
    6               5
    5               4
Shareable:             YES

What I want to get is like:

Name Name UID Host Name Pri Number Sec Number

...
What should the output look like, for the input file that you have posted ?
i.e. type in the expected output in your post.

tyler_durden
# 4  
Old 04-06-2010
Tyler,

I like to get the result like this: (Sorting by "Sec Number" column)
Code:
Name	Name UID		Host Name		Pri Number	Sec Number
test01	c7:...02		test01.com/test02.com	8		7
test01	c7:...02		test01.com/test02.com	10		8
test01	c7:...02		test01.com/test02.com	9		9
test01	c7:...02		test01.com/test02.com	7		10
test02	c4:...02		test01.com/test02.com	1		0
test02	c4:...02		test01.com/test02.com	2		1
test02	c4:...02		test01.com/test02.com	3		2
test02	c4:...02		test01.com/test02.com	4		3
test02	c4:...02		test01.com/test02.com	5		4
test02	c4:...02		test01.com/test02.com	6		5
test02	c4:...02		test01.com/test02.com	0		6


Last edited by vgersh99; 04-06-2010 at 03:55 PM.. Reason: code tags, please!
# 5  
Old 04-06-2010
Something like this -

Code:
$
$
$ cat file1.txt
Name:    test01
Name UID:     C7:9D:79:52:2A:3F:DF:11:A8:64:00:60:16:36:04:02
Shareable:             YES
HBA/SP Pairs:
  HBA UID                                          SP Name     SPPort
  -------                                          -------     ------
  20:00:00:1B:32:92:32:B6:21:00:00:1B:32:92:32:B6   SP B         0
Host name:             test01.com
  20:00:00:1B:32:92:32:B6:21:00:00:1B:32:92:32:B6   SP A         1
Host name:             test02.com
  20:00:00:1B:32:92:32:B6:21:00:00:1B:32:92:32:B6   SP B         2
Host name:             test01.com
  20:00:00:1B:32:92:32:B6:21:00:00:1B:32:92:32:B6   SP A         2
Host name:             test02.com
  20:00:00:1B:32:92:1D:60:21:00:00:1B:32:92:1D:60   SP A         0
Host name:             test01.com
  20:00:00:1B:32:92:1D:60:21:00:00:1B:32:92:1D:60   SP B         1
Host name:             test02.com
Primary/Secondary Pairs:
  Pri Number     Sec Number
  ----------     ----------
    8               7
    10              8
    9               9
    7               10
Shareable:             YES
Name:    test02
Name UID:     C4:9D:79:52:2A:3F:DF:11:A8:64:00:60:16:36:04:02
Shareable:             YES
HBA/SP Pairs:
  HBA UID                                          SP Name     SPPort
  -------                                          -------     ------
  20:00:00:1B:32:92:32:B6:21:00:00:1B:32:92:32:B6   SP B         0
Host name:             test01.com
  20:00:00:1B:32:92:32:B6:21:00:00:1B:32:92:32:B6   SP A         1
Host name:             test02.com
  20:00:00:1B:32:92:32:B6:21:00:00:1B:32:92:32:B6   SP B         2
Host name:             test01.com
  20:00:00:1B:32:92:32:B6:21:00:00:1B:32:92:32:B6   SP A         2
Host name:             test02.com
  20:00:00:1B:32:92:1D:60:21:00:00:1B:32:92:1D:60   SP A         0
Host name:             test01.com
  20:00:00:1B:32:92:1D:60:21:00:00:1B:32:92:1D:60   SP B         1
Host name:             test02.com
Primary/Secondary Pairs:
  Pri Number     Sec Number
  ----------     ----------
    0               6
    2               1
    1               0
    4               3
    3               2
    6               5
    5               4
Shareable:             YES
$
$
$
$ ##
$ perl -lne 'if (/^Name:\s*(\w+)$/ or eof) {
>              if ($name) {
>                 foreach $k (sort {$a <=> $b} keys %psnum) {
>                  printf("%-8s %-48s %-35s %10d %10d\n", $name, $nameuid, $hostnames, $psnum{$k}, $k);
>                }
>                $name = "";
>                $nameuid = "";
>                $hostnames = "";
>                %psnum = ();
>              } else {
>                printf("%-8s %-48s %-35s %-10s %-10s\n","Name", "Name UID", "Host Name", "Pri Number", "Sec Number");
>                printf("%8s %48s %35s %10s %10s\n","-"x8, "-"x48, "-"x35, "-"x10, "-"x10);
>              }
>              $name = $1;
>            } elsif (/^Name UID:\s*(.*?)$/) {
>              $nameuid = $1;
>            } elsif (/^Host name:\s*(.*?)$/) {
>              if ($hostnames !~ m/$1/) {
>                $hostnames = $hostnames eq "" ? $1 : $hostnames."/$1";
>              }
>            } elsif (/^\s*Pri Number\s*Sec Number$/) {
>              $prisec = 1;
>            } elsif ($prisec && /^\s*(\d+)\s*(\d+)\s*$/) {
>              $psnum{$2} = $1;
>            } elsif ($prisec && /^\s*$/) {
>              $prisec = 0;
>            }
>           ' file1.txt
Name     Name UID                                         Host Name                           Pri Number Sec Number
-------- ------------------------------------------------ ----------------------------------- ---------- ----------
test01   C7:9D:79:52:2A:3F:DF:11:A8:64:00:60:16:36:04:02  test01.com/test02.com                        8          7
test01   C7:9D:79:52:2A:3F:DF:11:A8:64:00:60:16:36:04:02  test01.com/test02.com                       10          8
test01   C7:9D:79:52:2A:3F:DF:11:A8:64:00:60:16:36:04:02  test01.com/test02.com                        9          9
test01   C7:9D:79:52:2A:3F:DF:11:A8:64:00:60:16:36:04:02  test01.com/test02.com                        7         10
test02   C4:9D:79:52:2A:3F:DF:11:A8:64:00:60:16:36:04:02  test01.com/test02.com                        1          0
test02   C4:9D:79:52:2A:3F:DF:11:A8:64:00:60:16:36:04:02  test01.com/test02.com                        2          1
test02   C4:9D:79:52:2A:3F:DF:11:A8:64:00:60:16:36:04:02  test01.com/test02.com                        3          2
test02   C4:9D:79:52:2A:3F:DF:11:A8:64:00:60:16:36:04:02  test01.com/test02.com                        4          3
test02   C4:9D:79:52:2A:3F:DF:11:A8:64:00:60:16:36:04:02  test01.com/test02.com                        5          4
test02   C4:9D:79:52:2A:3F:DF:11:A8:64:00:60:16:36:04:02  test01.com/test02.com                        6          5
test02   C4:9D:79:52:2A:3F:DF:11:A8:64:00:60:16:36:04:02  test01.com/test02.com                        0          6
$
$

tyler_durden
# 6  
Old 04-08-2010
If using awk/sed in windows2003, how to get the same output

Tyler,

Thanks so much for your great help ! Smilie

I'm not familar with perl script. If using awk/sed etc in windows2003, how to get the same output ?

What's more, if possible, based on the awk/sed script, could you briefly describe it ?

---------- Post updated 04-07-10 at 08:52 AM ---------- Previous update was 04-06-10 at 07:24 PM ----------

Experts,

Urgent help !! need awk/sed script to get the same output.

thanks in advance !

---------- Post updated at 08:54 AM ---------- Previous update was at 08:52 AM ----------

Experts,

Urgent help !! need awk/sed script to get the same output.

thanks in advance !

---------- Post updated at 09:44 AM ---------- Previous update was at 08:54 AM ----------

Quote:
Originally Posted by tojzz
Tyler,

Thanks so much for your great help ! Smilie

I'm not familar with perl script. If using awk/sed etc in windows2003, how to get the same output ?

What's more, if possible, based on the awk/sed script, could you briefly describe it ?

---------- Post updated 04-07-10 at 08:52 AM ---------- Previous update was 04-06-10 at 07:24 PM ----------

Experts,

Urgent help !! need awk/sed script to get the same output.

thanks in advance !

---------- Post updated at 08:54 AM ---------- Previous update was at 08:52 AM ----------

Experts,

Urgent help !! need awk/sed script to get the same output.

thanks in advance !


---------- Post updated at 09:57 AM ---------- Previous update was at 09:44 AM ----------

Need awk/sed scrpt to get the same output, thanks !

---------- Post updated at 10:24 AM ---------- Previous update was at 09:57 AM ----------

Can anybody help me ?

---------- Post updated 04-08-10 at 05:23 AM ---------- Previous update was 04-07-10 at 10:24 AM ----------

For the following perl script, can anyone help to convert it to awk statement ?

Code:
foreach $k (sort {$a <=> $b} keys %psnum)

Last edited by tojzz; 04-08-2010 at 07:24 AM.. Reason: add code
# 7  
Old 04-10-2010
MySQL

if you want sed script (but this method difficult and tiring)

Code:
sed -e '/^  20/d' -e '/^Primary/d' -e '/^ *HBA/d' -e '/^Shareable/d' -e '/^  -/d' -e '/^$/d'  file > firs 
sed -e 's/\(Name:\)  *\(.*\)/\1\n--------\n\2\n/g' -e 's/\(Name UID:\)  *\(.*\)/\1\n------------------------------------------------\n\2\n/g' firs > firs2 
sed -e 's/\(Host name:\)  *\(.*\)/\1\n-------------\n\2\n/g' firs2 > firs3 sed -n '/[^[0-9][0-9]*[0-9][0-9]* ]*/p' firs3 > prinumber 
sed -n '/[test][0-9]*$/p' firs3 > namefirst 
sed -e '1,1s/\(.*\)/\1\n\1\n\1\n\1/g' -e '2,2s/\(.*\)/\1\n\1\n\1\n\1\n\1\n\1\n\1\n\1/g' namefirst > namelist 
sed -n '/[A-Z][A-Z]*[:digit:][:digit:]*/p' firs3 | sed '/^Name/d' | sed -e '1,1s/\(.*\)/\1\n\1\n\1\n\1/g' -e '2,2s/\(.*\)/\1\n\1\n\1\n\1\n\1\n\1\n
\1\n\1/g' > nameuid 
sed -e '1i Name' -e '1i ------' namelist > namelist1
sed -e '1i Name UID' -e '1i -----------------------------------------------' nameuid > nameuid1
sed -e '1i Host Name' -e '1i ----------' hostname > hostname1
sed -e '1i Pri Number\tSec Number' -e '1i ----------\t----------' prinumber > prinumber1 
paste -d " " namelist1 nameuid1 hostname1 prinumber1 | sed '1,1s/\(Name\) \(Name UID\) \(Host Name\) \(Pri Number\)\t\(Sec Number\).*/\1   \2\t\t\t\t\t       \3  \4\t\5/'

Code:
Name   Name UID                                        Host Name  Pri Number    Sec Number
------ ----------------------------------------------- ---------- ----------    ----------
test01 C7:9D:79:52:2A:3F:DF:11:A8:64:00:60:16:36:04:02 test01.com     8               7
test01 C7:9D:79:52:2A:3F:DF:11:A8:64:00:60:16:36:04:02 test02.com     10              8
test01 C7:9D:79:52:2A:3F:DF:11:A8:64:00:60:16:36:04:02 test01.com     9               9
test01 C7:9D:79:52:2A:3F:DF:11:A8:64:00:60:16:36:04:02 test02.com     7               10
test02 C4:9D:79:52:2A:3F:DF:11:A8:64:00:60:16:36:04:02 test01.com     0               6
test02 C4:9D:79:52:2A:3F:DF:11:A8:64:00:60:16:36:04:02 test02.com     2               1
test02 C4:9D:79:52:2A:3F:DF:11:A8:64:00:60:16:36:04:02 test01.com     1               0
test02 C4:9D:79:52:2A:3F:DF:11:A8:64:00:60:16:36:04:02 test02.com     4               3
test02 C4:9D:79:52:2A:3F:DF:11:A8:64:00:60:16:36:04:02 test01.com     3               2
test02 C4:9D:79:52:2A:3F:DF:11:A8:64:00:60:16:36:04:02 test02.com     6               5
test02 C4:9D:79:52:2A:3F:DF:11:A8:64:00:60:16:36:04:02 test01.com     5               4
test02 C4:9D:79:52:2A:3F:DF:11:A8:64:00:60:16:36:04:02 test02.com

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read first column and count lines in second column using awk

Hello all, I would like to ask your help here: I've a huge file that has 2 columns. A part of it is: sorted.txt: kss23 rml.67lkj kss23 zhh.6gf kss23 nhd.09.fdd kss23 hp.767.88.89 fl67 nmdsfs.56.df.67 fl67 kk.fgf.98.56.n fl67 bgdgdfg.hjj.879.d fl66 kl..hfh.76.ghg fl66... (5 Replies)
Discussion started by: Padavan
5 Replies

2. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

3. Shell Programming and Scripting

Find lines with matching column 1 value, retain only the one with highest value in column 2

I have a file like: I would like to find lines lines with duplicate values in column 1, and retain only one based on two conditions: 1) keep line with highest value in column 3, 2) if column 3 values are equal, retain the line with the highest value in column 4. Desired output: I was able to... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

4. UNIX for Dummies Questions & Answers

Converting column to rows for every 3 lines in the column

Hi gurus! Please help me with this one. I have an file with the following contents: a b c d e f g h i j I would like to make to transform it to look like this as my output file: a,b,c d,e,f (4 Replies)
Discussion started by: kokoro
4 Replies

5. Shell Programming and Scripting

Filtering lines for column elements based on corresponding counts in another column

Hi, I have a file like this ACC 2 2 21 aaa AC 443 3 22 aaa GCT 76 1 33 xxx TCG 34 2 33 aaa ACGT 33 1 22 ggg TTC 99 3 44 wee CCA 33 2 33 ggg AAC 1 3 55 ddd TTG 10 1 22 ddd TTGC 98 3 22 ddd GCT 23 1 21 sds GTC 23 4 32 sds ACGT 32 2 33 vvv CGT 11 2 33 eee CCC 87 2 44... (1 Reply)
Discussion started by: polsum
1 Replies

6. UNIX for Dummies Questions & Answers

When reading a csv file, counter to read 20 lines and wait for minute then read next 20 till end

Hello All, i am a newbie and need some help when reading a csv file in a bourne shell script. I want to read 10 lines, then wait for a minute and then do a reading of another 10 lines and so on in the same way. I want to do this till the end of file. Any inputs are appreciated ... (3 Replies)
Discussion started by: victor.s
3 Replies

7. Shell Programming and Scripting

Select lines in which column have value greater than some percent of total file lines

i have a file in following format 1 32 3 4 6 4 4 45 1 45 4 61 54 66 4 5 65 51 56 65 1 12 32 85 now here the total number of lines are 8(they vary each time) Now i want to select only those lines in which the values... (6 Replies)
Discussion started by: vaibhavkorde
6 Replies

8. Shell Programming and Scripting

Read CSV column value based on column name

Hi All, I am newbie to Unix I ve got assignment to work in unix can you please help me in this regard There is a sample CSV file "Username", "Password" "John1", "Scot1" "John2", "Scot2" "John3", "Scot3" "John4", "Scot4" If i give the column name as Password and row number as 4 the... (3 Replies)
Discussion started by: JohnGG
3 Replies

9. Shell Programming and Scripting

paste each 10 lines of single column to several column

Hi, I need to paste each 10 lines of single column to several columns. Please, can anyone tell me how to write in awk? Input File: 22 34 36 12 17 19 15 11 89 99 56 38 29 (4 Replies)
Discussion started by: nica
4 Replies

10. Shell Programming and Scripting

how to read the column and print the values under that column

hi all:b:, how to read the column and print the values under that column ...?? file1 have something like this cat file1 ======= column1, column2,date,column3,column4..... 1, 23 , 12/02/2008,...... 2, 45, 14/05/2008,..... 3, 56, 16/03/2008,..... cat file2 =======... (6 Replies)
Discussion started by: gemini106
6 Replies
Login or Register to Ask a Question