Reputation: 17467
I want to replace second last line of file, I know $ use for last line but don't know how to say second line from end.
parallel (
{
ignore(FAILURE) {
build( "Build2Test", BUILDFILE: "", WARFILE: "http://maven.example.com/130602.0.war", STUDY: "UK", BUG: "33323" )
}},
)
I want to replace }},
with }}
in short i want to remove ,
comma but this file has many other codes so i can't use pattern match i need to use second line from end of file.
Upvotes: 4
Views: 6330
Reputation: 63974
If you know, how to change N-th line, simply reverse the the file first, e.g. It is not as much professional as the other sed solutions, but works... :)
tail -r <file | sed '2s/}},/}}/' | tail -r >newfile
e.g. from the next input
}},
}},
}},
}},
}},
the above makes
}},
}},
}},
}}
}},
The tail -r
are BSD equivalent of Linux's tac
command. On Linux use tac
on OS X or Freebsd use tail -r
. Bot doing the same: prints the file in reveresed order of lines (last line prints as first).
Upvotes: 7
Reputation: 247200
reverse the file, work on the 2nd line, then re-reverse the file:
tac file | sed '2 s/,$//' | tac
to save the result back to "file", add this to the command
> file.new && mv file file.bak && mv file.new file
Or, use an ed
script
ed file <<END
$-1 s/,$//
w
q
END
Upvotes: 6
Reputation: 58568
This might work for you (GNU sed):
sed '$!N;$s/}},/}}/;P;D' file
Keep two lines in the pattern space and at-end-of-file substitute the required pattern.
Upvotes: 3
Reputation: 208665
The following should work (note that on some systems you may need to remove all of the comments):
sed '1 { # if this is the first line
h # copy to hold space
d # delete pattern space and return to start
}
/^}},$/ { # if this line matches regex /^}},$/
x # exchange pattern and hold space
b # print pattern space and return to start
}
H # append line to hold space
$ { # if this is the last line
x # exchange pattern and hold space
s/^}},/}}/ # replace "}}," at start of pattern space with "}}"
b # print pattern space and return to start
}
d # delete pattern space and return to start'
Or the compact version:
sed '1{h;d};/^}},$/{x;b};H;${x;s/^}},/}}/;b};d'
Example:
$ echo 'parallel (
{
ignore(FAILURE) {
build( "Build2Test", BUILDFILE: "", WARFILE: "http://maven.example.com/130602.0.war", STUDY: "UK", BUG: "33323" )
}},
)' | sed '1{h;d};/^}},$/{x;b};H;${x;s/^}},/}}/;b};d'
parallel (
{
ignore(FAILURE) {
build( "Build2Test", BUILDFILE: "", WARFILE: "http://maven.example.com/130602.0.war", STUDY: "UK", BUG: "33323" )
}}
)
Upvotes: 7