Shell Script to convert the file

Posted on 2014-07-11
Last Modified: 2014-07-11
Hello Experts,

I have one file as like below on my unix box as like below:


Which contains "tab" which I want to replace with ";" symbol.

After conversion the file should look like below:

Final output should look like below:
Question by:Swadhin Ray
    LVL 68

    Expert Comment

    If your shell is "bash":

    sed 's/       \{1,\}/;/g' inputfile

    Create the "tab" between the first "/" and  "\{" by hitting <Ctrl><V> first, then <TAB>
    (press and hold the Ctrl key and type "v", then release the Ctrl key and press the TAB key).

    The above will write out the result to your terminal. To create an output file run:

    sed 's/       \{1,\}/;/g' inputfile > outputfile

    If your "sed" suports it you can change the inputfile "in place" and crate a backup file "inputfile.bak" in one go by specifying the "-i.bak" option:

    sed -i.bak 's/       \{1,\}/;/g' inputfile

    Under ksh create the "tab" between the first "/" and  "\{" by first typing "\" then TAB. Attention: The backslash will vanish when TAB is pressed. That's desired and OK.

    If you're using a different shell, please let me know.
    LVL 16

    Author Comment

    by:Swadhin Ray
    how I can put them all into one "" file.

    where I need to run something like :

    which should take the " Sample-file.txt " autometically and convert the file and save as like :
    LVL 68

    Accepted Solution

    Create a file "" like this:

    sed 's/       \{1,\}/;/g'  Sample-file.txt > Sample-file-converted.txt

    Take care to specify the correct paths to the concerned files.
    The same rules as described above for specifying the "TAB" characters can be applied, but since you're using an editor to create the script file  you can also simply press the TAB key.

    Once you saved the above as "" run:

    chmod +x

    to set the "executable" bit, then run

    LVL 16

    Author Comment

    by:Swadhin Ray
    Thanks a lot ..
    I used like below and works perfectly file:

    sed 's/\t\{1,\}/;/g'  Sample_file.txt > Sample-file-converted.txt

    Open in new window

    Can we convert the txt file to .csv file ?

    And use like optionally enclosed by ' " ' and comma delimited ?
    LVL 16

    Author Comment

    by:Swadhin Ray
    I got it :

    sed -e 's/"/\\"/g' -e 's/\t/","/g' -e 's/^/"/' -e 's/$/"/' Sample_file.txt > Sample-file-converted.csv
    chmod +x Sample-file-converted.csv

    Open in new window

    LVL 16

    Author Closing Comment

    by:Swadhin Ray
    Thanks a lot...

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
    Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now