Results 1 to 5 of 5

Thread: cut command

  1. #1
    Super MURCer UtwigMU's Avatar
    Join Date
    Jul 2002
    Location
    Sin City
    Posts
    4,964

    Default 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.

    Example:

    cut -f 1 -d : /etc/passwd

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

    Results:
    root
    bin
    daemon
    user
    alice

    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; 26th June 2018 at 07:44.

  2. #2
    Moderator VJ's Avatar
    Join Date
    Mar 2001
    Location
    Belgium/Poland/Spain
    Posts
    9,395

    Default


    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; 26th June 2018 at 14:37.
    pixar
    Dream as if you'll live forever. Live as if you'll die tomorrow. (James Dean)

  3. #3
    Super MURCer UtwigMU's Avatar
    Join Date
    Jul 2002
    Location
    Sin City
    Posts
    4,964

    Default

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

  4. #4
    Moderator VJ's Avatar
    Join Date
    Mar 2001
    Location
    Belgium/Poland/Spain
    Posts
    9,395

    Default

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

  5. #5
    Super MURCer UtwigMU's Avatar
    Join Date
    Jul 2002
    Location
    Sin City
    Posts
    4,964

    Default

    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; 9th July 2018 at 11:28.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •