LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 02-06-2008, 10:12 AM   #16
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76

Hi.
Quote:
Originally Posted by jschiwal View Post
I did triple check my results with a sample example, but I'll take your word for it. You could use the quit command if you are extracting information from a file.
Code:
jschiwal@hpamd64:~> cat test
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8

jschiwal@hpamd64:~> sed -i '/4/s/4/four/;/4/q' test
jschiwal@hpamd64:~> cat test
line 1
line 2
line 3
line four
line 5
line 6
line 7
line 8
Code:
sed --version
GNU sed version 4.1.5
Your sed replaces the string that is searched for in the sed quit sub-command "/4/q". However, because that was replaced, the line for the quit never matched, and all lines were copied.

If we add a line with two "4" strings, it will show the truncation. If we do the substitution at the beginning of line as we did earlier, it will also truncate. Here's the former case:
Code:
#!/usr/bin/env sh

# @(#) user1    Demonstrate truncation with sed.

#  ____
# /
# |   Infrastructure BEGIN

echo
set -o nounset

debug=":"
debug="echo"

## The shebang using "env" line is designed for portability. For
#  higher security, use:
#
#  #!/bin/sh -

## Use local command version for the commands in this demonstration.

set +o nounset
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version =o $(_eat $0 $1) sed
set -o nounset

echo

FILE=${1-user-data1}
cat >$FILE <<'EOF'
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9 two fours to change 4 4
line 10
EOF

echo " Input file $FILE:"
cat $FILE
echo
echo "( $( wc $FILE ) )"

# |   Infrastructure END
# \
#  ---

echo
echo " Results from processing:"
# jschiwal@hpamd64:~> sed -i '/4/s/4/four/;/4/q'
sed -i '/4/s/4/four /;/4/q' $FILE
cat $FILE

echo " --- line above is last line in $FILE"

exit 0
Producing:
Code:
% ./user1

(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)
GNU sed version 4.1.2

 Input file user-data1:
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9 two fours to change 4 4
line 10

( 10 26 95 user-data1 )

 Results from processing:
line 1
line 2
line 3
line four
line 5
line 6
line 7
line 8
line 9 two fours to change four  4
 --- line above is last line in user-data1
cheers, makyo
 
Old 12-17-2012, 03:07 AM   #17
ubungu
LQ Newbie
 
Registered: Jun 2012
Location: /home/ubungu
Posts: 27

Rep: Reputation: Disabled
Quote:
Originally Posted by Tinkster View Post
Depends on your preferences, the task at hand and your knowledge
of the file to edit in question.

I reckon that if I *know* that line 19375 needs to be
commented my sed
Code:
sed -i '19375 s/^/#/' file
is going to be done quicker than someones pico (nano, whatever)




Cheers,
Tink
Thank you very much, this is the way for insert string beginning of line :d
 
Old 12-19-2012, 08:21 AM   #18
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please don't re-open old threads unless you have something substantial to add to that discussion, such as a correction or updated information. If you have a new question of your own, start a new thread and refer back to the old one if necessary. Thanks.


Now to supply my own substantial addition to the thread , these days I like to use ed for many things like this. It has a more flexible addressing system because it operates on the whole file at once.

Code:
printf '%s\n' '19375 s/^/# /' 'w' | ed -s file.txt
printf '%s\n' 'g/pattern/ s/^/# /' 'w' | ed -s file.txt
How to use ed:
http://wiki.bash-hackers.org/howto/edit-ed
http://snap.nlc.dcccd.edu/learn/nlc/ed.html
(also read the info page)
 
  


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
Insert CRLF's with sed? unSpawn Programming 6 11-22-2006 07:46 AM
SED - replace / insert furquan Programming 5 03-01-2006 06:58 PM
insert string with sed greg108 Programming 7 02-18-2005 01:11 PM
insert a symbol with sed tonton Programming 5 08-31-2004 11:33 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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