awk :how to change delimiter without giving all field name

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk :how to change delimiter without giving all field name
# 1  
Old 10-19-2013
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


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

i have used below codes

awk -F "\t" 'BEGIN {OFS="\",\""} {print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13}' test

now is there anyway without giving all field from $1 to $13 i can achieve same result in awk.

there is way in sed like below
sed 's/    /","/g'

but i dont want to use sed.
# 2  
Old 10-19-2013
awk '{$1=$1}1' FS='\t' OFS='","' file

By assigning $1 onto itself, one of the fields gets altered and therefore the records gets recalculated and FS gets replaced by the OFS..

If you happen to use AIX try:
awk '{$1=x$1}1' FS='\t' OFS='","' file

Last edited by Scrutinizer; 10-19-2013 at 04:24 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 10-21-2013
A sed option:-
sed 's/<tab>/","/g'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Change delimiter is not working using awk

I have file 2.txt and I want to change the delimiter form , to : Not sure what is the problem with below command cat 2.txt 1,a 2,b 3,d awk 'BEGIN {FS=",";OFS=":";} {print $0}' 2.txt Please use CODE tags as required by forum rules! (11 Replies)
Discussion started by: vamsi.valiveti
11 Replies

2. UNIX for Beginners Questions & Answers

String has * as the field delimiter and I need echo/awk to escape it, how?

Hi, I am trying to read an Oracle listener log file line by line and need to separate the lines into several fields. The field delimiter for the line happens to be an asterisk. I have the script below to start with but when running it, the echo command is globbing it to include other... (13 Replies)
Discussion started by: newbie_01
13 Replies

3. Shell Programming and Scripting

How can awk ignore the field delimiter like comma inside a field?

We have a csv file as mentioned below and the requirement is to change the date format in file as mentioned below. Current file (file.csv) ---------------------- empname,date_of_join,dept,date_of_resignation ram,08/09/2015,sales,21/06/2016 "akash,sahu",08/10/2015,IT,21/07/2016 ... (6 Replies)
Discussion started by: gopal.biswal
6 Replies

4. 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

5. Shell Programming and Scripting

how to find the nth field value in delimiter file in unix using awk

Hi All, I wanted to find 200th field value in delimiter file using awk.? awk '{print $200}' inputfile I am getting error message :- awk: The field 200 must be in the range 0 to 199. The source line number is 1. The error context is {print >>> $200 <<< } using... (4 Replies)
Discussion started by: Jairaj
4 Replies

6. Shell Programming and Scripting

awk output field delimiter

Dear All, 1.txt (tab in between each value in a line) a b c a b c a c d you can see below, why with ~ i can output with tab, but = cannot? # awk -F'\t' '$2 ~ /b/' 1 a b c a b c # awk -F'\t' '$2 = "b"' 1 a b c a b c a b d ... (1 Reply)
Discussion started by: jimmy_y
1 Replies

7. Shell Programming and Scripting

need to convert a decimal value to an ascii char with awk for the field delimiter

Hello, I need an awk script to receive a variable that's an decimal value such as 009 or 031 and then convert this value to an ascii character to use as the FS (field separator for the input file). For example, 009 should be converted to an ascii tab 031 should be converted to an ascii... (1 Reply)
Discussion started by: script_op2a
1 Replies

8. UNIX for Advanced & Expert Users

Printing Field with Delimiter in AWK/cut

Hello, I had posted earlier about printing fields using AWK, but now I have a slightly different problem. I have text files in the format: 1*2,3,4,5 and wish to print the first, third, and fifth fields, including the asterisk and commas. In other words, after filtering it should look... (1 Reply)
Discussion started by: Jahn
1 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

Set a variable field delimiter using awk

How can i set a variable field delimiter using awk?? I wanna do something like this ,but i canīt get the correct syntaxis : VARI=TEST echo "0121212TESTxvcshaashd"|awk 'FS="$VARI" {print $2}' Thanks. (2 Replies)
Discussion started by: Klashxx
2 Replies
Login or Register to Ask a Question