LinuxQuestions.org
Visit Jeremy's Blog.
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 09-24-2013, 06:00 PM   #1
aihaike
Member
 
Registered: Feb 2006
Location: Paris, France
Distribution: Slackware, CentOS
Posts: 270

Rep: Reputation: 20
Question Modifying lines in file from the shell


Dear all,

I've got to modify lines of 100 of files.
When I have several lines to change, I usually use nedit which can handle this and apply the modification to all open files as well. In order to not open those files I thought about using sed but can't figure how to modify a bloc of lines at once.
Would you please give me some hint here?
Thank you,

Éric.
 
Old 09-24-2013, 06:02 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
We need a lot more information about what you're trying to do. What lines are you changing and how are you changing them? Are the changes based on line and column numbers or specific words/strings that need to be searched for?

In other words, how would you describe your actions in machine language?
"Replace the character on line 16, column 12 with a 5"
or
"Replace the fourth word on line 12 with 'yellow'"
or
"Find the second line that begins with 'See spot', and replace the third word with 'run'"
or
"Delete line 23, but only if it contains the phrase 'rotten sandwich'"
or
???

Last edited by suicidaleggroll; 09-24-2013 at 06:08 PM.
 
Old 09-24-2013, 06:41 PM   #3
aihaike
Member
 
Registered: Feb 2006
Location: Paris, France
Distribution: Slackware, CentOS
Posts: 270

Original Poster
Rep: Reputation: 20
yes, you're right.
I need to replace those lines (they actually correspond to line 35 to 47):

Code:
CURDIR=$PWD
cd $CURDIR
rm -f nodelist >& /dev/null
touch nodelist

for host in `echo $LSB_HOSTS`
do
echo $host >> nodelist
sleep 2
done

NP=`cat nodelist |wc -l`
NN=`cat nodelist |sort |uniq|tee nodes |wc -l`
by those ones:

Code:
CURDIR=$PWD
cd $CURDIR
rm -f nodelist nodes n >& /dev/null
touch nodelist
touch nodes
NP=0

for host in `echo $LSB_MCPU_HOSTS |sed -e 's/ /:/g'|  sed 's/:n/\nn/g'`
do
echo $host >> nodelist
echo $host | cut -d ":" -f1 >> nodes
nn=`echo $host | cut -d ":" -f2`
NP=`echo $NP+$nn | bc`
done

NN=`cat nodelist | wc -l`
thanks.
 
Old 09-24-2013, 07:35 PM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,879

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
This is a simple and contrived example of a block replacement. Adapt it to your own application.

InFile1 ...
Code:
one
two
three
four
five
six
seven
eight
nine
ten
InFile2 ...
Code:
FOUR
FIVE
SIX
The first sed makes a copy of lines 1-3 in a work file and the second sed makes a copy of lines 7-EOF in another work file. The cat combines those work files with a second input file making a conceptual "sandwich."
Code:
sed '3q'   $InFile1 >$Work1
sed '1,6d' $InFile1 >$Work2
cat $Work1 $InFile2 $Work2 >$OutFile
Executing this code produced this OutFile ...
Code:
one
two
three
FOUR
FIVE
SIX
seven
eight
nine
ten
Daniel B. Martin
 
1 members found this post helpful.
Old 09-24-2013, 08:18 PM   #5
aihaike
Member
 
Registered: Feb 2006
Location: Paris, France
Distribution: Slackware, CentOS
Posts: 270

Original Poster
Rep: Reputation: 20
Thumbs up

It works, thank you for the tip.

Éric.
 
  


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
Modifying records of a file using shell scripting barunparichha Linux - Software 5 05-21-2009 09:33 AM
Looking for a shell script that prints a file only if it has more than 10 lines djfog Linux - Newbie 8 07-01-2008 12:23 PM
Shell script for modifying file extensions VTGuitarMan Linux - Newbie 6 03-08-2008 07:24 PM
modifying a file in shell (via sed ?) Shautieh Programming 7 09-19-2006 05:14 AM
[c shell] How do I find how many lines a file has? saiz66 Programming 5 10-08-2004 03:01 PM

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

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