sed line range, all but the last line
sed -e “$ ! s/a/b/” This will match every line but the last. Confirmed with a quick test!
sed -e “$ ! s/a/b/” This will match every line but the last. Confirmed with a quick test!
Since | is a valid regex expression, it needs to be escaped with \\| or put in square brackets: [|]. You can do this: awk -F’ \\|\\|\\| ‘ ‘{print $1}’ file Some other variations that work as well: awk -F’ [|][|][|] ‘ ‘{print “$1”}’ file awk -F’ [|]{3} ‘ ‘{print “$1”}’ file awk -F’ \\|{3} … Read more
sed -n ‘/matched/,$p’ file awk ‘/matched/,0’ file
GNU sed has a suitable addressing mode: sed -n ‘1~2!p’ file which means, starting from line 1, and with step 2, print all other lines. Equivalently, you can drop the -n, and delete matching lines: sed ‘1~2d’ It can also be done using awk: awk ‘NR%2==0’ file (Whenever line number is a multiple of 2, … Read more
For case-insensitive use /I instead of /i. sed -e “/pattern/Id” filepath
I figured out what was wrong. I needed to add ” after the -i and before the ‘s/../../’: grep -l \’texttofind\’ * | xargs sed -i ” ‘s/toreplace/replacewith/g’
This works: sed -rne ‘s/(dbservername)\s+\w+/\1 yyy/gip’ (When you use the -r option, you don’t have to escape the parens.) Bit of explanation: -r is extended regular expressions – makes a difference to how the regex is written. -n does not print unless specified – sed prints by default otherwise, -e means what follows it is … Read more
You can use an alternative regex delimiter as a search pattern by backslashing it: sed ‘\,some/path,d’ And just use it as is for the s command: sed ‘s,some/path,other/path,’ You probably want to protect other metacharacters, though; this is a good place to use Perl and quotemeta, or equivalents in other scripting languages. From man sed: … Read more
try this short thing: awk ‘!($3=””)’ file
You just need to escape the quote in your first example: $ sed ‘s/\”//g’ file.txt