Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.