Results 1 to 4 of 4

Thread: Writing Phrase from file based on number (bash)

  1. #1
    Join Date
    Dec 2008
    Beans
    10

    Writing Phrase from file based on number (bash)

    Hello Guys,

    Sorry if this has been posted elsewhere I was just hoping you could help with me an problem I'm having with a bash script.

    Essentially I have a file with:

    XYZ (TAB) 45
    CRW (TAB) 1
    CLP (TAB) 5

    I want the script to write to another file XYZ because the number after the tab is over 30. The others the script should be ignore because they are below 30.

    How would I go about doing this? I'm struggling a bit with the syntax for it and was hoping to learn from the example. Sorry I realise this is a big ask.

    Thanks

    Lewis

  2. #2
    Join Date
    Jun 2016
    Beans
    2,831
    Distro
    Xubuntu

    Re: Writing Phrase from file based on number (bash)

    I would split the file contents into two arrays, then iterate through the array indices -
    Code:
    #!/bin/bash
    
    # This is the path to the file containing that data
    yourfile='./test.txt'
    
    # Parse the file into two arrays, one of the names (e.g. XYZ) and another of the numbers
    names=($(cat "$yourfile" | sed -r -e 's/\t.*$//g'))
    numbers=($(cat "$yourfile" | sed -r -e 's/^.*\t//g'))
    
    # Process the arrays by iterating through the indices
    # Note: this assumes the two arrays are the same length
    for i in $(seq 0 $((${#names[@]} - 1)));do
      if [[ "${numbers[$i]}" -gt 30 ]];then
        # do what you want with "${names[$i]}" here
      fi
    done
    Xubuntu 22.04, ArchLinux ♦ System76 hardware, virt-manager/KVM, VirtualBox
    When your questions are resolved to your satisfaction, please use Thread Tools > "Mark this thread as solved..."

  3. #3
    Join Date
    Dec 2008
    Beans
    10

    Re: Writing Phrase from file based on number (bash)

    Hello halogen2

    Many thanks for the quick reply I should have been more specific with my request my apologies,

    My file is actually more similar to:
    Here Is Some Text 30
    Hello World 10
    More Text 1

    I've edited the code you wrote adding in:

    Code:
    # Parse the file into two arrays, one of the names (e.g. XYZ) and another of the $
    names=($(cat "$yourfile" | sed -r -e 's/\t.*$//g'))
    numbers=($(cat "$yourfile" | sed -r -e 's/^.*\t//g'))
    
    # Process the arrays by iterating through the indices
    # Note: this assumes the two arrays are the same length
    for i in $(seq 0 $((${#names[@]} - 1)));do
      if [[ "${numbers[$i]}" -gt 30 ]];then
        # do what you want with "${names[$i]}" here
    echo "${names[$i]}" >> 2.tmp
    However the code writes to file like this:

    Code:
    Here
    is 
    some
    text
    hello
    world
    more text
    How would I edit the command to write to file:

    Code:
    Here Is Some Text    
    Hello World              
    More Text
    Many thanks once again for all your help

    Lewis

  4. #4
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Writing Phrase from file based on number (bash)

    If you have a column of space-separated text followed by a tab-delimited number, then the obvious way to process it would be using awk e.g.

    Code:
    awk -F '\t' '$2+0 > 30 {print $1}' file.txt

Bookmarks

Posting Permissions

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