Share this on:

39 comment

  1. Thanks, I used this code and edited it. It’s the closest I’ve been to the output I need but for some reason using this to export each line as an individual variable isn’t working.

    Here is the script, my guess is that it’s a syntax issue of some sort. Thanks in advance for anyone who offers help!

    1. Can you tell me what are you trying to create here? If possible please use our forum for further discussion.

  2. Hi,
    I have 2 CSV files. let say emp_data_Jan.csv (will contain the data of employees till Jan) and emp_data_feb.csv (will contain the data of employees till Feb). I need a Shell script which can read both these files and compare the data between emp_data_Jan.csv and emp_data_feb.csv. After comparision i need the output data in new CSV file. Output data will be records which are present in emp_data_Feb.csv and not in emp_data_Jan.csv. Can any one help me out in this.

    Thanks in Advance


  3. Want to know how to read word by word for each line , from the file . what could be the seperator value for IFS

  4. Hi All,
    Here is another way-

    ## This will read a file line by line
    x=`wc -l $1 |awk '{print $1}'`
    echo "No of lines are: $x \n"
    while [ $i -le $x ]
            echo "Line Number: $i -->\n"
            echo  "`head -$i  $1 | tail -1` \n"
            i=`expr $i + 1`
  5. I was having a real mental block on the best way to read files in shell scripts and I found this site and example script. It was a real help. I did have the following thoughts though:

    I don’t think that it is really necessary to mess with IFS nor with saving and restoring FD 0. I have found that the following adaptation seems to do the job. (With one alteration: not defaulting the input file to stdin)

    # Make sure we get file name as command line argument
    FILE=${1?"No file name specified"}
    # Check that file exists and is readable
    [ ! -f $FILE ] && { echo "$FILE: does not exist"; exit 1; }
    [ ! -r $FILE ] && { echo "$FILE: cannot be read"; exit 2; }
    #Open file for reading
    exec 3<$FILE
    #Process file line by line
    while read -u 3 line
       # use $line variable to process line in processLine() function
       processLine $line
    # Close file after reading
    exec 3<&-
    exit 0

    YMMV. Hope this helps someone else.

  6. Thanks a lot. It works fine. when i read a text from a file and create a new file using that text two small letters disappearing from the text and that leads to an invalid file path . two letters are “e”and “n”. while reading from the file it fails to read these two letters . could you please tell me possible reason for this . is it taking those letters as some delimiter or something ?

    Pre requesties

    1)template.txt in the same path contains a text “ordprg”and a blank line

    2) in the same path contains two lines
    Following program will eliminate “e” and “n”

    #My Shell script is 
    line="$@" # get all args
    # F1=$(echo $line | awk '{ print $1 }')
    #echo $line
    #touch $line".sh"
    #echo $line &gt;&gt; $line".sh"
    #echo "date_stamp="'`'"date +%Y%m%d.%H%M%S"'`'" # used to date stamp all files for this run" &gt;&gt; $line".sh"
    #echo $fname
    #pname ="cp /home/jtest/test.txt /home/rms_user/jtest/$fname
    #cp /home/jtest/test.txt $fname
    #echo fname
    ### Main script stars here ###
    # Store file name
    # Make sure we get file name as command line argument
    # Else read it from standard input device
    if [ "$1" == "" ]; then
    #echo "k.txt"
    #echo "1k.txt"
    # make sure file exist and readable
    if [ ! -f $FILE ]; then
    #echo "2k.txt"
    echo "$FILE : does not exists"
    exit 1
    elif [ ! -r $FILE ]; then
    #echo "3k.txt"
    echo "$FILE: can not read"
    exit 2
    # read $FILE using the file descriptors
    # Set loop separator to end of line
    echo $IFS
    IFS=$(echo -en "\n\b")
    exec 3&lt;&amp;0
    exec 0 temp_file
    mv temp_file $line".sh"
    #echo "#filename" $line".sh" &gt;&gt; $line".sh"
    exec 0&lt;&amp;3
    # restore $IFS which was used to determine what the field separators are
    exit 0

    # Please Respond to this Thanks in Advance

  7. @michelek,

    This can be easily fixed by adding following code before and after while loop:

    IFS=$(echo -en "\n\b")
    # add your while loop here

    while / for / untile loop is using $IFS to determine what the field separators are. I’ve modified this to use end of line. Here is final modified code:

    IFS=$(echo -en "\n\b")
    exec 3<&0
    exec 0<$FILE
    while read line
    	# use $line variable to process line in processLine() function
    	processLine $line
    exec 0<&3

    The script has been updated to fix this issue. Just download updated version.


  8. Hey this worked perfectly. I used it to automatically patch my sun system. I used smpatch analyze > patch.txt then used your script to read in the outputed patch numbers.

    Have a question? Post it on our forum!