Commands tagged sed (354)


  • 0
    id <user_name> | sed 's/,/\n/g' | tr ' ' '\n'
    gadjodilo · 2017-12-24 02:33:40 0
  • Remove all line from a playlist m3u that not finish with MP3 or mp3 or wma or flac creating a new clean plalist m3u Show Sample Output


    0
    sed -e '/MP3$\|mp3$\|wma$\|flac$/!d' <filename_source> > <filename_destination>
    vinabb · 2017-03-21 22:14:08 0
  • Removes the last 5 lines from a file using sed


    0
    sed -n -e :a -i -e '1,5!{P;N;D;};N;ba' /etc/apt/sources.list
    redowk · 2017-03-06 18:22:48 0
  • Tails make.out file and pipes it to sed which uses 3 colors: green: percentage (eg: 10%) yellow: warning (case insensitive) red: error (case insensitive)


    0
    tail -f ~/make.out | sed -e 's/\(...%\)/\o033[32m\1\o033[39m/' -e 's/\(.*[Ww][Aa][Rr][Nn][Ii][Nn][Gg].*\)/\o033[33m\1\o033[39m/' -e 's/\(.*[Ee][Rr][Rr][Oo][Rr].*\)/\o033[31m\1\o033[39m/'
    redowk · 2017-03-06 18:20:36 0
  • Search a string in YouTube using GET command, parse the response with a suitable replace pattern and pipe its result to VLC Show Sample Output


    0
    GET https://www.youtube.com/results?search_query=shaun+the+sheep | sed -ne '/<a href="\/watch.*title="/s/.*<a href="\(\/watch[^"]*\)".* title="\([^"]*\)".*/https:\/\/www.youtube.com\1/p' | vlc -
    plokij · 2016-10-28 23:55:59 0

  • 1
    id <username> | sed s/' '/'\n'/g | sed s/,/',\n '/g | sed s/'('/' ('/g | sed s/uid/' uid'/g | sed s/gid/' gid'/g | sed s/=/' = '/g
    TuxOtaku · 2016-10-04 16:53:29 1
  • remove the IP from proxy reverse server and parentesis from real IP obtained from X-forwarder_IP Show Sample Output


    0
    tr -s ' ' | cut -d' ' -f2- |cut -c2- |sed 's/)//1'
    horaciod · 2016-09-12 19:25:23 0
  • FQPN - Path = Filename


    0
    Print everything after last slash
    qdrizh · 2016-08-17 07:10:33 1

  • 0
    tr "\|\;" "\n" < ~/.bash_history | sed -e "s/^ //g" | cut -d " " -f 1 | sort | uniq -c | sort -rn | head -20
    turrtle13 · 2016-07-01 19:27:12 0

  • 0
    man $(ls /bin | shuf -n1)
    jubnzv · 2016-06-28 18:34:46 0
  • retrieve file names back from touch commands for them Show Sample Output


    1
    touch files.txt; cat reorder_files.sh | while read line; do x=`echo $line | sed 's/touch \([a-z0-9\.]\+.*.pdf\);.*/\1/'`; echo $x >> files.txt ; done;
    programmer · 2016-04-19 11:01:17 1
  • I had to reconfigure all of my 150 domains to use "localhost" as IMAP/SMTP server instead of mail.[domain]. This little thing did the job in a fraction of a second!


    0
    sed -i -E 's/mail\..*/localhost\"/g' *
    Blagus · 2016-01-15 14:48:32 0
  • On debian parent process is running as root, workers as www-data. You can run strace in backgroud, get its PID, curl your webpage, kill strace and read your stats.


    0
    strace -c $(ps -u www-data o pid= | sed 's/^/-p/')
    brablc · 2015-11-25 08:10:52 0
  • This is how you can do this without having to use oneline Show Sample Output


    0
    git log | nl -w9 -v0 --body-numbering='pcommit\ [0-9a-f]\{40\}' | sed 's/^ \+\([0-9]\+\)\s\+/HEAD~\1 /'
    guywithnose · 2015-11-23 21:53:33 0
  • Print a git log (in reverse order) giving a reference relative to HEAD. HEAD (the current revision) can also be referred to as HEAD~0 The previous revision is HEAD~1 then HEAD~2 etc. . Add line numbers to the git output, starting at zero: ... | nl -v0 | ... . Insert the string 'HEAD~' before the number using sed: ... | sed 's/^ \+/&HEAD~/' . Thanks to bartonski for the idea :-) Show Sample Output


    1
    git log --oneline | nl -v0 | sed 's/^ \+/&HEAD~/'
    flatcap · 2015-11-23 21:35:57 0
  • I personally like it very much and have wrapped it into a function, named "apt-propos" ;), also you can use --names-only option for a sort-of "apt-whatis"


    0
    apt-cache search byo | sed "s/^\([[:alnum:]\.-]*\) - /\1=%%%=- /" | column -s '=%%%=' -t
    VoidDroid · 2015-10-18 10:55:34 0
  • Take the header line from a comma-delimited CSV file and enumerate the fields. . First sed replaces all commas with newlines s/,/\n/g Then sed quits (q) after the first line. Finally, nl numbers all the lines Show Sample Output


    0
    sed 's/,/\n/g;q' file.csv | nl
    flatcap · 2015-08-26 11:38:56 0
  • Replace all instances of "A" with "B" in file "source" saved as file "destination". !! IF A/B is multi-byte, then separate bytes with spaces like so: "s/20\ 0A/00/g". Show Sample Output


    2
    xxd -p source | fold -w2 | paste -sd' ' | sed "s/A/B/g" | xxd -p -r > destination
    hincor · 2015-05-26 18:29:48 0
  • Find and replace specific characters in a single line in multiple files with sed. Show Sample Output


    -1
    for f in `ls`; do sed -i '/MATCHING STRING/ { s/ORIGINAL/REPLACEMENT/; }' ${f} ; done
    krizzo · 2015-05-21 19:37:42 2
  • I used this fragment with Imagemagick convert so that I can place long text strings in pictures. The "\n" gets converted to a true newline in the image. So this fragment uses fold command to wrap the line and then sed to convert newlines (and any trailing spaces on the line) to the text "\n" Show Sample Output


    1
    fold -sw 20 <(echo "Long Text to be wrapped with \"\n\"") |sed ':a;N;$!ba;s/ *\n/\\n/g'
    alecthegeek · 2015-04-16 21:06:53 3
  • Thanks to knoppix5 for the idea :-) Print selected lines from a file or the output of a command. Usage: every NTH MAX [FILE] Print every NTH line (from the first MAX lines) of FILE. If FILE is omitted, stdin is used. The command simply passes the input to a sed script: sed -n -e "${2}q" -e "0~${1}p" ${3:-/dev/stdin} print no output sed -n quit after this many lines (controlled by the second parameter) -e "${2}q" print every NTH line (controlled by the first parameter) -e "0~${1}p" take input from $3 (if it exists) otherwise use /dev/stdin {3:-/dev/stdin} Show Sample Output


    2
    function every() { sed -n -e "${2}q" -e "0~${1}p" ${3:-/dev/stdin}; }
    flatcap · 2015-04-03 01:30:36 4
  • Sometimes commands give you too much feedback. Perhaps 1/100th might be enough. If so, every() is for you. my_verbose_command | every 100 will print every 100th line of output. Specifically, it will print lines 100, 200, 300, etc If you use a negative argument it will print the *first* of a block, my_verbose_command | every -100 It will print lines 1, 101, 201, 301, etc The function wraps up this useful sed snippet: ... | sed -n '0~100p' don't print anything by default sed -n starting at line 0, then every hundred lines ( ~100 ) print. '0~100p' There's also some bash magic to test if the number is negative: we want character 0, length 1, of variable N. ${N:0:1} If it *is* negative, strip off the first character ${N:1} is character 1 onwards (second actual character). Show Sample Output


    1
    function every() { N=$1; S=1; [ "${N:0:1}" = '-' ] && N="${N:1}" || S=0; sed -n "$S~${N}p"; }
    flatcap · 2015-03-21 23:44:59 4
  • In the field, I needed to script a process to scan a specific vendor devices in the network. With the help of nmap, I got all the devices of that particular vendor, and started a scripted netcat session to download configuration files from a tftp server. This is the nmap loop (part of the script). You can however, add another pipe with grep to filter the vendor/manufacturer devices only. If want to check the whole script, check in http://pastebin.com/ju7h4Xf4 Show Sample Output


    0
    nmap -sP 10.0.0.0/8 | grep -v "Host" | tail -n +3 | tr '\n' ' ' | sed 's|Nmap|\nNmap|g' | grep "MAC Address" | cut -d " " -f5,8-15
    jaimerosario · 2014-12-26 18:31:53 0

  • 0
    echo to_camel_case__variable | sed -r 's/(.)_+(.)/\1\U\2/g;s/^[a-z]/\U&/'
    estani · 2014-11-05 13:23:01 0
  • Use sed to search and replace pipes for tabs in file stream with backup


    0
    sed -i.bak -e s/\|/\\t/g filepath.tsv
    johnmoxon · 2014-11-04 00:14:59 0
  •  1 2 3 >  Last ›

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands


