Need help to manipulate data using script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help to manipulate data using script
# 1  
Old 11-28-2013
Need help to manipulate data using script

Hi

i want to manipulate my data to convert row to column

Code:
name
600

Slno    vlan
1        600
2        609
3        700

name
700

Slno    vlan
1        600
2        609
3        700


I want to convert above file like this output

Code:
Slno    vlan    name  
1          600     600
2          609     600
3          700     600
Slno    vlan    name  
1          600     700
2          609     700
3          700     700

Please help me to convert

Thanks in advance
Smilie
Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules. Without the CODE tags, spacing in your input and output files is hidden by HTML processing.

Last edited by Don Cragun; 11-28-2013 at 05:07 PM.. Reason: Add CODE tags.
# 2  
Old 11-28-2013
An awk approach:
Code:
awk '
        /name/ {
                print "Slno", "Name"
                f = 0
                getline
                nm = $0
        }
        /Slno vlan/ {
                f = 1
                next
        }
        f && NF {
                print $0, nm
        }

' OFS='\t' file

# 3  
Old 11-28-2013
Please use code tags as required by forum rules!

Try
Code:
awk '/name/ {getline name; next} NF {print $0, $1="Slno"?"name":name}' file
Slno vlan name
1 600 name
2 609 name
3 700 name
Slno vlan name
1 600 name
2 609 name
3 700 name

This User Gave Thanks to RudiC For This Post:
# 4  
Old 11-28-2013
Slight change to RudiC's solution to get last "name" field printing:

Code:
awk '/name/ {getline name; next} NF {print $0, $1=="Slno"?"name":name}' file
Slno vlan name
1 600 600
2 609 600
3 700 600
Slno vlan name
1 600 700
2 609 700
3 700 700

I tried a slightly different approach - using the blank lines as RS:

Code:
awk '
  /name/ { name=$2; next }
  /Slno vlan/ {
      $1=$1" name";
      for(i=2;i<=NF;i++) $i=$i" "name
  }
  1 ' FS='\n' OFS='\n' RS= infile

This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 11-28-2013
Thanks for you replay.! its very use full

I need a slight change in the output as below

input file is like this

Code:
 
name1
600  

Slno    vlan 
1        600
2        609
3        700 

name2 
700  

Slno    vlan 
1        600 
2        609 
3        700

and out put file

Code:
Slno vlan name 
1 600 name1 
2 609 name1 
3 700 name1 

Slno vlan name 
1 600 name2 
2 609 name2 
3 700 name2

Thank you
# 6  
Old 11-28-2013
Try these changes to my earlier posted code:

Code:
awk '
  !/Slno/ {
     name=$1
     next
   }
  /Slno vlan/ {
      $1=$1" name";
      for(i=2;i<=NF;i++) $i=$i" "name
  }
  1
' FS='\n' ORS='\n\n' OFS='\n' RS= infile

# 7  
Old 11-28-2013
Quote:
Originally Posted by Chubler_XL
Try these changes to my earlier posted code:

Code:
awk '
  !/Slno/ {
     name=$1
     next
   }
  /Slno vlan/ {
      $1=$1" name";
      for(i=2;i<=NF;i++) $i=$i" "name
  }
  1
' FS='\n' ORS='\n\n' OFS='\n' RS= infile

I am getting below output

Code:
awk '
  !/Slno/ {
     name=$1
     next
   }
  /Slno vlan/ {
      $1=$1" name";
      for(i=2;i<=NF;i++) $i=$i" "name
  }
  1
' FS='\n' ORS='\n\n' OFS='\n' RS= test


OUTPUT


Code:
Slno    vlan  
1    600
2    609
3    700

Slno    vlan  
1    600
2    609
3    700

Slno    vlan  
1    600
2    609
3    700

Thank you..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help - manipulate data by columns and repeated

