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
awk and file parsing devtakh Shell Programming and Scripting 4 05-06-2008 11:13 AM
Parsing xml file using Sed kapilkinha UNIX for Advanced & Expert Users 3 04-08-2008 09:43 AM
Parsing a file aol12123 Shell Programming and Scripting 11 03-25-2008 02:06 AM
Parsing a csv file chiru_h Shell Programming and Scripting 6 02-12-2008 09:33 AM
parsing file through awk bbeugie Shell Programming and Scripting 13 08-22-2006 01:21 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-24-2008
aristegui aristegui is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 24
Parsing a file (sed/awk?)

Hello people, newbie question. I'm trying to parse these type of file

1 "CAR " " C1 " " " 6 0 C1
2 "CAR " " O1A" " " 8 0 O1A
3 "CAR " " O1B" " " 8 -1 O1B
4 "CAR " " C2 " " " 6 0 C2
5 "CAR " " C3 " " " 6 0 C3
6 "CAR " "H5A1" " " 8 0 H5A1

to the new one

1 "CAR " " C1 " " " 6 0 0 1 "C1" 0
2 "CAR " " O1A" " " 8 0 0 1 "O1A" 0
3 "CAR " " O1B" " " 8 -1 0 1 "O1B" 0
4 "CAR " " C2 " " " 6 0 0 1 "C2" 0
5 "CAR " " C3 " " 1f" 6 0 0 1 "C3" 0
6 "CAR " "H5A1" " " 6 0 0 1 "H5A1" 0

Every field are constant, but I have to insert the 1f value in a certain line (here the number 5) and i know previosly what is it's (i think i will use a counter to do it).

Are sed/awk the best approach to do it? How is the best way to start, extract the values with awk and then modify it with sed? is a mess that some fields could have white spaces ( " C1 ", " O1B", "H5A1").

I'm starting with this, could anybody help me with the code?
Thank you in advanced. Best regards.
  #2 (permalink)  
Old 06-24-2008
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Code:
nawk '{$NF = "0 1" "\"" $NF "\" 0"; print}' myFile
  #3 (permalink)  
Old 06-24-2008
aristegui aristegui is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 24
Great!!! thank you very much. And, could you help me about how is the way to insert the 1f value in a certain line, for example in the line number five? (whoa, this is really powerful).

Thank youn in advanced.
  #4 (permalink)  
Old 06-24-2008
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Code:
nawk '{$NF = "0 1" "\"" $NF "\" 0"; if (FNR==5) $8= "1f" $8; print}' myFile
  #5 (permalink)  
Old 06-24-2008
aristegui aristegui is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 24
I have a problem with white spaces, they are important for the specified file format, and using the awk command

Code:
awk '{$NF = "0 1" "\"" $NF "\" 0"; print}' myFile
i retrieve the correct output, but with the fields separated only for one white space.

Please, what could be the way to parse this lines...?

HTML Code:
    1        2   -5.288000   -3.217000    4.035000     1 " " X " "    2   0.17850   0.00000 "car " " C1 " "    "    6   0 C1
    2       15   -5.385000   -3.069000    5.276000     1 " " X " "   70  -0.64780   0.00000 "car " " O1a" "    "    8   0 O1a
    3       18   -6.014000   -4.000000    3.392000     1 " " X " "   70  -0.64780   0.00000 "car " " O1b" "    "    8  -1 O1b
    4        3   -4.190000   -2.467000    3.295000     1 " " X " "    2   0.06130   0.00000 "car " " C2 " "    "    6   0 C2
    5        3   -3.620000   -3.260000    2.103000     1 " " X " "    2   0.09070   0.00000 "car " " C3 " "    "    6   0 C3
    6       16   -2.802000   -2.377000    1.324000     1 " " X " "   70  -0.38950   0.00000 "car " " O3 " "    "    8   0 O3
...to this new ones, respecting the number of white spaces?

HTML Code:
    1        2   -5.288000   -3.217000    4.035000     1 " " X " "    2   0.17850   0.00000 "CAR " " C1 " "    "    6   0    0    1 "C1" 0
    2       15   -5.385000   -3.069000    5.276000     1 " " X " "   70  -0.64780   0.00000 "CAR " " O1A" "    "    8   0    0    1 "O1A" 0
    3       18   -6.014000   -4.000000    3.392000     1 " " X " "   70  -0.64780   0.00000 "CAR " " O1B" "    "    8  -1    0    1 "O1B" 0
    4        3   -4.190000   -2.467000    3.295000     1 " " X " "    2   0.06130   0.00000 "CAR " " C2 " "    "    6   0    0    1 "C2" 0
    5        3   -3.620000   -3.260000    2.103000     1 " " X " "    2   0.09070   0.00000 "CAR " " C3 " "  1f"    6   0    0    1 "C3" 0
    6        3   -2.750000   -4.417000    2.639000     1 " " X " "    2   0.14300   0.00000 "CAR " " C4 " "    "    6   0    0    1 "C4" 0
I use html tag to reflect the correct white spaces. Thank you in advanced.
  #6 (permalink)  
Old 06-24-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 750
Quote:
Originally Posted by aristegui View Post
I have a problem with white spaces, they are important for the specified file format, and using the awk command

Code:
awk '{$NF = "0 1" "\"" $NF "\" 0"; print}' myFile
i retrieve the correct output, but with the fields separated only for one white space.

Please, what could be the way to parse this lines...?
Code:
awk -F'[ ]' '{$NF = "0 1" "\"" $NF "\"\t0"; print}' file
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 05:26 PM.


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