sed question - change 2d to last line in file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers sed question - change 2d to last line in file
# 1  
Old 05-29-2011
sed question - change 2d to last line in file

The file is repeating blocks of text separated by a blank line. Some of the lines are unique.

e.g.:

This is unique line
This is line 2
This is line 3
This is line 4

This is unique line
This is line 2
This is line 3
This is line 4

...


The goal is to change/replace line 4 only on the last block, in a single pass.

Available tools are /bin/sh and some of the POSIX standard utilities. But no awk. No Perl, etc.

It's possible in sed. But how to do it in one pass?

Here's a two-pass.

Code:
sed '$d' < file |sed '$d'
printf "This is the new line 4\n"

And here's a script that changes only line 4 in the _first_ block.

Code:
# sed -nf script < file

/This is line 4/{
s//This is the new line 4/
:loop
p
n
bloop
}
p





Cheers.

Last edited by uiop44; 05-29-2011 at 10:08 PM..
# 2  
Old 05-29-2011
Here's a single pass sed that should work. I've put each command on a separate line to make it more clear.

Code:
sed '
$ b last
/^$/ b blank
H
d
b
:blank
x
p
d
x
b
:last
x
s/This is line 4/New Line Four/
p
x
' tdata

I tested this with GNU sed version 4.1.5.

I've not added an explanation of the logic on the off chance that you'd like to figure it out; I'd be happy to if you'd like.

There is a big assumption that you know the line 4 text to put into a substitute command. If it's not known, this won't work, but from your description I believe you do know it.

The one liner is below:

Code:
' $ b last; /^$/ b blank; H; d; b; :blank x; p; d; x; b; : last x; s/This is line 4/new line 4/; p; x' tdata

# 3  
Old 05-29-2011
solved

Found via the infamous sed one-liners file.

Code:
# delete last 2 lines of file

sed 'N;$!P;$!D;$d' < file
printf "This is the new line 4\n"
printf "\n"

---------- Post updated at 08:36 PM ---------- Previous update was at 08:24 PM ----------

Thanks agama.

Indeed line 4 is known.

This looks clear and I will study it as I really want to start using loops and the full range of sed commands. I use sed constantly but I'm just not there yet.


Quote:
Originally Posted by agama
Here's a single pass sed that should work. I've put each command on a separate line to make it more clear.

Code:
sed '
$ b last
/^$/ b blank
H
d
b
:blank
x
p
d
x
b
:last
x
s/This is line 4/New Line Four/
p
x
' tdata

I tested this with GNU sed version 4.1.5.

I've not added an explanation of the logic on the off chance that you'd like to figure it out; I'd be happy to if you'd like.

There is a big assumption that you know the line 4 text to put into a substitute command. If it's not known, this won't work, but from your description I believe you do know it.

The one liner is below:

Code:
' $ b last; /^$/ b blank; H; d; b; :blank x; p; d; x; b; : last x; s/This is line 4/new line 4/; p; x' tdata

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

File name change with sed

I have a bunch of text files like this: Sample_S1_L001_R1.txt Sample_S10_L001_R1.txt Sample_S11_L001_R1.txt I am using the following script to add a 0 to those files with a single digit after the S: ls *.txt | sed 's/\(.*_S\)\(_.*\)/mv & \10\2/' | sh And then the following script to... (4 Replies)
Discussion started by: Xterra
4 Replies

2. Shell Programming and Scripting

Trying to take file numbers from a file, pass them to sed to change strings in corresponding lines

I have a bunch of file numbers in the file 'test': I'm trying the above command to change all the instances of "H" to "Na+" in the file testsds.pdb at the line numbers indicated in the file 'test'. I've tried the following and various similar alternatives but nothing is working: cat test |... (3 Replies)
Discussion started by: crunchgargoyle
3 Replies

3. Shell Programming and Scripting

simple sed question - replace from phrase to end of line

I have the following line an in input file I want to digest with sed and simple replace the bold part with a variable defined in my bash script. I can do this in several sed operations but I know there must be a way to do it in a single sed line. What is the syntax? Line in file:... (1 Reply)
Discussion started by: graysky
1 Replies

4. Shell Programming and Scripting

SED Question: Search and Replace start of line to matching pattern

Hi guys, got a problem here with sed on the command line. If i have a string as below: online xx:wer:xcv: sdf:/asdf/http:https-asdfd How can i match the pattern "http:" and replace the start of the string to the pattern with null? I tried the following but it doesn't work: ... (3 Replies)
Discussion started by: DrivesMeCrazy
3 Replies

5. Shell Programming and Scripting

change part of a line with sed

Hi gurus, I'd like to change this complete line on a file: BAN_COMMAND="/etc/apf/apf -d $ATTACK_HOST {bfd.$MOD}" to this one: BAN_COMMAND="/sbin/iptables -A INPUT -s $ATTACK_HOST -j DROP" I've tried a lot without any successful . :( thanks in advance Israel. (2 Replies)
Discussion started by: iga3725
2 Replies

6. Shell Programming and Scripting

Using sed command to change end of line

I am looking to change a data file into a javascript string and this is the code that I am using: sed -i '' -e 's/^/str += "/' -e 's/$/";/' file.xml The first part -e 's/^/str += "/' works as intended, but the second part -e 's/$/";/' adds an additional newline to my file, so that instead of... (3 Replies)
Discussion started by: figaro
3 Replies

7. Shell Programming and Scripting

Script to change file contents line by line

Hi, I'm struggling to write a script to do the following, -will go through each line in the file -in a specific character positions, changes the value to a new value -These character positions are fixed througout the file ----------------------- e.g.: file1.sh will have the following 3... (4 Replies)
Discussion started by: vini99
4 Replies

8. Shell Programming and Scripting

Sed question (Removing a line of text)

I am working with bash on HP-UX server at school. As practice for scripting, I am trying to make a pretend server admin script that adds a user to the system, deletes a user from the system, and lists all users of the pretend system. I have accomplished this with a select loop. Adding users, and... (2 Replies)
Discussion started by: masterscout1977
2 Replies

9. Shell Programming and Scripting

change line found by pattern using sed

I want to change a line like CPM_THRESHOLD 0.8 // to a new value using sed I am trying sed -i "s/CPM_THRESHOLD/CPM_THRESHOLD\t$COH\t\t\/\//" $INPUT_4 but how can i substitute the whole line begining with CPM_THRESHOLD and substitute it? (2 Replies)
Discussion started by: larne
2 Replies

10. Shell Programming and Scripting

How to change this file with SED?

I have a file like below: I want to delete the rows which begining with "BC" "CD" and "TY" . then change every fields into one row like this at last delete the head words : USing awk to change it is not hard. I want to know how to do this work using SED ? Thank you! ... (4 Replies)
Discussion started by: bejgirl
4 Replies
Login or Register to Ask a Question