Change delimiter is not working using awk


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Change delimiter is not working using awk
# 8  
Old 02-14-2018
What operating system are you using?
# 9  
Old 02-14-2018
Code:
awk -F, '{$1=$1}1' OFS=: 2.txt

with no space at all in {$1=$1}1
Code:
1:a
2:b
3:d

# 10  
Old 02-14-2018
Quote:
Originally Posted by vamsi.valiveti
Thanks for quick reply.I can get the solution using sed command but just want to know what is the problem with my command posted in question and why it is not working?
It is working, but mayhap not the way you want it to work. You print the entire line unaltered. And, why should awk modify the line if not triggered to do so? That's what RavinderSingh13 did. man awk:
Quote:
Assignment to NF or to a field causes $0 to be reconstructed by concatenating the $i's separated by OFS.
And this does work, at least for me and obviously for him, spaces in there ad libitum.
# 11  
Old 02-15-2018
Quote:
Originally Posted by Don Cragun
What operating system are you using?

I am using AIX
# 12  
Old 02-15-2018
With the awk that is shipped with AIX, there is no reason why the code suggested by RavinderSingh13:
Code:
awk -F, '{$1=$1} 1' OFS=":" 2.txt

and the equivalent code suggested byabdulbadii:
Code:
awk -F, '{$1=$1}1' OFS=: 2.txt

should not produce the output you say you want as long as a text file named 2.txt contains the sample text you showed us in post #1 in this thread.

Note that I'm only talking about the output produced by these scripts on standard output; I'm not saying that either of these scripts would change the contents of 2.txt (since neither of them would make any change to the input file).

As noted by RudiC in post #10 in this thread, the awk script you included in post #1 is fundamentally different from the code suggested by RavinderSingh13 and by abdulbadii. The code you included should just copy the contents of the named input file(s) to standard output without making any changes (for the reasons RudiC mentioned in his post).
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. Shell Programming and Scripting

Perl Code to change file delimiter (passed as argument) to bar delimiter

Hi, Extremely new to Perl scripting, but need a quick fix without using TEXT::CSV I need to read in a file, pass any delimiter as an argument, and convert it to bar delimited on the output. In addition, enclose fields within double quotes in case of any embedded delimiters. Any help would... (2 Replies)
Discussion started by: JPB1977
2 Replies

2. Shell Programming and Scripting

Need to use delimiter as : and space in awk

Hi , Please suggest me how do I use : (colon and one space) as a delimiter in awk Best regards, Vishal (2 Replies)
Discussion started by: Vishal_dba
2 Replies

3. Shell Programming and Scripting

awk :how to change delimiter without giving all field name

Hi Experts, i need to change delimiter from tab to "," sample test file cat test A0000368 A29938511 072569352 5 Any 2 for £1.00 BUTCHERS|CAT FOOD|400G Sep 12 2012 12:00AM Jan 5 2014 11:59PM Sep 7 2012 12:00AM M 2.000 group 5 ... (2 Replies)
Discussion started by: Lakshman_Gupta
2 Replies

4. Shell Programming and Scripting

Change the delimiter from Comma to Pipeline

Hello All, I need to convert a csv file to pipeline delimiter file in UNIX. The data in file itself contains comma with double qouted qualifier apart from the comma separator. Let me know how to do it. Appreciate any help if awk can be used to do it. Mentioned below is the sample record of... (14 Replies)
Discussion started by: Arun Mishra
14 Replies

5. Shell Programming and Scripting

To change the delimiter for first two columns

Dear Friends, I have file as below 1|sdf|rere|sert|trt|rtr i want to change the delimeter first three columns two fields expected output 1~sdf~rere|sert|trt|rtr Plz help (2 Replies)
Discussion started by: i150371485
2 Replies

6. Shell Programming and Scripting

awk delimiter

I have several hundred files that look like this: ABB110405110000.rawfma8 AAB110405110001.rawhr32 If I use the statement ls | awk '{x=$0;gsub("","",x) I see that all characters other than 0-9 are eliminated but how do I eliminate all characters including numbers after the "." In my mind I... (1 Reply)
Discussion started by: rdburg
1 Replies

7. UNIX for Dummies Questions & Answers

How to change delimiter in my file ?

Hi I have a file in which delimiter is ';' However if the delimiter is within "" it is a part of the string and not delimiter. How to get the fields ? I want to replace the delimiter ';' to '|'. The file contains data like this : 11111; “2222 2222”; “3333; 3333”; “4444 ""44444” The file... (2 Replies)
Discussion started by: dashing201
2 Replies

8. Shell Programming and Scripting

AWK double delimiter

Hello, an awk style question (or a stupid question... it depends on your point of view :) ) How can I write in one awk command these two ones ? $USER - `grep $USER /etc/passwd | awk -F: '{ print $5 }' | awk -F, '{ print $1 }'` Thanks gb (4 Replies)
Discussion started by: gogol_bordello
4 Replies

9. Shell Programming and Scripting

AWK how to change delimiter while outputting

Hello I need some help in outputting Fields when the delimiter has changed: echo "test1,test2 | test3,test4,test5" | awk -F"," '{print $1,"COUNT",$2,$4}' prints out: test1 COUNT test2 | test3 test5 But how to change the -F"," to -F"|" delimiter, so that it separates the fields from $2... (2 Replies)
Discussion started by: sdohn
2 Replies

10. Shell Programming and Scripting

Pivot variable record length file and change delimiter

Hi experts. I got a file (500mb max) and need to pivot it (loading into ORCL) and change BLANK delimiter to PIPE |. Sometimes there are multipel BLANKS (as a particular value may be BLANK, or simply two BLANKS instead of one BLANK). thanks for your input! Cheers, Layout... (3 Replies)
Discussion started by: thomasr
3 Replies
Login or Register to Ask a Question