Output file with <Tab> or <Space> Delimited


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Output file with <Tab> or <Space> Delimited
# 1  
Old 12-31-2015
IBM Output file with <Tab> or <Space> Delimited

Input file:
Code:
xyz,pqrs.lmno,NA,NA,NA,NA,NA,NA,NA
abcd,pqrs.xyz,NA,NA,NA,NA,NA,NA,NA

Expected Output:
Code:
 
 xyz pqrs.lmno NA NA NA NA NA NA NA
abcd pqrs.xyz NA NA NA NA NA NA NA

Command Tried so far:
Code:
 
 awk -F"," 'BEGIN{OFS=" ";} {print}' $File_Path/File_Name.csv

Issue:
Output is not as extected... output is same as input.
# 2  
Old 12-31-2015
To me, sed seems a simpler alternative:
Code:
user@host~> sed 's/,/ /g' file
xyz pqrs.lmno NA NA NA NA NA NA NA
abcd pqrs.xyz NA NA NA NA NA NA NA
user@host~>

Correction to your command:
Code:
awk -F"," 'BEGIN{OFS=" ";} {$1=$1; print}' file

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 12-31-2015
Hello TechGyaann,

Following may help you in same.
1st:
Code:
 tr ',' ' ' < Input_file

2nd:
Code:
awk '{gsub(/\,/," ",$0);print}'  Input_file

Output will be as follows in both above codes.
Code:
xyz pqrs.lmno NA NA NA NA NA NA NA
abcd pqrs.xyz NA NA NA NA NA NA NA

NOTE: considering that the first line space which you have showed us in the sample output is a typo here.

Thanks,
R. Singh

Last edited by RavinderSingh13; 12-31-2015 at 03:24 AM.. Reason: Added a note for user.
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 12-31-2015
IBM

Quote:
Originally Posted by RavinderSingh13
Hello TechGyaann,

Following may help you in same.
1st:
Code:
 tr ',' ' ' < Input_file

2nd:
Code:
awk '{gsub(/\,/," ",$0);print}'  Input_file

Output will be as follows in both above codes.
Code:
xyz pqrs.lmno NA NA NA NA NA NA NA
abcd pqrs.xyz NA NA NA NA NA NA NA

NOTE: considering that the first line space which you have showed us in the sample output is a typo here.

Thanks,
R. Singh
Yeah that was a Typo.

I shall try it, hope it should work. (not sure about gsub etc.. im learning awk and unix shell scripting)
# 5  
Old 12-31-2015
Quote:
Originally Posted by TechGyaann
[...]not sure about gsub etc.. im learning awk and unix shell scripting)
Awk has two built-in functions to deal with string substitutions: sub() and gsub(). There are others built-ins functions for matching and extracting parts of substrings, but these are for substitutions. The main different between both is that sub() will only do the substitution one time on the first instance of the match, and gsub() will do it for every instance of the match; the g in front tries to detonate this distinction and stand for global as in all or the whole.
The syntax could be expressed as gsub(regex, replacement, string), where regex is an Extended Regular Expression to match, replacement is the substitution part and string is the sequence of characters you want the work to be done on.
Using the example given by RavinderSingh13:
Code:
awk '{gsub(/\,/," ",$0);print}'

In gsub(), now we know:
/\,/ is the regex representing the comma we want to find
" " is what we want to substitute for
$0 is the range of characters we want to work on, in this case the whole record.
After the function is done:
print is for after the substitutions are done, display the whole modified record.

However, awk users are attracted by the terse and brief statement abilities that the language has to offer and how much does with so little user code, thus they thrive in applying the most idiomatic and succinct expressions possible. Don't be surprise if you see instead of:
Code:
awk '{gsub(/\,/," ",$0);print}' file.here

this:
Code:
awk 'gsub(/,/, " ")' file.here

or even
Code:
awk 'gsub(/,/, OFS)' file.here

The original command and these two latest produce the same output, in this case.

Or instead of:
Code:
awk -F"," 'BEGIN{OFS=" ";} {$1=$1; print}' file

this:
Code:
awk -F, '$1=$1' file


Last edited by Aia; 12-31-2015 at 03:00 PM.. Reason: Add more
# 6  
Old 12-31-2015
Quote:
Originally Posted by Aia
... ... ...
Or instead of:
Code:
awk -F"," 'BEGIN{OFS=" ";} {$1=$1; print}' file

