Find the answer to your Linux question:
Results 1 to 5 of 5
hi, i have a file and within the file i have some text. I need to remove the following characters from the file. See my example below : cat file1.dat ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2011
    Posts
    14

    sed issue


    hi,

    i have a file and within the file i have some text. I need to remove the following characters from the file. See my example below :

    cat file1.dat
    Initialize.txt R1_P1000_ABCD_CAL_20101231_Config1C.tar

    and I would require the file to look like this (like to remove .tar) :

    cat file1.dat
    Initialize.txt R1_P1000_ABCD_CAL_20101231_Config1C

    How can I do this using the SED command ?

    thank you

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,382
    Code:
    sed 's/\.tar//g' file1.dat
    This will remove all ".tar".
    If the result is to your liking, add -i to the sed call and file1.dat will be changed in place.
    You must always face the curtain with a bow.

  3. #3
    Just Joined!
    Join Date
    Oct 2011
    Posts
    14
    That has worked ok, but if I add a tar command to the file1.dat, see below :

    cat file1.dat
    tar xvf R1_P1000_ABCD_CAL_20101231_Config1C.tar
    Initialize.txt R1_P1000_ABCD_CAL_20101231_Config1C.tar

    and I ran the sed command :

    sed 's/\.tar//g' file1.dat

    This removes the .tar from the 1st line, but I need to keep this line as the same.
    Only the second line needs the .tar removed.

    Please could you advise the best way for this.

    thank you

  4. #4
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,382
    To continue the way
    Code:
    sed '2s/\(Initialize\.txt.*\)\.tar/\1/' file1.dat
    But since you ask for the best way..
    The sed approach seems like fixing after the fact.

    Maybe that file1.dat can be created correctly in the first place?
    How is it generated?
    You must always face the curtain with a bow.

  5. #5
    Just Joined!
    Join Date
    Oct 2011
    Posts
    14
    thank you, I am now using your example within my script that i have created - see below :

    #!/bin/sh
    #set -x
    ################################################## ##############################
    #
    # Copyright ABC plc 2011
    #
    # Name: Verify_Install.sh
    #
    # Version: V0.1
    # Date: 12-10-11
    #
    # Purpose: Verify Install Process
    #
    ################################################## ##############################

    #############################################
    # Start Verify Intall Processes #
    #############################################

    Verify_Install()
    {
    SCRIPTS_DIR=$HOME/Control_Management/scripts
    BUILD_DIR=$RELEASE/$CURRENT_CONFIG/Config
    PROCESS_DIR=$RELEASE/Scripts/Batch
    PACKAGE_CONFIG1C_DIR=$OPS/Project/Release1/Config1C/Dynamic

    cd $PACKAGE_CONFIG1C_DIR

    tar xvf R1_P1000_ABCD_CAL_20101231_Config1C.tar

    cd $PROCESS_DIR
    echo "Jobstream Verify_Install Started"
    echo " "

    ./Initialize.ksh R1_P1000_ABCD_CAL_20101231_Config1C.tar 20101231

    echo " "
    echo "Jobstream Verify_Install completed"
    }

    now when i use your example, this still keeps the same format for both lines in my script.

    The line - tar xvf R1_P1000_ABCD_CAL_20101231_Config1C.tar needs to stay the same

    and the following line - ./Initialize.ksh R1_P1000_ABCD_CAL_20101231_Config1C.tar 20101231
    needs the .tar removed from this line only.

    kind regards

Posting Permissions

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