Topic
5 replies Latest Post - ‏2009-01-05T17:28:52Z by senhu
SystemAdmin
SystemAdmin
232 Posts
ACCEPTED ANSWER

Pinned topic Program an script ( bash )

‏2008-03-11T02:09:09Z |
Hi All,

I need to do a script in bash programming language. My problem is described bellow:

I have one log file and i need to read it taking some fields separated by comma and save them in a new file.
Example:
file1:
00,343434,test,232323,3434545,454657,656dsd
02,34sd434,test,23323,344545,45d657,sdsf6dsd
.....
.....

So, how can i take, for example, the first and third fields of the each line and save them in the new file.

Thanks regards,

Message was edited by: FilipeMotta
Updated on 2009-01-05T17:28:52Z at 2009-01-05T17:28:52Z by senhu
  • SystemAdmin
    SystemAdmin
    232 Posts
    ACCEPTED ANSWER

    Re: Program an script ( bash )

    ‏2008-03-11T13:27:57Z  in response to SystemAdmin
    Hi,

    I did through cut command, but i yet get the wrong information. My original file is described bellow:

    0,11,03,2008,08,01,31,Start,test@mail.com,2332,Async,00,08738639088,,,,
    0,11,03,2008,08,01,33,Start,test@mail.com,20219,Async,030,8137075056,,,,
    0,11,03,2008,08,01,42,Start,test2@mail.com,20219,Async,030,8137075056,,,,

    I want filter the date, the action and the email, so i put the follow command:
    cut -d, -f2,3,4,8,9 file

    Ok, the file was filter, but i want the unique result of the email. I tried to use the uniq command but i'm not get the correct result. I tried this command line:
    cut -d, -f2,3,4,8,9 file | uniq -f 8
    but the result retrieve only one line when it should give two lines. If someone knows how unique result of the email, please, answer it...

    Thanks

    Message was edited by: FilipeMotta
    Updated on 2008-03-11T13:27:57Z at 2008-03-11T13:27:57Z by SystemAdmin
    • SystemAdmin
      SystemAdmin
      232 Posts
      ACCEPTED ANSWER

      Re: Program an script ( bash )

      ‏2008-03-12T16:53:30Z  in response to SystemAdmin
      Hi,

      Before using the uniq command,you have to sort the inputs.So try this one

      cut -d, -f2,3,4,8,9 file2.txt | sort -t "," +4 | uniq

      -t - for specifying the delimiter
      +4 - for sorting based on the fifth field from the input of cut command ie,email
      With Regards
      Dileep Pattayath
  • SystemAdmin
    SystemAdmin
    232 Posts
    ACCEPTED ANSWER

    Re: Program an script ( bash )

    ‏2008-03-12T12:29:19Z  in response to SystemAdmin
    U CAN use This :
    cat file_name | awk '{print $1 " " $2 }' > newfile
  • senhu
    senhu
    1 Post
    ACCEPTED ANSWER

    script in biterscripting

    ‏2009-01-05T17:28:52Z  in response to SystemAdmin
    You can use the following commands in biterscripting (http://www.biterscripting.com) for more control and more parsability on input lines.

    wex "1" # Extract 1st field
    wex -e "3" # Extract 3rd field even if it is empty (-e)
    wex $n # Extract n'th field (n is an int variable)
    lex "1" # Extract first line
    lex "l" # Extract last line (in double quotes is the letter l=Ell
    lex $k # Extract k'th line
    len # Get number of lines
    wen # Get number of fields

    l=line, w=word
    ex=extractor,en=enumerator/counter

    Sen
    (I am a scripting trainer.)