Sunday, December 24, 2017

customizing any text file with sed

customizing any text file with sed


Well, this is really helpful, if you start to work with GNU/Linux for every purpose in your computer: could be end user, programming, deploying and administering your server, etc. sooner or later you will need to adjust,customize or update text based files with a lot of lines, in case like those ones, you may use commands like "sed" to achieve your purpose.

This post is not an intro to regular expressions, because a topic like that is really complex, this is just how to change/update a text file with a lot of lines using sed, I suggest you to get a good book about regular expressions or start to google about it, seriously, it is really helpful to know about it.

Cool, imagine that you have a File called mylist.txt with 1000 lines, like this:

File001=/ok/ok1/myfile1.txt
Title001="My first file"
File002=/ok/ok1/myfile2.txt
Title002="My second file"
....

1. How to delete all the lines starting with "Title"? here it is the command:
   sed /Title/d mylist.txt -> with this line youll see the changes just in the console, now, if you want to maket it permanently, just use this one:

  sed -i /Title/d mylist.txt

  Then, if you do a cat of mylist.txt, you will notice there is no lines starting with "Title".


2. How may I delete all characters in front of the symbol "="?:
   sed s/=[^=]*$// mylist.txt

3. How may I delete all characters behind of the symbol "="?
   sed s/.*=// mylist.txt

4. how may I add a string -/home/work/- at the beginning of all lines?
  sed s/^//home/work// mylist.txt

5. Find all numbers in the lines and remove them?
    sed -e s/[0-9:=]//g mylist.txt

Thats it, dont remember you can check if the results are the needed, then if you want to make the changes permanently, just add -i after sed and thats all.

Hope this may help you some day... at least when you need it.

Cheers

visit link download