LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-29-2004, 12:51 AM   #1
Helene
LQ Newbie
 
Registered: Apr 2004
Posts: 19

Rep: Reputation: 0
My field separator changes when using awk


Hi,
I have this file:
Helene;/home/home.html;24/02/04 14:16:09;oldcomment
Helene;/home/home.gif;24/02/04 14:16:09;oldcomment

With this code, I want to change the comment (last field of the record), to a new comment specified by the user.

variable="Helene"
awk -F';' ' { if( $1 ~/'${variable}'/ ){
print "Please enter a comment for this record: " >"/dev/tty"
print $0 > "/dev/tty"
getline newcomment < "/dev/tty"
$5=newcomment
print $0 }
} ' myFile > tmpFile

This is the result:
Helene /home/home.html 24/02/04 14:16:09 newcomment1
Helene /home/home.gif 24/02/04 14:16:09 newcomment1

Where is my field separator going??
 
Old 04-29-2004, 09:44 AM   #2
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
You're declaring the semi-colon as a field separator and only telling it to print fields. So the semi-colon isn't being printed because it's not part of a field. My awk is terrible so I don't know the right way - I think there's an elegant way around this but a stubborn 'I want a semi-colon!' method would be

Code:
variable="Helene"
awk -F';' ' { if( $1 ~/'${variable}'/ ){
        print "Please enter a comment for this record: " >"/dev/tty"
        print $0 > "/dev/tty"
        getline newcomment < "/dev/tty"
        $5=newcomment
        print $1";"$2";"$3";"$5 }
} ' myFile > tmpFile
Something along those lines.
 
Old 04-29-2004, 08:17 PM   #3
Helene
LQ Newbie
 
Registered: Apr 2004
Posts: 19

Original Poster
Rep: Reputation: 0
Thanks Digiot. That's the same solution as I came up with my self. It doesn't look very good, but it works!! Thanks anyway..
 
Old 05-01-2004, 08:10 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
To make it "look better" you should also set an output separator ("OFS"). AFAIK this cannot be done from a command line option, but needs to be set from inside the awk-script. So below the input separator ("FS") is also set inside the script instead of with the -F';' option.
Code:
variable="Helene"
awk 'BEGIN {FS=";" ; OFS=";"} { if( $1 ~/'${variable}'/ ){
print "Please enter a comment for this record: " >"/dev/tty"
print $0 > "/dev/tty"
getline newcomment < "/dev/tty"
$5=newcomment
print $0 }
} ' myFile > tmpFile
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
quick hand with awk multiple field separators pld Programming 10 05-28-2010 07:51 AM
perl input field separator Tinkster Programming 5 10-18-2004 04:08 PM
AWK: print field to end, and character count? ridertech Linux - Newbie 1 05-07-2004 05:07 PM
Multiple Field Seperators in Awk... TheDarktrooper Programming 6 05-06-2004 04:50 AM
Two field seperators in awk?? Astro Programming 2 11-09-2003 10:12 AM


All times are GMT -5. The time now is 07:58 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration