LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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-05-2010, 07:41 AM   #1
quanba
Member
 
Registered: Jul 2009
Posts: 41

Rep: Reputation: 15
awk: How can I keep the format of the record?


Hi there,

In /etc/fstab, I have a record:

LABEL=/<\t\t\t>/<\t\t\t>ext3<\t\t\t>default,nolog<\t>1 1

\t: represent a tab between 2 fields

I just want to remove "nolog" in the 4th field (only): gsub(/nolog/,"",$4)

The function work ok but it returns a record:
LABEL=/ / ext3 default 1 1

I know the problem is OFS=" ", but how can I keep the format of the record? (the same number of tabs, spaces).

Thanks for your help

Last edited by quanba; 04-05-2010 at 09:20 PM.
 
Old 04-05-2010, 07:49 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
why don't you just gsub?
Code:
awk '{gsub(",nolog","")}1' file
 
Old 04-05-2010, 08:13 AM   #3
quanba
Member
 
Registered: Jul 2009
Posts: 41

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ghostdog74 View Post
why don't you just gsub?
Code:
awk '{gsub(",nolog","")}1' file
I thought about your solution, but I want to gsub at the 4th field only, for example:

/tmp/nolog/abc / ext3,nolog

In this case, gsub will replace nolog in the directory name
 
Old 04-05-2010, 09:12 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Firstly, your assumption about replacing nolog in the directory name is incorrect as there is no preceding comma (,).

But I am not sure why you feel the format has changed with your original example?
There appears to be the same number of white space in both the original and the removed version on the line.

Maybe you would clarify a little further?
 
Old 04-05-2010, 09:22 PM   #5
quanba
Member
 
Registered: Jul 2009
Posts: 41

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by grail View Post
Firstly, your assumption about replacing nolog in the directory name is incorrect as there is no preceding comma (,).

But I am not sure why you feel the format has changed with your original example?
There appears to be the same number of white space in both the original and the removed version on the line.

Maybe you would clarify a little further?
Sorry for the inconvenience, I edited the thread above
 
Old 04-05-2010, 09:50 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Well unless you think you will have a comma in a directory name or that option anywhere else on the line,
it would appear ghostdog's solution is the winner
 
  


Reply



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
updating a field in a record through AWK suresh.chola Programming 9 01-18-2010 08:07 AM
[SOLVED] split multi line record into multiple files with awk pcock Programming 10 11-12-2009 04:12 AM
awk record separator question johnpaulodonnell Linux - Newbie 2 07-30-2007 09:35 AM
awk syntax to print particular record of xyz file? johnpaulodonnell Linux - Newbie 4 06-14-2007 07:47 AM
awk: How can I return a specified record dimsh Linux - Newbie 4 09-24-2005 11:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:17 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration