Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2009
    Location
    /usr/bin
    Posts
    217

    help with creating a bash script

    Hello friends

    I'm trying to write a bash script to match 2 files and generate output


    #1 file has

    some_text 1
    some_textx 2
    some_texta 3
    some_texte 4
    #2 file has
    1 aa
    2 bb
    3 cc
    4 dd
    What I want, is to compare 2 files, and replace 1,2,3,4 in first file with aa,bb,cc,dd of second file, so that output should be
    some_text aa
    some_textx bb
    some_texta cc
    some_texte dd
    Can some one please help in writing script for this?


    Cheerz

  2. #2
    Join Date
    Feb 2007
    Location
    Florida
    Posts
    1,932
    This should work for you assuming the format for file one will always be <TEXT> <NUMBER> and file two will always be <NUMBER> <TEXT>:

    Code:
    #!/bin/sh
    
    FILE1=/path/to/file1
    FILE2=/path/to/file2
    NUM=`cat $FILE1 | wc -l`
    COUNT=1
    while [ $COUNT -le $NUM ]; do
            ONE=`cat $FILE1 | grep " $COUNT$" | /usr/bin/awk '{print $1}'`
            TWO=`cat $FILE2 | grep "^$COUNT " | /usr/bin/awk '{print $2}'`
            echo $ONE $TWO
            COUNT=$((COUNT + 1))
    done
    I added more lines to my files to make sure it worked correctly with multiple digits and here's the output:
    Code:
    # sh test.sh
    some_text aa
    some_textx bb
    some_texta cc
    some_texte dd
    some_texteb ee
    some_textec ff
    some_texted gg
    some_textee hh
    some_textef ii
    some_texteg jj
    some_texteh kk
    some_textei ll
    some_textej mm
    some_textek nn
    some_textel oo
    some_textem pp
    some_texten qq
    some_texteo rr
    some_textep ss
    some_texteq tt
    some_texter uu
    some_textes vv
    -Joe @ Secure Dragon LLC.
    + OpenVZ Powered by Wyvern | KVM | cPanel Hosting | Backup VPSs | LowEndBoxes | DDOS Protection
    + Florida | Colorado | Illinois | California | Oregon | Georgia | New Jersey | Arizona | Texas

  3. #3
    Well, You should learn C programming basic first then you can switch back to any others languages.

Similar Threads

  1. [ Help ] Creating a bash script to install softwares?
    By LFern in forum Programming Discussion
    Replies: 1
    Last Post: 07-23-2011, 04:51 PM
  2. Help with creating a PHP script
    By acctman in forum Programming Discussion
    Replies: 4
    Last Post: 06-26-2006, 11:06 PM
  3. Help with small sh/cron script
    By ReliableServers in forum Hosting Security and Technology
    Replies: 3
    Last Post: 10-26-2001, 06:59 PM
  4. Help with small sh/cron script
    By ReliableServers in forum Dedicated Server
    Replies: 1
    Last Post: 10-25-2001, 05:02 AM
  5. I need some help with creating...
    By MySiteHost in forum Hosting Software and Control Panels
    Replies: 5
    Last Post: 12-19-2000, 09:17 PM

Posting Permissions

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