this:
Code:
awk -F, '$1=$1' file

Note that, although it works with the sample data provided, the last suggestion above will delete lines found in file that have an empty 1st field or have a 1st field that is just a string of one or more 0 characters. The following idiom is safer if either of these could be present in your input:
Code:
awk -F, '{$1=$1}1' file

This User Gave Thanks to Don Cragun 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

Replace a column in tab delimited file with column in other tab delimited file,based on match

Hello Everyone.. I want to replace the retail col from FileI with cstp1 col from FileP if the strpno matches in both files FileP.txt ... (2 Replies)
Discussion started by: YogeshG
2 Replies

2. UNIX for Dummies Questions & Answers

Need to convert a pipe delimited text file to tab delimited

Hi, I have a rquirement in unix as below . I have a text file with me seperated by | symbol and i need to generate a excel file through unix commands/script so that each value will go to each column. ex: Input Text file: 1|A|apple 2|B|bottle excel file to be generated as output as... (9 Replies)
Discussion started by: raja kakitapall
9 Replies

3. Shell Programming and Scripting

How to convert space&tab delimited file to CSV?

Hello, I have a text file with space and tab (mixed) delimited file and need to convert into CSV. # cat test.txt /dev/rmt/tsmmt32 HP Ultrium 6-SCSI J3LZ 50:03:08:c0:02:72:c0:b5 F00272C0B5 0/0/6/1/1.145.17.255.0.0.0 /dev/rmt/c102t0d0BEST /dev/rmt/tsmmt37 ... (6 Replies)
Discussion started by: prvnrk
6 Replies

4. Shell Programming and Scripting

How to remove alphabets/special characters/space in the 5th field of a tab delimited file?

Thank you for 4 looking this post. We have a tab delimited file where we are facing problem in a lot of funny character. I have tried using awk but failed that is not working. In the 5th field ID which is supposed to be a integer only of that file, we are getting corrupted data as below. I... (12 Replies)
Discussion started by: Srithar
12 Replies

5. Shell Programming and Scripting

How to make tab delimited file to space delimited?

Hi How to make tab delimited file to space delimited? in put file: ABC kgy jkh ghj ash kjl o/p file: ABC kgy jkh ghj ash kjl Use code tags, thanks. (1 Reply)
Discussion started by: jagdishrout
1 Replies

6. UNIX for Dummies Questions & Answers

Changing only the first space to a tab in a space delimited text file

Hi, I have a space delimited text file but I only want to change the first space to a tab and keep the rest of the spaces intact. How do I go about doing that? Thanks! (3 Replies)
Discussion started by: evelibertine
3 Replies

7. Shell Programming and Scripting

Help with converting Pipe delimited file to Tab Delimited

I have a file which was pipe delimited, I need to make it tab delimited. I tried with sed but no use cat file | sed 's/|//t/g' The above command substituted "/t" not tab in the place of pipe. Sample file: abc|123|2012-01-30|2012-04-28|xyz have to convert to: abc 123... (6 Replies)
Discussion started by: karumudi7
6 Replies

8. UNIX for Dummies Questions & Answers

How to echo space or tab delimited values into rows?

Hi, I have the following code: LIST=`ls | grep '.sql$'` echo $LIST The above code will give me something like.. file1.sh file2.sh file3.sh file4.sh file5.sh I want to display the values into rows using echo like... file1.sh file2.sh (5 Replies)
Discussion started by: adshocker
5 Replies

9. Shell Programming and Scripting

Merging files into a single tab delimited file with a space separating

I have a folder that contains say 50 files in a sequential order: cdf_1.txt cdf_2.txt cdf_3.txt cdf_3.txt . . . cdf_50.txt. I need to merge these files in the same order into a single tab delimited file. I used the following shell script: for x in {1..50}; do cat cdf_${x}.txt >>... (3 Replies)
Discussion started by: Lucky Ali
3 Replies

10. UNIX for Dummies Questions & Answers

Converting Space delimited file to Tab delimited file

Hi all, I have a file with single white space delimited values, I want to convert them to a tab delimited file. I tried sed, tr ... but nothing is working. Thanks, Rajeevan D (16 Replies)
Discussion started by: jeevs81
16 Replies
Login or Register to Ask a Question