LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 01-19-2010, 03:55 AM   #1
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Rep: Reputation: 15
Replacing string using awk


Hey guys, i have a string of information displayed in this way :

Code:
John:king:20:34:60
what i am tring to do is to read in input which is given by the user and change it to the correct string , which is $2.

I came up with a coding like this :

Code:
awk -F: -OFS: '{ $1 = "text" ; print}' database.txt > newdatabase.text
\

what it suppose to do is to read "$1" and replace it with the word text and then print the new rows out into the new database.text file.
did i get the syntax wrong? could somebody help me with this?

Last edited by gregarion; 01-19-2010 at 03:59 AM.
 
Old 01-19-2010, 04:15 AM   #2
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Original Poster
Rep: Reputation: 15
alright guys, i got the answer to it!

Code:
awk  '{ $1 = "'$edd'" ; print $0 }' fruittemp > fruittemp2
 
Old 01-19-2010, 04:56 AM   #3
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
I know you have this working, but just for info, sed is generally used for string replacement such as this.
 
Old 01-19-2010, 11:02 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by devnull10 View Post
I know you have this working, but just for info, sed is generally used for string replacement such as this.
yes it does, but the data can be broken down into fields with a distinct field delimiter. The better approach is not regex.
 
  


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
c++ : replacing a char in a string cb951303 Programming 20 02-25-2015 08:18 PM
Replacing certain parts of a string dipuasks Linux - Server 5 11-22-2008 07:09 AM
Error in replacing special chars using awk linux_vidhyarthi Linux - General 2 04-09-2008 07:39 PM
Replacing text on specific lines with sed or awk? Lantzvillian Linux - Newbie 5 10-17-2007 09:00 AM
replacing string within same file tostay2003 Programming 2 07-06-2007 02:25 AM

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

All times are GMT -5. The time now is 12:01 PM.

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