Find the answer to your Linux question:
Results 1 to 9 of 9
Hi all, I got two files say, file1.txt and file2.txt file1.txt content (two fields) Code: page=node_1_0_1 category page=node_1_0_2 brand file2.txt content (only one field) Code: page=node_1_0_1 page=node_1_0_2 Now i want ...
  1. #1
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Question Help on shell script

    Hi all,
    I got two files say,
    file1.txt and
    file2.txt

    file1.txt content (two fields)
    Code:
    page=node_1_0_1  category
    page=node_1_0_2  brand
    file2.txt content (only one field)
    Code:
    page=node_1_0_1
    page=node_1_0_2
    Now i want to replace file2.txt with the second field of file1.txt

    I need output like,
    Code:
    page=category
    page=brand
    How to do this??Any ideas?
    Thanks
    - 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 Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Something like this:

    Code:
    awk 'FNR==NR{arr[$1]=$1;next} arr[$1]==$1{print}' "file2" "file1"
    Regards

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

    Exclamation

    Hi thanks for ur reply,but i'm not getting expected results.

    Code:
    [oss@pc021698 JAVA]$ awk 'FNR==NR{arr[$1]=$1;next} arr[$1]==$1{print}' "file2.txt" "file1.txt"
    page=node_1_0_1 category
    page=node_1_0_2 brand
    
    [oss@pc021698 JAVA]$ cat file1.txt
    page=node_1_0_1 category
    page=node_1_0_2 brand
    [oss@pc021698 JAVA]$ cat file2.txt
    page=node_1_0_1
    page=node_1_0_2
    - 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 Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    I think I misread something, to print only lines of file2 if the line exists in file1 try this:

    Code:
    awk '
    FNR==NR{arr[$1]=$1;next} 
    arr[$1]==$1 && $2{split($1,s"=");{print s[1],$2}
    ' file2 file1
    Regards

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

    Question

    sorry again ,
    it shows errors,unexpected new line or end of string
    Code:
    awk: cmd. line:4: arr[$1]==$1 && $2{split($1,s"=");{print s[1],$2}
    awk: cmd. line:4:                                                 ^ unexpected newline or end of string
    - 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
    -------------------

  6. #6
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    I apologise, a typo, remove the { before the print statement:

    Code:
    awk '
    FNR==NR{arr[$1]=$1;next} 
    arr[$1]==$1 && $2{split($1,s,"=");print s[1],$2}
    ' file2 file1
    Regards
    Last edited by Franklin52; 12-24-2007 at 12:10 PM. Reason: typo

  7. #7
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568
    Sorry again ...still not solved....some error in split cmd
    Code:
    awk: cmd. line:3: (FILENAME=file1 FNR=1) fatal: split: second argument is not an array
    - 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
    -------------------

  8. #8
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Sorry again, I'm just awaked, it must be:

    Code:
    split($1,s,"=")
    Regards

  9. #9
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Smile

    it works now
    Thanks Franklin .. Thanks for your patience
    - 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
  •  
...