Find the answer to your Linux question:
Results 1 to 5 of 5
Hi all, I'm writing shell script on my own I'm at the last step in the script i didn't seem to get it right Here is problem: I have 2 ...
  1. #1
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Smile How to get this?

    Hi all,
    I'm writing shell script on my own
    I'm at the last step in the script i didn't seem to get it right

    Here is problem:
    I have 2 files.

    file1.txt
    Code:
    AAA
    BBB
    CCC
    EE
    file2.txt
    Code:
    AAA34
    BBB66
    CCC
    DDD
    EE&something
    FF
    GG
    I need output file like,
    Code:
    AAA34
    BBB66
    CCC
    EE&something
    How to do this?
    I thought about diff and comm ...but doesn't seems to work for me.
    Any thoughts ?
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    It's not clear what you're trying to do here.

    The output file looks just like the the first, second, third, and fifth lines of the second input file, with the first input file ignored completely.

    In other words, I don't think we can guess your intention by just looking at the input and desired output. You'll have to tell us more completely what you want.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568
    Ok..this is what i want :
    I want to grep lines from file2 which begins with the content of file1.

    Example:
    I got a file (say file1) which will have few words like,
    Code:
    This 
    That
    These
    line
    And another file (file2.txt) has the following contents:
    [code]
    This is line1.
    That is line2 .
    These are line3 and 4.
    And finally i have
    Dont Get this line
    [\code]

    I want to grep only those lines from file2 which begins with the contents from file1.

    the desired output :
    Code:
    This is line1.
    That is line2 .
    These are line3 and 4.
    And these two lines are omiited
    Code:
    And finally i have
    Dont Get this line.
    because
    Code:
    "And finally i have"
    we don't have start wording in file1.
    Code:
    "Dont Get this line"
    Though the word "line" is available in file1.
    Since this sentence begins with the word "Dont" we skip this too.

    Any ideas?
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  4. #4
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Code:
    awk 'FNR==NR{
     a[$0]=$0
     next
    }
    { 
     for ( items in a ) {
      if ( items ~ $0 ) {
        print items
      }
     }
    }' file2 file1
    output:
    Code:
    # ./test.sh
    AAA34
    BBB66
    CCC
    EE&something

  5. #5
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568
    i found a solution:

    I needed grep with ^ option

    Code:
    	#----- The file to read
            myFile="file1.txt"
            myLine=""
    
    i="1"
     while [ 1 ]
            do
                    read myLine || break
    		F1=$(echo $myLine | awk '{ print $1 }')
    
    		grep '^'$F1 file2.txt >> file3.txt
    
      done < $myFile
    exit;
    so simple
    Though i love C cprogramming,shell script is very useful and powerful
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

Posting Permissions

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