Learning Linux Commands: sed - LinuxConfig.org

Welcome to the second part of our series, a part that will focus on sed, the GNU version. As you will see, there are several variants of sed, which is available for quite a few platforms, but we will focus on GNU sed versions 4.x. Many of you have already heard about sed and already used it, mainly as a substitution tool. But that is just a segment of what sed can do, and we will do our best to show you as much as possible of what you can do with it.
This is a companion discussion topic for the original entry at https://linuxconfig.org/learning-linux-commands-sed


Hi we can i change “” to “/” via sed

dushyant bhagwani -> kde

using sed

sed 's/\\/\//g' filename

using tr

cat Filename | tr '\' '/'


I need to to delete all files from a folder with below mentioned condition. Any suggestion would be helpful.
File Name: fileNameBeginDateEndDate.txt (begin and end date in YYYYMMDD format)
All files in which current date is not in between begin date and end date need to be deleted.
File that need to be deleted:File2016010120170101.txt
File that should not be deleted: File2016010120190101.txt

Thanks in Advance!


I need to replace every second column by third column and vice versa in every line in txt file and print it to stdout. And every column is separated by ‘\t’
For example…

TXT file:

  1. line: first second third fourth
  2. line: abc car xyz 123

first third second fourth
abc xyz car 123

Thanks for any advice how to do that :slight_smile:

Lubos Rendek Mod -> cp1116

If you are not looking for a sed specific solution, try awk:

$ cat file.txt
1 2 3 4
1 2 3 4
$ awk ‘{t = $2; $2 = $3; $3 = t; print; }’ file.txt
1 3 2 4
1 3 2 4

Hope this helps…

cp1116 -> Lubos Rendek

Wau… so simple… it works :slight_smile: Thank you very much my friend!

I have one more problem… How to take all characters from txt file, then sort them and print every character only once? I have written this (below). But problem is that It needs sed probably, which I do not know ho to use, as last step.

egrep -o “.” $DATA1 | sort | tr -d ‘\n’ | sed [something]

For example…

TXT file:

  1. line: ABCCXYYF
  2. line: abdrrr233lmnoo

my wrong OUTPUT:

correct OUTPUT should be:

If you can, please help with this as well… Thanks

Lubos Rendek Mod -> cp1116

I think what you are looking for is uniq command. This should be easy as:

$ cat file.txt

$ grep -o . file.txt | sort -n | uniq | tr -d ‘\n’; echo

Hope this helps…!


cp1116 -> Lubos Rendek

Works like charm! Thank you :slight_smile:

But one more thing… in first problem I have posted. I need ‘\t’ beetwen every column in the output as well… not just blank space but ‘\t’ . How to do that?

awk ‘{ t=$2"\t"; $2="\t"$3; $3="\t"t;print; }’ This solves it but only partially.

OUTPUT should be:
first [’\t’] third [’\t’] second [’\t’] fourth [’\t’] etc… [’\t’] last


Lubos Rendek Mod -> cp1116

$ awk ‘{t = $2; $2 = $3; $3 = t; print; }’ file.txt | sed ‘s/ /\t/g’

cp1116 -> Lubos Rendek

Thank you! :slight_smile: