No announcement yet.

cut command

  • Filter
  • Time
  • Show
Clear All
new posts

  • cut command

    Recently I taught a 10-hour Linux webinar (basically digested first half of RHCSA) and as I researched some additional books I found cut command which is very useful.

    What cut does is selects only specified fields from text file where you tell it what the delimiter is.


    cut -f 1 -d : /etc/passwd

    This selects 1st field in file /etc/passwd (user list) using : as delimiter and outputs it.


    Our website is a mess as the Italians who can only copy paste and make stuff shiny but cannot code used some godawful visual composer on the wordpress and every page is full of UBB-like code with brackets on top of HTML. I wanted to find only list of country names in code containing 90.000 characters, so I wrote a bash script to find which columns delimited by = contain countries and then only output those fields to new file which is only 3000 characters long and has fields neatly arranged in lines, so I can just clear the bad text using dd or visual line in vi.

    Then I can plug the country list in google translate and write a sed or awk script that will instareplace country names without me manually scrubbing through 90.000 character mess of the page code.
    Last edited by UtwigMU; 26 June 2018, 06:44.

  • #2

    Yes... some things are a lost art... I put regular expressions under the same category.

    My girlfriend had to rename quite a number of attributes/methods in a Java code and change the signature (relating to a change in a MongoDB database, and she had to adjust her code for it), and it was for a class that had many instances and those methods were called frequently. I will say off the bat that am not so good at regular expressions, but I saw they could be useful in this case and it would be worth the time. It took me about 10 minutes to figure out the regular expression needed to make the replacement using Notepad++. She than had to use it multiple times, but each time with slightly different names in the regular expression. 10 minutes of searching to write the expression, 5-10 minutes of find/replace and done... whereas the alternative would be to manual replace everything, which would have taken whole day if not longer.
    Last edited by VJ; 26 June 2018, 13:37.
    Dream as if you'll live forever. Live as if you'll die tomorrow. (James Dean)


    • #3
      Wow a girlfriend who can program, you're lucky.


      • #4
        Dream as if you'll live forever. Live as if you'll die tomorrow. (James Dean)


        • #5
          sed and regex are closest realworld thing to magic. At work i have ~1600 directories following

          serial_some numbers in brackets or not Product name Type comment convention but not perfectly.

          I'm creating a website where people can enter their serial and download manuals, plans and software. I need to parse these folders so I wrote complex sed regex bash script that will parse folders mounted from Windows server and create database. It gets 97% results correctly whether there are underscores, spaces brackets
          Last edited by UtwigMU; 9 July 2018, 10:28.