How to delimit the fields of a input file which has special characters?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to delimit the fields of a input file which has special characters?
# 1  
Old 03-01-2017
How to delimit the fields of a input file which has special characters?

Hi All, I am a newbie to Shell scripting. I have a requirement to Delimit the file fields of a Input file having special characters and spaces with ";".

Input File
Code:
 
----------------------------------
Server                    Port
----------------------------------
Local                      1001

-----------------------------------------
Name        Country        Count
-----------------------------------------
XXX          Bermuda        999

So my requirement is to get the output like,
Code:
Server;Port
Local;1001

Name;Country;Count
XXX;Bermuda;999

Kindly, Please help me to full-fill the requirement..

Thank you in advance
# 2  
Old 03-01-2017
Welcome to the forum; and: thanks for intuitively using code tags correctly!

Any preferred tools? As you didn't specify what to do with the dash lines, nor with the empty lines (one is suppressed, one is retained), try (exploiting a few of awk's internals and defaults):
Code:
awk '$1=$1' OFS=";" file
----------------------------------
Server;Port
----------------------------------
Local;1001
-----------------------------------------
Name;Country;Count
-----------------------------------------
XXX;Bermuda;999


EDIT: to remove the dash lines, expand like
Code:
awk '($1=$1) && 1-sub(/--*/,_)' OFS=";" file
Server;Port
Local;1001
Name;Country;Count
XXX;Bermuda;999

These 2 Users Gave Thanks to RudiC For This Post:
# 3  
Old 03-02-2017
Thanks for the quick reply Rudic. I will try

---------- Post updated 03-02-17 at 06:01 PM ---------- Previous update was 03-01-17 at 09:03 PM ----------

When executed i am getting the below error
Code:
awk: syntax error near line 1
awk: bailing out near error 1

# 4  
Old 03-02-2017
Assuming your OS - which you failed to mention - is SunOS or Solaris:

Quote:
Originally Posted by Don Cragun
If you are using a Solaris/SunOS system, use /usr/xpg4/bin/awk or nawk instead of awk.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 03-03-2017
Thanks Rudic.. It worked. Can you please help me if the country name is

Code:
Saudi Arabia

I am getting it output as
Code:
Saudi;Arabia

Sorry for changing the requirement. The output should be like
Code:
Server;Port;Name;Country;Count
Local;1001;XXX;South Africa;999


Last edited by Suganbabu; 03-03-2017 at 02:24 AM.. Reason: Requirement change
# 6  
Old 03-03-2017
That dependes on if and how fields are separated uniquely. If there is any chance that fields are separated by just one single space, you're doomed. If there's ALWAYS multiple spaces separating fields, change the field separator to sth. like " +".
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Rename File Name with Special Characters

I am trying to rename files with spaces and other characters and not able to be successful. FileNames: UPLOAD REFERENCE.xls UPLOAD MASS REFERENCE.XLS find /UPLOAD REFERENCE/ -depth -type f -name "* *" -exec rename " " "_" "{}" ";" The above one is successful to replace spaces... (1 Reply)
Discussion started by: eskay
1 Replies

2. UNIX for Dummies Questions & Answers

Naming file with special characters ?

Hi all, I have a problem with file naming in linux. I have to create a file named like 11/22.csv but shell displays error: no such file or dir. Problem is / character in the file name. I searched unix linux naming concepts and it's restricted in OS. Please tell me if there's any other chance? OS... (3 Replies)
Discussion started by: sembii
3 Replies

3. Shell Programming and Scripting

File containing special characters

Hello All, I am facing challenges in order to transfer a file from windows to unix box,the file contains a special character '×' ,now when I am transferring the file from windows to unix that special character converted to something else like 'Ã' ,another thing I have noticed that the hardware is... (1 Reply)
Discussion started by: prarat
1 Replies

4. Shell Programming and Scripting

Check input for lenght, special characters and letter case

I made menu script for users so they can run other script without going in shell just from menu. But i must control their input. These are criteria: Input must have 4 signs First two signs are always lower case letters Input shall not have some special signs just letters and numbers ... (1 Reply)
Discussion started by: waso
1 Replies

5. Shell Programming and Scripting

Ommiting special characters as input - Help

Hey Everyone, I'm quite new to unix (hence the 0 posts!) and im trying to write a simple program that outputs what the user types in to the screen, as long as it is a letter. This part works fine, however, when a "\" is entered doesnt not display anything and moves to the next line. Is... (11 Replies)
Discussion started by: ultiron
11 Replies

6. Shell Programming and Scripting

Help in preserving special characters from input file

Hi Forum. I've tried to search online for a solution but I cannot seem to find one. Hopefully, someone here can help me out. I would appreciate it. Input file abc.txt: $InputFile_Borrower=CMTSLST\EDW_COMMERCIAL_MTGE_BORROWER_dat.lst... (14 Replies)
Discussion started by: pchang
14 Replies

7. Shell Programming and Scripting

Bourne Shell: Special characters Input Prevention

Environment: Sun UNIX Language: Bourne Shell Problem: I am writing a script that allows user to key in a directory. Example: /root/tmp. Since the user can key in anything he/she wants, I need to validate to make sure that he/she does not key in anything funny i.e.... (37 Replies)
Discussion started by: totziens
37 Replies

8. Shell Programming and Scripting

escaping special characters in file name...

dear, I would like to rename files in a dir to another format, so I write a bash shell script to handle it. But my problem now is how to handle files having special characters like spaces, (, ): "a b c (d).doc" It seems that I need to escape those characters before applying the "mv" command.... (1 Reply)
Discussion started by: lau0001
1 Replies

9. UNIX for Dummies Questions & Answers

how to see special characters in a file using vi

Hi, I have a file which has special characters. I can't see them when I "vi" the file. But I am sure there are some special un seen characters. How can I see them? Please help. Thx (6 Replies)
Discussion started by: jingi1234
6 Replies

10. UNIX for Dummies Questions & Answers

search special characters in a file

Hello I am new to shell scripting and can anyone tell me how to check if there are any special characters in a file. Can i use grep ? thanks susie (2 Replies)
Discussion started by: cramya80
2 Replies
Login or Register to Ask a Question