File content manipulation (update (word)/remove(word)/delete line) by sed command in Unix

File content manipulation (update (word)/remove(word)/delete line) by sed command in Unix

Sample manual, just type the sed and press enter or man sed:
sed
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

  -n, --quiet, --silent
                 suppress automatic printing of pattern space
  -e script, --expression=script
                 add the script to the commands to be executed
  -f script-file, --file=script-file
                 add the contents of script-file to the commands to be executed
  --follow-symlinks
                 follow symlinks when processing in place; hard links
                 will still be broken.
  -i[SUFFIX], --in-place[=SUFFIX]
                 edit files in place (makes backup if extension supplied).
                 The default operation mode is to break symbolic and hard links.
                 This can be changed with --follow-symlinks and --copy.
  -c, --copy
                 use copy instead of rename when shuffling files in -i mode.
                 While this will avoid breaking links (symbolic or hard), the
                 resulting editing operation is not atomic.  This is rarely
                 the desired mode; --follow-symlinks is usually enough, and
                 it is both faster and more secure.
  -l N, --line-length=N
                 specify the desired line-wrap length for the `l' command
  --posix
                 disable all GNU extensions.
  -r, --regexp-extended
                 use extended regular expressions in the script.
  -s, --separate
                 consider files as separate rather than as a single continuous
                 long stream.
  -u, --unbuffered
                 load minimal amounts of data from the input files and flush
                 the output buffers more often
      --help     display this help and exit
      --version  output version information and exit

If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret.  All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.

GNU sed home page: <http://www.gnu.org/software/sed/>.
General help using GNU software: <http://www.gnu.org/gethelp/>.

How to use sed command for extracting the URL’s:

Just extract the URL’s from access.log: We’ve one file called access.log with below mention data/line.

access.log:
Hello tutorialbyexample, home page access by GET http://www.tutorialbyexample.com/success HTTP 200
This is the success page of five success by POST http://www.tutorialbyexample.com/2015/09/five-5-success-factor.htmlHTTP 200
Hello tutorialbyexample, home page access by GET http://www.tutorialbyexample.com/success HTTP 200
This is the success page of five success by POST http://www.tutorialbyexample.com/2015/09/five-5-success-factor.htmlHTTP 200
Hello tutorialbyexample, home page access by GET http://www.tutorialbyexample.com/
This is the success page of five success by POST http://www.tutorialbyexample.com/2015/09/five-5-success-factor.html
Hello tutorialbyexample, home page access by GET http://www.tutorialbyexample.com/

Output:

Command:
cat access.log | sed 's/.*GET //' | sed 's/.*POST //' | sed 's/success HTTP 200//' | sed 's/ HTTP 200//' > access_200.log

Explanations:
cat access.log:For reading the file.
sed 's/.*GET //' search the data from beginning till GET and replace with nothing.
sed 's/.*POST //'search the data from beginning till GET and replace with nothing.
sed 's/success HTTP 200//' search the data starting from success HTTP 200 and replace with nothing.
sed 's/ HTTP 200//'search the data starting from HTTP 200 and replace with nothing.
> access_200.log will redirect into new file.
| has been used for executing the multiple command in single.


Ref from Unix.

No comments:

Post a Comment