write lines of one file to new file dependent on first field of line

write lines of one file to new file dependent on first field of line

Post by Andy Wilso » Thu, 10 Jul 2008 06:02:46


sample data

qaaeae01: Name: ethernet
qaaeae01: Node: ethernet@4,1
qaaeae01: Device Type: network
qaaeae01: Physical Location: U787B.001.DNW9929-P1-C1-T2
qaaeae01:
qaaeae01: Name: ethernet
qaaeae01: Node: ethernet@6
qaaeae01: Device Type: network
qaaeae01: Physical Location: U787B.001.DNW9929-P1-C1-T3
qaaeae01:
qaaeae01: Name: ethernet
qaaeae01: Node: ethernet@6,1
qaaeae01: Device Type: network
qaaeae01: Physical Location: U787B.001.DNW9929-P1-C1-T4
qaaiap02: INSTALLED RESOURCE LIST WITH VPD
qaaiap02:
qaaiap02: The following resources are installed on your machine.
qaaiap02:
qaaiap02: Model Architecture: chrp
qaaiap02: Model Implementation: Multiple Processor, PCI bus
qaaiap02:
qaaiap02: sys0 System Object
qaaiap02: sysplanar0 System Planar
qaaiap02: vio0 Virtual I/O Bus
qaaiap02: vsa0 U9131.52A.105661G-V1-C0 LPAR Virtual Serial Adapter
qaaiap02:
qaaiap02: Device Specific.(YL)........U9131.52A.105661G-V1-C0
qaaiap02:
qaaiap02: vty0 U9131.52A.105661G-V1-C0-L0 Asynchronous Terminal


would like to write files containing all lines beginning with the first field and would be nice if files was named by first field
My skills with sed and awk are quite limited . I have spent several hours on this and would prefer not to do the work in a editor .
I do want to learn this as i know things like this will come up again. I am running a current Linux install with bash shell.
 
 
 

write lines of one file to new file dependent on first field of line

Post by pk » Thu, 10 Jul 2008 18:12:52


If you don't want the filename to appear in the output, then

awk -F: '{a=$1;sub(/^[^:]*:/,"");print > a}' file

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.

 
 
 

write lines of one file to new file dependent on first field of line

Post by Andy Wilso » Thu, 10 Jul 2008 22:55:54


Thank you this did exactly what I wanted to do. now that i have examples
I will take time to tear them apart so i can understand what they are doing .