Find the answer to your Linux question:
Results 1 to 4 of 4
I have been trying to figure out how to insert characters with AWK. I am feeding AWK with a very large array. awk -F " " "{print $0}" Using the ...
  1. #1
    Just Joined!
    Join Date
    Jun 2011
    Posts
    2

    AWK insert characters

    I have been trying to figure out how to insert characters with AWK. I am feeding AWK with a very large array.

    awk -F " " "{print $0}"

    Using the above I get something like:
    cat dog bird cat dog bird cat dog bird

    I would like the output to be:
    ..cat..dog..bird..cat..dog..bird..cat..dog..bird..

    I know that I could use awk -F " " "{print ".."$0".."}"
    to insert some things, but it doesn't give me periods *between* columns as well as at the beginning and end.
    I have also tried the OFS option, but it doesn't seem to work!

    A newb in need.... lol

  2. #2
    Just Joined!
    Join Date
    Jun 2007
    Posts
    16

    sounds like homework...

    cat /tmp/xxx |awk '{gsub(/^/,"..");print}' |awk '{gsub(/ /,"..");print}' |awk '{gsub(/$/, "..");print}' > /tmp/xxxx

  3. #3
    Just Joined!
    Join Date
    Jan 2011
    Location
    Fairfax, Virginia, USA
    Posts
    94
    Hi, I think sed fits your problem nicer with something like this:
    Code:
    [brian@bmicek ~]$ echo "cat  dog    bat" | sed 's/ /./g'
    cat..dog....bat
    If you want to use AWK, OFS will work but in your example you have to keep in mind that it sort of represents the "," in a print command ... this semi broken code shows what I mean:
    Code:
    [brian@bmicek ~]$ echo "cat  dog    bat" | awk -v 'OFS=.' '{  print $1,$2 }'
    cat.dog
    If you wanted, you could iterate from $1 to $NF and do something clever, but I think this is what you want:

    Code:
    [brian@bmicek ~]$ echo "cat  dog    bat" | awk '{  gsub( " ","." ); print }'
    cat..dog....bat
    One thing to know is that these examples were not done on Ubuntu. Ubuntu decided to replace gawk with another version of AWK that does not support the gawk extensions. I'm unclear if this is a gawk extension or not. If you wanted, you could install gawk (which is better frankly) on Ubuntu. Here is how you can tell:
    Code:
    [brian@bmicek ~]$ ls -l `which awk`
    lrwxrwxrwx 1 root root 14 Nov 22  2010 /usr/bin/awk -> ../../bin/gawk

  4. #4
    Just Joined!
    Join Date
    Jun 2011
    Posts
    2
    Thanks for the fast responses!
    I found yet another alternative worked good too.
    Because I was working with an array with a variable size, it was twice as difficult due to my limited understanding...

    this seemed to do the trick. I used this to write a script for grep, its was good practice and now i can search a bit faster.

    parsed_long_search=`echo ${search_array[*]} | awk 'BEGIN { OFS=".....\\\|....." } { $1=$1; print "...."$0"...." ; }'`

    Code:
    #!/bin/bash
    clear
    echo Kiah07 2011 
    
    ##Take multiple search strings from a user and feed them into grep to find file matches
    ##regardless of the text or lack thereof surrounding the specified strings. Just a plaything,
    ##
    
    txt=$(tput setaf 3)
    txt2=$(tput setaf 1)
    trst=$(tput sgr0)
    
    exit_timer=0
    cycle=0
    declare -a search_array
    
    
    
    main() {
    while [ $exit_timer == "0" ]
        do
           let "cycle +=1"
           echo "Please type a string to search for>"   
           echo -n ${txt}"Item $cycle:  ${trst}"  & read  search_array[$cycle]  
                 echo 
                 echo -n "Search for an additional string?  (y/n)"" "     
                 read decision
                 echo
                     if [ "$decision" == "n" ]; 
                       then  let "exit_timer =1"
                     fi
        done
    parse
    }
    
    
    
    parse() {
    parsed_long_search=`echo ${search_array[*]} | awk 'BEGIN { OFS=".....\\\|....." } { $1=$1; print "...."$0"...." ; }'`  
    parsed_short_search=` echo ${search_array[*]} | awk 'BEGIN { OFS="\\\|" } { $1=$1; print ; }'`
    get_directory
    }
    
    
    
    get_directory() {
    echo  "What directory should be searched?  **Default is current directory"
    read searchdirectory
    echo "About to search for:"  $txt${search_array[*]}$trst;
    confirm
    }
    
    
    
    confirm() {
    echo -n "Proceed?  (y/n)"" "
     read confirm_answer 
        if [ "$confirm_answer" == "y" ];
        then search
        else newsearch
        fi
    }
    
    
    
    newsearch(){
    echo -n "${txt2}**Search will not proceed**${trst}  Perform a new search?  (y/n)"" "
             read newsearch_choice
             let "exit_timer = 0"
             if [ "$newsearch_choice" == "y" ];
             then 
                     echo -n "Clear previous search terms?  (y/n)"" "
                     read clear_decision
                     if [ "$clear_decision" == "y" ];
                     then  let "cycle = 0"
                           unset search_array
                           echo "Previous search terms cleared"
                     fi
                     main          
             else exit 
             fi
    }
    
    
    
    search() {
    find $searchdirectory -type f -print0 | xargs --null grep --color=always -ao "$parsed_short_search\|$parsed_long_search" ./ ; 
    #find $searchdirectory -type f -print0 | xargs --null grep --color=always -ao "$first_string\|........$first_string........\|$second_string\|........$second_string........." ./ ; 
    #find $searchdirectory -type f -exec grep --color=always -ao "$first_string\|........$first_string........\|$second_string\|........$second_string........" ./ {} \;
    }
    
    
    main
    
    echo "Searching Completed."

Posting Permissions

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