Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: difference between awk and sed scripting  (Read 18063 times)

0 Members and 1 Guest are viewing this topic.

surekha

    Topic Starter


    Greenhorn

    difference between awk and sed scripting
    « on: November 02, 2008, 12:20:29 AM »
    the main difference is that in sed script he script is made to excute once,
     but not in awk
    but i am  not clear with it .

    can any one help me out ...........
    « Last Edit: November 02, 2008, 02:49:30 AM by surekha »
    Love all        Trust few         Follow one

    ghostdog74



      Specialist

      Thanked: 27
      Re: difference between awk and sed scripting
      « Reply #1 on: November 02, 2008, 01:19:47 AM »
      the main difference is that the script is made to excute once in sed but not in awk
      but i am clear with it .

      can any one help me out ...........


      you have to make your question clear. Both are tools for processing text. awk is a programming language whose main purpose is processing text BUT it can be used as a general purpose programming language as well.

      surekha

        Topic Starter


        Greenhorn

        Re: difference between awk and sed scripting
        « Reply #2 on: November 02, 2008, 07:59:04 PM »
        yes both can be used  as programming language
        but i dont know whats the difference ,since both are used to search pattern
        Love all        Trust few         Follow one

        Caleneledh

        • Guest
        Re: difference between awk and sed scripting
        « Reply #3 on: March 11, 2010, 02:03:21 PM »
        Although sed and awk are similar they do have different purposes. 

        Sed is a stream editor mainly used to change change a field.  For example:

        sed -e 's/oldstuff/newstuff/g' inputFileName > outputFileName

        would change all occurences of oldstuff to newstuff in inputFileName, and redirect the output to outputFileName.

        Awk is more usualy used to format fields.  For example:

        awk '/regex/ { print x }; { x=$0 }'

        This would search for occurences of /regex/ and print the line FOLLOWING it.


        This is not hard and fast though as there is some overlap between the two.

        Geek-9pm


          Mastermind
        • Geek After Dark
        • Thanked: 1026
          • Gekk9pm bnlog
        • Certifications: List
        • Computer: Specs
        • Experience: Expert
        • OS: Windows 10
        Re: difference between awk and sed scripting
        « Reply #4 on: March 11, 2010, 02:18:29 PM »
        Helo, Caleneledh and wel come to the CH forum.

        This may be AWK ward, but no a criticisms of what yu just  SED.

        Most likely ghostdog74  will not respond to your answer. Its been a year from the time he made the post. But maybe he will drop it and see it.

        But anyway, I enjoyed your answer. Now  I know the difference.

        Here is a reference:
        http://www.faqs.org/docs/abs/HTML/sedawk.html
        I put that here so the search engine will have some information for the keywords AWK and SED and what is the difference between AWK and SED.

        ghostdog74



          Specialist

          Thanked: 27
          Re: difference between awk and sed scripting
          « Reply #5 on: March 24, 2010, 11:40:46 PM »
          Although sed and awk are similar they do have different purposes. 

          Sed is a stream editor mainly used to change change a field.  For example:

          sed -e 's/oldstuff/newstuff/g' inputFileName > outputFileName

          would change all occurences of oldstuff to newstuff in inputFileName, and redirect the output to outputFileName.

          Awk is more usualy used to format fields.  For example:

          awk '/regex/ { print x }; { x=$0 }'

          This would search for occurences of /regex/ and print the line FOLLOWING it.


          This is not hard and fast though as there is some overlap between the two.

          sadly, not true. sed is a stream editor to manipulate and change text. All it ever does is using regular expression. Plus some buffers to hold data. that's about it. It lacks abilities to count (at least easily), or lacks arrays, and other programming constructs. While, awk on the other hand, does the job of sed more easily and more intuitively. it can also replace text as easily as using sed. In the end, they actually do not have different purposes in terms of text/file processing.
          Code: [Select]
          sed 's/old/new' file

          same as
          Code: [Select]
          awk '{sub("old","new")}1' file

          Now, take a csv file that is comma separated and say i want to find the value of the 3rd field. Its more easily done with awk than with sed. eg

          awk
          Code: [Select]
          awk -F"," '{print $3}' csvfile
          sed
          Code: [Select]
          sed 's/\(.[^,]*\),\(.[^,]*\),\(.[^,]*\)\(.*\)/\3/' csvfile

          You can learn both sed and awk, but frankly, for all your text/file processing + programming needs, just learn awk will do.
          « Last Edit: March 25, 2010, 12:16:02 AM by ghostdog74 »

          ghostdog74



            Specialist

            Thanked: 27
            Re: difference between awk and sed scripting
            « Reply #6 on: March 24, 2010, 11:42:18 PM »
            Most likely ghostdog74  will not respond to your answer. Its been a year from the time he made the post. But maybe he will drop it and see it.
            well, i seldom click "New replies to your posts", but if i do, i would reply as necessary :)