Learn how to a build a cloud-first strategyRegister Now


Shell Script to convert the file

Posted on 2014-07-11
Medium Priority
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
  • 4
  • 2
LVL 68

Expert Comment

ID: 40190258
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
ID: 40190273
how I can put them all into one "test.sh" 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

woolmilkporc earned 2000 total points
ID: 40190284
Create a file "test.sh" 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 "test.sh" run:

chmod +x test.sh

to set the "executable" bit, then run

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

LVL 16

Author Comment

by:Swadhin Ray
ID: 40190489
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
ID: 40190504
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
ID: 40190506
Thanks a lot...

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Utilizing an array to gracefully append to a list of EmailAddresses
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

810 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