LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-13-2009, 12:00 PM   #1
liunx
LQ Newbie
 
Registered: Feb 2009
Location: around the world
Distribution: red hat
Posts: 5

Rep: Reputation: 0
i have to modify a line in a file using shell scripts,i dont know how to go about it


the content of the file is more like this

101 bruno alex 29383 2009
102 mary smith 2829 2892
103 junu adama 282 828298
104 wendy will 29373 122

assuming i want to modify the first line with 101 what could possibly be the solution.
the name of the file is details.
 
Old 02-13-2009, 12:07 PM   #2
repo
LQ 5k Club
 
Registered: May 2001
Location: Belgium
Distribution: Arch
Posts: 8,529

Rep: Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899
what have you tried so far?
 
Old 02-13-2009, 08:59 PM   #3
nflenz
Member
 
Registered: Feb 2006
Distribution: CRUX 2.4
Posts: 96

Rep: Reputation: 18
Code:
$ sed -i /101/s/bruno/not-bruno/ filename
 
Old 02-13-2009, 09:10 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,103

Rep: Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117
First post - does this look like homework or not ?.
 
Old 02-15-2009, 03:19 AM   #5
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 4,070

Rep: Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897
Quote:
Originally Posted by syg00 View Post
First post - does this look like homework or not ?.
Looks like homework to me....

First you have to select something with which to make the change. sed/AWK/python/perl all sound like the kind of thing that you could use, so, having a short list you select a possible candidate (say sed, that seems like a good choice) and read the man page. So you issue the command

'man sed'

and you get information. (If you didn't have a list of possibles, you try 'man -k string' and see if that helps; as an example 'man -k change' shows you a lot of unintresting stuff, but 'man -k edit' gets lists stuff you might actually use).

Having read the man page, you try something. If after that you can't understand what is going on, or there is a definable area that you didn't understand, you could try posting on Linuxquestions to see if someone can help you on to the next stage, telling people what you have tried or what you think. then, whether it is homework or not, someone will probably actually help, rather than giving you a fragment of code that does the job without helping you much
 
Old 02-16-2009, 11:50 AM   #6
liunx
LQ Newbie
 
Registered: Feb 2009
Location: around the world
Distribution: red hat
Posts: 5

Original Poster
Rep: Reputation: 0
thanks

Quote:
Originally Posted by repo View Post
what have you tried so far?
i have tired using sed
as in
sed '101d' filename
but its not deleting the line with 101

i am new to shell scripting
and linux. "newbie"
 
Old 02-16-2009, 11:53 AM   #7
liunx
LQ Newbie
 
Registered: Feb 2009
Location: around the world
Distribution: red hat
Posts: 5

Original Poster
Rep: Reputation: 0
thanks for ur idea

i have done that;
i am trying to do it using shell scripts
 
Old 02-16-2009, 11:56 AM   #8
liunx
LQ Newbie
 
Registered: Feb 2009
Location: around the world
Distribution: red hat
Posts: 5

Original Poster
Rep: Reputation: 0
yes

more like it
 
Old 02-16-2009, 11:57 AM   #9
liunx
LQ Newbie
 
Registered: Feb 2009
Location: around the world
Distribution: red hat
Posts: 5

Original Poster
Rep: Reputation: 0
thanks

i ll try it now
 
Old 02-16-2009, 12:22 PM   #10
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Quote:
sed '101d' filename
That will delete line 101. Not a line that matches "101".

Be careful which regular expression you use to find a match. To simple a pattern, and you might delete other lines as well.
Use ^, $ and literals (as anchors) to prevent false matches. Test what you have on a larger set. For example, if you wanted to match the line "104 wendy will 29373 122" and a later line had 104 in the 5th column, it would be deleted as well.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 06:07 PM
I would like need a suggestion on bash shell : Read a file line by line and do stuff madi3d8 Linux - Newbie 1 01-15-2009 09:30 AM
modify content in a file using shell script fjkum Linux - Newbie 2 08-03-2006 04:46 AM
how to modify a text file or a string in shell programming luckyvietman Programming 5 07-12-2005 05:08 PM
Scripts failing with #!/bin/bash or other shell on first line rdfoerster Fedora 6 11-16-2004 07:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:00 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