Check These Out

Set random background image in gnome
Changes your desktop background image in gnome. Update the directory to wherever you keep your wallpapers. I like to create a sub-directory in my Wallpaper folder called "cycle" that I use to define the wallpapers I wish to loop in cron. ex: $ gconftool-2 -t str -s /desktop/gnome/background/picture_filename "$(find ~/Wallpapers/cycle -type f | shuf -n1)"

Listing directory content of a directory with a lot of entries
Ever wanted to get the directory content with 'ls' or 'find' and had to wait minutes until something was printed? Perl to the rescue. The one-liner above(redirected to a file) took less than five seconds to run in a directory with more man 2 million files. One can adapt it to e.g. delete files that match a certain pattern.

find builtin in bash v4+
This command will give you the same list of files as "find /etc/ -name '*killall' | xargs ls -l". In a simpler format just do 'ls /etc/**/file'. It uses shell globbing, so it will also work with other commands, like "cp /etc/**/sshd sshd_backup".

create a backup for all directories from current dir
Create backup (.tar.gz) for all first-level directory from current dir.

pimp text output e.g. "Linux rocks!" to look nice

Get full from half remembered commands
Show all commands having the part known by you. Eg: $apropos pdf | less

Convert seconds to [DD:][HH:]MM:SS
Converts any number of seconds into days, hours, minutes and seconds. sec2dhms() { declare -i SS="$1" D=$(( SS / 86400 )) H=$(( SS % 86400 / 3600 )) M=$(( SS % 3600 / 60 )) S=$(( SS % 60 )) [ "$D" -gt 0 ] && echo -n "${D}:" [ "$H" -gt 0 ] && printf "%02g:" "$H" printf "%02g:%02g\n" "$M" "$S" }

Unix time to local time
Today

Load another file in vim
You can then switch from a file to another with ^W^W

Password Generation
Produces secure passwords that satisfy most rules for secure passwords and can be customized for correct output as needed. See "man pwgen" for details.


Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: