recentpopularlog in

kme : sed   57

How do I stop sed from adding extra newline characters - Unix & Linux Stack Exchange
GNU sed, if it's available, will not print an extraneous newline if you do 'sed -n "s/patt/repl/p"' (source:
If you do need that file not to end in a newline character, then you could use perl or other tools that can cope with non-text data.
<code class="language-bash">perl -pe 's|<LIST_G_STATEMENT>|$&\n|g'</code>
perl  sed  newlines  textprocessing  unix  annoyance  solution 
december 2019 by kme
bash - Pad one-digit, two-digit and three-digit numbers with zeros with sed - Stack Overflow
<code class="language-bash">$ sed -E 's/([[:digit:]]+)/000&/g;s/0+([[:digit:]]{4})/\1/g' file.txt</code>
sed  textprocessing  shellscripting  solution 
may 2019 by kme
command line - sed weirdness, unmatched { - Super User |
The 'e' GNU extension (execute process) also falls into this category: it must have a newline right after it; try using the ANSI-C quoting ($'string') feature of Bash to substitute in a literal newline.
sed  shellscripting  solution  syntax 
may 2019 by kme
Sed command returning "invalid command code" - Stack Overflow
I had this error on macOS and the problem was that '-i' expects a (zero-length) string argument. But this is a weird-ass way of telling you that.
macos  bsd  sed  shellscripting  annoyance  erromessage  solution 
march 2019 by kme
use of alternation "|" in sed's regex - Super User |
<code style="language-bash">echo "blia blib bou blf" | sed 's/bl\(ia\|f\)//g'</code>
For anyone else confused by this answer \| only works in gnu sed (gsed on os x) not vanilla sed (sed on os x). – Andrew Hancox Apr 4 '12 at 14:54
sed  shellscripting  textprocessing  syntax  newbie  dammitbrain  linuxonly  solution 
march 2019 by kme
linux - How to use sed to remove the last n lines of a file - Stack Overflow |
Yeah, it's possible in 'sed', but ugly.

<code class="language-bash">head -n -2 myfile.txt</code>
bash  linux  sed  shellscripting  textprocessing  solution 
march 2019 by kme
bash - How to add a carriage return with sed? - Stack Overflow |
In this case, this guy was right.
sed is for simple subsitutions on individual lines, that is all. For anything else you should be using awk:
shellscripting  sed  awk  textprocessing  whitespace  sortof  solution 
march 2019 by kme
Is there an alternative to sed that supports unicode? - Unix & Linux Stack Exchange |
Just use that syntax:

sed 's/馑//g' file1

Or in the escaped form:

sed "s/$(echo -ne '\u9991')//g" file1

(Note that older versions of Bash and some shells do not understand echo -e '\u9991', so check first.)
sed  unicode  textprocessing  solution 
january 2019 by kme
scripting - The way to use `/usr/bin/env sed -f ` in shebang? - Unix & Linux Stack Exchange |
You can't use 'env', so just '#!/usr/bin/sed -f' should be OK.

The problem with that is (as mentioned in the top-rated answer), you can't include more than one argument in a shebang, so you need to use one of the alternate techniques if you want to, say, do 'sed -n 'f'.
For a long script, it may be more convenient to use a heredoc. An advantage of a heredoc is that you don't need to quote the single quotes inside, if any. A major downside is that the script is fed to sed on its standard input, with two annoying consequences. Some versions of sed require -f /dev/stdin instead of -f -, which is a problem for portability. Worse, the script can't act as a filter, because the standard input is the script and can't be the data.
<code class="language-bash">
exec sed -f - -- "$@" <<'EOF'
sed  shellscripting  shebang  solution  dammitbrain 
january 2018 by kme
How can I replace a newline (n) using sed? - Stack Overflow |
Note that you have to give each command as a separate `-e` in OS X and BSD. I thought that the `$!ba` was the problem (history expansion), and wasted a bunch of time trying to figure out why it wouldn't work on OS X. It was the semicolons!
Fast answer:

<code class="language-bash">sed ':a;N;$!ba;s/\n/ /g' file</code>

- :a create a label 'a'
- N append the next line to the pattern space
- $! if not the last line, ba branch (go to) label 'a'
- s substitute, /\n/ regex for new line, / / by a space, /g global match (as many times as it can)

sed will loop through step 1 to 3 until it reach the last line, getting all lines fit in the pattern space where sed will substitute all \n characters

All alternatives, unlike sed will not need to reach the last line to begin the process

with bash, slow

<code class="language-"bash>while read line; do printf "%s" "$line "; done < file</code>

with perl, sed-like speed

<code class="language-bash">perl -p -e 's/\n/ /' file</code>

with tr, faster than sed, can replace by one character only

<code class="language-bash">tr '\n' ' ' < file</code>

with paste, tr-like speed, can replace by one character only

<code class="language-bash">paste -s -d ' ' file</code>

with awk, tr-like speed

<code class="language-bash">awk 1 ORS=' ' file</code>
bash  shellscript  sed  syntax  newbie  textprocessing  fuckina  solution 
november 2017 by kme
sed - Merge two lines into one - Stack Overflow
Note the options using 'tr' and 'paste':
paste -d " " - - < filename

Related (
ls -1 | tr -d '\n'
shellscripting  newlines  whitespace  sed  awk  textprocessing  dammitbrain  maybesolution 
april 2016 by kme
Non greedy regex matching in sed? - Stack Overflow
Neither basic nor extended Posix/GNU regex recognizes the non-greedy quantifier; you need a later regex. Fortunately, Perl regex for this context is pretty easy to get:

perl -pe 's|(http://.*?/).*|\1|'
perl  sed  regexp  regex  nongreedy  quantifier  essential  textprocessing  dammitbrain  solution 
july 2015 by kme
How to get only filename using sed - Unix & Linux Stack Exchange
out_file="$(echo $in_file | sed 's=.*/==;s/\.[^.]*$/.new_ext/'
sed  oneliner  solution  shell  commandline  scripting 
july 2014 by kme
List of 'sed' one-liners
See also
<code class="language-bash"># undo double-spacing (assumes even-numbered lines are always blank)
sed 'n;d'</code>

<code class="language-bash"> # join pairs of lines side-by-side (like "paste")
sed '$!N;s/\n/ /'

# if a line ends with a backslash, append the next line to it
sed -e :a -e '/\\$/N; s/\\\n//; ta'

# if a line begins with an equal sign, append it to the previous line
# and replace the "=" with a single space
sed -e :a -e '$!N;s/\n=/ /;ta' -e 'P;D'
programming  reference  sed  essential  oneliners  shellscripting  textprocessing  dammitbrain 
may 2008 by kme

Copy this bookmark:

to read