LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 04-12-2010, 04:22 AM   #1
shayori
LQ Newbie
 
Registered: Feb 2010
Posts: 10

Rep: Reputation: 0
how to modify a line of a file using shell script?


I have a file like below

n
p
1
1
50
w

I need to modify the first occurence of 1 byt some other integer.

How do i do it using shell script?Can anybode help me out with that?
Thanks in advance

Shayori
 
Old 04-12-2010, 05:26 AM   #2
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Could you please explain this problem a bit more?

1 byt with integer means ?
 
Old 04-12-2010, 06:26 AM   #3
bakdong
Member
 
Registered: Apr 2009
Posts: 214

Rep: Reputation: 44
I suspect you will probably be looking for sed

http://sed.sourceforge.net/sed1line.txt
 
Old 04-12-2010, 07:51 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
I think you mean: "replace the first occurence of "1" with a different integer."

SED works line by line, and there is no (easy) way of having it stop after changing just one line. Take a look also at AWK.

Good tutorials on both SED and AWK here: http://www.grymoire.com/Unix/
 
1 members found this post helpful.
Old 04-13-2010, 12:24 AM   #5
bsat
Member
 
Registered: Feb 2009
Posts: 347

Rep: Reputation: 72
You can use grep ( -m option) to limit the output only to the first occurrence and then pipe the output to sed.
 
Old 04-13-2010, 02:54 AM   #6
bakdong
Member
 
Registered: Apr 2009
Posts: 214

Rep: Reputation: 44
Quote:
Originally Posted by pixellany View Post
I think you mean: "replace the first occurence of "1" with a different integer."

SED works line by line, and there is no (easy) way of having it stop after changing just one line.
This doesn't seem too difficult....

Code:
# cat all.txt
1
3
2
1
7
4
a
5
h
a
a
a
99
Code:
# sed -e '1,/a/ s/a/B/' all.txt
1
3
2
1
7
4
B
5
h
a
a
a
99
 
1 members found this post helpful.
Old 04-13-2010, 03:05 AM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
I think until the OP answers PMP's question, the current set of replies are shooting in the dark.
 
Old 04-13-2010, 03:23 AM   #8
bakdong
Member
 
Registered: Apr 2009
Posts: 214

Rep: Reputation: 44
You may be right, but it's always fun playing with sed!

and, although I could be wrong

Quote:
I need to modify the first occurence of 1 by some other integer.
seems fairly clear to me.
 
Old 04-16-2010, 01:08 AM   #9
shayori
LQ Newbie
 
Registered: Feb 2010
Posts: 10

Original Poster
Rep: Reputation: 0
Thank you for the help

Thank you to all.
sed is working for me.Its a real powerful command,a lot can be done with it.
I need to dig in more to understand 'sed' very well.
Anyways Thanks a lot once again
 
Old 04-16-2010, 03:44 AM   #10
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
If you have your answer, please mark as SOLVED.
 
  


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
bash shell script read file line by line. Darren[UoW] Programming 51 07-04-2013 09:16 PM
bash Shell script to modify DNS zone file KinnowGrower Programming 4 12-12-2009 09:07 PM
i have to modify a line in a file using shell scripts,i dont know how to go about it liunx Linux - General 9 02-16-2009 01:22 PM
modify content in a file using shell script fjkum Linux - Newbie 2 08-03-2006 05:46 AM


All times are GMT -5. The time now is 09:44 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration