Showing posts with label date. Show all posts
Showing posts with label date. Show all posts

Friday, May 5, 2017

6 often needed sed scripts for string processing

1. How to remove all empty lines in a file
  sed -e '/^$/d' filename.txt

2. How to remove the leading empty spaces
  (whitespace only) -> sed -e 's/^[ ]*//' /file/path/filename.txt
  (whitespace and tab \t) -> sed -e 's/^[ \t]*//' /file/path/filename.txt

3. How to add some string to starting of all lines of a file
  sed -e 's/^/string-to-add-at-starting/g' /file/path/filename.txt

4. How to add date to starting of all lines
  sed -e "s/^/$(date) /g" /file/path/filename.txt  - Note: The Double quotes.
  Before    ->    line in a file
  After    ->    Thu May  4 17:31:58 IST 2017 line in a file

5. How to add formatted date to starting of all lines
  sed -e "s/^/$(date '+%m\/%d\/%Y %H:%M:%S') /g" /file/path/filename.txt
  Example:
   Before    ->    line in a file
   After    ->    05/04/2017 17:30:50 line in a file

6. How to do multiple replace at one go
  sed -e 's/string-to-find1/string-to-replace1/g' -e 's/string-to-find2/string-to-replace2/g' /file/path/filename.txt

Some More scripts:
7. How to remove all contents after pattern match
  sed -e '/pattern/,$d' /path-to/file/filename.txt
  For Ex: To remove all contents after first empty line -> sed -e '/^$/,$d'


8. How to remove all contents before pattern match
  sed -e '0,/pattern/d' /path-to/file/filename.txt
  For Ex: To remove all contents before first empty line -> sed -e '0,/^$/d'

Thursday, June 2, 2011

How to remove continuous(unnecessary) occurance of a character?

In Debian Linux we have one superb command tool called 'tr' [Translate or delete characters]. This simple handy tool has may usages, here am going to explain how to remove sequence of repeated (same)character.

The date command will produce output as follows
Ex-1-scenario:
$date
Thu Jun 2 17:10:17 IST 2011
Note: There are 2 spaces between the Month(Jun) and the date(2) because the date is single digit.
Ex-2-scenario:
$date
Thu Jun 22 17:10:17 IST 2011

So, if i try to cut the date using cut command then with empty space( ) as delimiter. then the field count will change.
For example:
Ex-1-scenario:
In f1-Thu f2-Jun f3-( ) f4-2 f5-17:10:17 f6-IST f7-2011

Ex-2-scenario:
In f1-Thu f2-Jun f3-2 f4-17:10:17 f5-IST f6-2011

So, how can i split fields correctly regardless of the single or double digit dates. Here comes our tr tool. It will be done as follows
for both scenario-1, scenario-2
$date | tr -s ' '
Note: if you use tr -s 'y' it will remove the continues occarance of 'yyyyy' and put only one single 'y' there.
will output with single space( ) between fields. Happy scripting.. :)

For more information see the man page of tr, date commands. For online man pages of tr, date.