The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
ksh - comma seperate input Mike_the_Man Shell Programming and Scripting 4 11-05-2008 02:44 AM
how to seperate space in a string kittusri9 Shell Programming and Scripting 5 05-08-2008 06:43 AM
Seperate commands on the same line bobk544 Shell Programming and Scripting 3 07-28-2007 05:14 PM
Need help to seperate data getdpg Shell Programming and Scripting 4 08-30-2006 05:28 AM
/usr/lib on a seperate partition progressdll UNIX for Advanced & Expert Users 4 12-21-2004 09:59 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-28-2003
inquirer's Avatar
inquirer inquirer is offline
Registered User
  
 

Join Date: Aug 2001
Posts: 79
row seperate

i have this sample data:

test01 --- abc-01 name1
abc-02 name2
abc-03 name3

test02 --- abc-20 name4
abc-21 name5

test03 --- abc-22 name6
abc-23 name7


i want to generate a file that looks like:

test01 abc-01 name1
test01 abc-02 name2
test01 abc-03 name3
test02 abc-20 name4
test02 abc-21 name5
test03 abc-22 name6
test03 abc-23 name7

i know awk can use RS (record seperator) but i dont know much of it. pls help

thanks!
  #2 (permalink)  
Old 07-28-2003
criglerj's Avatar
criglerj criglerj is offline
Registered User
  
 

Join Date: May 2002
Location: Atlanta
Posts: 129
Here's your sample data in a code block to remove ambiguity about spacing after fishing around in "view page source" in my browser. I also realigned it to what I guess your data really looks like. The fact of spacing matters more to awk than the amount of spacing anyhow, but everyone who posts here should be aware that alignment gets ruined by the VBcode->HTML conversion unless it's in a code block.
Code:
test01 ---   abc-01     name1
             abc-02     name2
             abc-03     name3

test02 ---   abc-20     name4
             abc-21     name5

test03 ---   abc-22     name6
             abc-23     name7
Fiddling with RS is not helpful here.
This should do what you want, though it is untested code, and you shouldn't run it until you understand it.
Code:
#!/usr/bin/awk
NF == 0 { next }                      # Skip empty lines
NF == 4 { tag = $1 }                  # Pick up the tag
        { print tag, $(NF - 1), $NF } # Print lines with data
The moderators will probably want to move this to the shell scripting forum.
  #3 (permalink)  
Old 07-28-2003
oombera's Avatar
oombera oombera is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2002
Location: Cleveland, OH
Posts: 804
You know more than I do about awk - I would've come up with some convoluted mess to do that.

I can tell what your code is doing, but I'm wondering, won't the two lines
Code:
NF == 4 { tag = $1 }                  # Pick up the tag
        { print tag, $(NF - 1), $NF } # Print lines with data
only pick up on, say,
Code:
test01 ---   abc-01     name1
but not
Code:
             abc-02     name2
             abc-03     name3
?
  #4 (permalink)  
Old 07-29-2003
inquirer's Avatar
inquirer inquirer is offline
Registered User
  
 

Join Date: Aug 2001
Posts: 79
Unhappy sorry about the wrong forum

hey, you are right about the spacing and the forum. i think this topic should be moved with the shell scripting forum. will the moderators do this for me :P

thanks alot!
  #5 (permalink)  
Old 07-29-2003
criglerj's Avatar
criglerj criglerj is offline
Registered User
  
 

Join Date: May 2002
Location: Atlanta
Posts: 129
Quote:
Originally posted by oombera
[W]on't the two lines
Code:
NF == 4 { tag = $1 }                  # Pick up the tag
        { print tag, $(NF - 1), $NF } # Print lines with data
only pick up on, say,
Code:
test01 ---   abc-01     name1
but not
Code:
             abc-02     name2
             abc-03     name3
?
Nope. Your confusion may come from a mistaken notion that the pattern/action pairs look like exclusive cases, but you should look at them like a C switch statement: You fall through to the next "case" (awk "pattern") unless you encounter a "break" (awk "next"). So NF == 0 { next } skips empty lines. Then NF == 4 { tag = $1 } picks up the first field and goes to the next pattern when there are four fields. The next pattern is empty, so it matches every line that wasn't "next"-ed so far, and its action is to print the saved tag and the last two fields from the current input line. If there is exactly one field on a line, awk will break in this last case, but that wasn't part of the OP's stated problem.
  #6 (permalink)  
Old 07-29-2003
inquirer's Avatar
inquirer inquirer is offline
Registered User
  
 

Join Date: Aug 2001
Posts: 79
how to code block

how do i code block? i would like to make some corrections in my post :P
  #7 (permalink)  
Old 07-30-2003
oombera's Avatar
oombera oombera is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2002
Location: Cleveland, OH
Posts: 804
When you're making a post to the forums, you should see some buttons above the textbox you type your message in. Look for a button with the # symbol on it to insert code. Read the help page for more on VB Code.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:14 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0