Shell Script to convert the file

Hello Experts,

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

Sample-file.txt

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

After conversion the file should look like below:

Final output should look like below:
Expected-Result.txt
LVL 17
Swadhin RaySenior Technical Engineer Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

woolmilkporcCommented:
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.
0
Swadhin RaySenior Technical Engineer Author Commented:
how I can put them all into one "test.sh" file.

where I need to run something like :

./test.sh
which should take the " Sample-file.txt " autometically and convert the file and save as like :
Sample-file-converted.txt
0
woolmilkporcCommented:
Create a file "test.sh" like this:

#!/bin/bash
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

./test.sh
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Swadhin RaySenior Technical Engineer Author Commented:
Thanks a lot ..
I used like below and works perfectly file:

#!/bin/bash
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 ?
0
Swadhin RaySenior Technical Engineer Author Commented:
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

0
Swadhin RaySenior Technical Engineer Author Commented:
Thanks a lot...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.