Hello good afternoon to everyone. I'm new to the forum and would like to request your help in handling data. I hope my English is clear. I have a file (Dato01.txt) to contine the following structure. # Col1 - Col2 - Col3 - Col4 Patricia started Jun 22 05:22:58 Carolina started Jun... (5 Replies)
Discussion started by: kelevra
5 Replies

2. Shell Programming and Scripting

Extract & Manipulate continous data stream-- tcpdump

Hello; I have this rather tricky problem to solve --(to me, anyways) .. I am processing the following one liner with tcpdump.. tcpdump -i T3501 -A ether host 00:1e:49:29:fc:c9 or ether host 00:1b:2b:86:ec:1b or ether host 00:21:1c:98:a4:08 and net 149.83.6.0/24 | grep --line-buffered -B... (5 Replies)
Discussion started by: delphys
5 Replies

3. Shell Programming and Scripting

Script to manipulate contents of clipboard

Hi there, I'm a total newbie and would like some help. I'd like to have a script to - load the contents of the clipboard into a variable - manipulate the variable by adding the characters /* to the beginning of every line and */ to the end of every line - load the variable back into the... (3 Replies)
Discussion started by: js8765
3 Replies

4. Shell Programming and Scripting

Shell script to manipulate a file

Hello, I have a file with following contents : WSL SRVGRP=LISTENER SRVID=2 CLOPT="-A -t -- -n 0x0002aa050a03cc65 " RQPERM=0660 REPLYQ=Y RPPERM=0660 MIN=1 MAX=1 CONV=N I need to print only the value in Hex i.e.... (2 Replies)
Discussion started by: deo_kaustubh
2 Replies

5. Shell Programming and Scripting

Manipulate data in detail problem facing

Input Participant number: HAC Position type Location Distance_start Distance_end Range Mark 1 1 + Front 808 1083 276 2 1 + Front 1373 1636 264 3 1 - Back 1837 2047 211 Participant number: BCD Position type... (6 Replies)
Discussion started by: patrick87
6 Replies

6. Shell Programming and Scripting

how to manipulate with lines while playing with data

hello everyone, well I have a file which contains data, I want to add the data on hourly basis, like my file contains data for 24 hours, (so a total of 1440 ) lines. Now i want to add the data on hourly basis to get average values. like if I use (head) command it is ok for first go, but... (5 Replies)
Discussion started by: jojo123
5 Replies

7. Shell Programming and Scripting

Script to manipulate logfile text

Hi guys, I was wandering if a Shell guru could give me some advice on tackling a problem. I have used a mixture of grep, cut and awk to get data from a log file in the following format: 14/11/08 10:39: Checking currenly : Enabled 14/11/08 10:39: Records allocated : 221... (11 Replies)
Discussion started by: rosspaddock
11 Replies

8. UNIX for Dummies Questions & Answers

Excel data manipulate

All, I have the following format of data in a spreadsheet A 1 2 3 4 B 1 2 3 4 where 'A' is value of 'A1', '1 2 3 4' is value of cell B1, 'B' is value of cell A2, and '1 2 3 4' is value of cell B2. There... (12 Replies)
Discussion started by: rahulrathod
12 Replies

9. Shell Programming and Scripting

manipulate data with specific format

Hi everybody: I have a problem with how I have to manipulate the data which have specific format like this: 249. 0.30727021E+05 0.30601627E+05 0.37470780E-01 -0.44745335E+02 0.82674536E+03 248. 0.30428182E+05 0.30302787E+05 0.40564921E-01 -0.45210293E+02 ... (5 Replies)
Discussion started by: tonet
5 Replies

10. Shell Programming and Scripting

sed or other tool to manipulate data, including email addresses

I have a list of names and email addresses, like this. The <tab> markers are actually tabs. joe.blow <tab> joe.blow@wherever.com tom.t.hall <tab> tom.t.hall@wherever.com john.r.smith <tab> john.r.smith@wherever.com sally.jones <tab> sally.jones@state.or.us I want to parse the data so that... (3 Replies)
Discussion started by: manouche
3 Replies
Login or Register to Ask a Question