Why awk removes delimiters?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Why awk removes delimiters?
# 1  
Old 02-10-2014
[Solved] Why awk removes delimiters?

Code :

Code:
echo "1,2,3,4"|awk -F "," 'NR==n{$3=a}1' n=1 a=45

Output :

Code:
1 2 45 4

Expected :

Code:
1,2,45,4

# 2  
Old 02-10-2014
Because you reformatted the output by modifying a field, just set the print output separator to the one you want:

Code:
$ echo "1,2,3,4"|nawk -F "," 'NR==n{$3=a}1' n=1 a=45 OFS=","
1,2,45,4

This User Gave Thanks to jlliagre For This Post:
# 3  
Old 02-10-2014
Thanks,

nawk is not working for me but awk is working

Code:
echo "1,2,3,4"|awk -F "," 'NR==n{$3=a}1' n=1 a=45 OFS=","

few more questions

if i have a variable number1

i need to check number1=55

i need to modify the code to check the condition and replace

Code:
echo "1,2,3,4"|awk -F "," 'NR==n{$3=a}1' n=1 a=45 OFS=","

if i run below

Code:
#!/bin/ksh
set -x
n=1
a=45
echo "1,2,3,4"|awk -F "," -v a=$a n=$n 'NR==n{$3=a}1' OFS=","

output

Code:
1,2,3,4


Expected

Code:
1,2,45,4


Last edited by Rajesh_us; 02-10-2014 at 10:29 PM..
# 4  
Old 02-10-2014
You are missing an option:
Code:
#!/bin/ksh
set -x
n=1
a=45
echo "1,2,3,4"|awk -F "," -v a=$a -v n=$n 'NR==n{$3=a}1' OFS=","

This User Gave Thanks to jlliagre For This Post:
# 5  
Old 02-11-2014
thanks a lot
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed or awk removes attachment in email

Hi We have a requirement to send email using shell script.email should have html body and pdf attachment. We used uuencode for attaching files and sendmail option to acheive and it is working fine. However custoemr wants to make body of email slightly dynamic. E.g dear customer in html file... (3 Replies)
Discussion started by: Harish7586
3 Replies

2. Shell Programming and Scripting

awk removes delimiter unexpectedly

datafile: blah,blah,blah,blah,blah,blah,blah,blah,blah=0_nblah=0-- ,blah,blah,blah im using the following command to turn the "_n" and "-- " to just a space " " only in the $9th field. meaning, it has to make the changes only in the 9th column/field of the datafile. awk -F, '{... (1 Reply)
Discussion started by: SkySmart
1 Replies

3. Shell Programming and Scripting

Delimiters with awk?

I have a file which is separated by delimiter "|", but the prob is one of my column do contain delimiter as description so how can i differentiate it? PS : the delmiter does have backslash coming before it, if occurring in column Annual|Beleagured|Desc|Denver... (2 Replies)
Discussion started by: nikhil jain
2 Replies

4. Shell Programming and Scripting

awk multiple delimiters

Hi Folks, This is the first time I ever encountered this situation My input file is of this kind cat input.txt 1 PAIXAF 0 1 1 -9 0 0 0 1 2 0 2 1 2 1 7 PAIXEM 0 7 1 -9 1 0 2 0 1 2 2 1 0 2 9 PAKZXY 0 2 1 -9 2 0 1 1 1 0 1 2 0 1 Till the sixth column (which is -9), I want my columns to... (4 Replies)
Discussion started by: jacobs.smith
4 Replies

5. Shell Programming and Scripting

Use two delimiters in awk

I have a file having lines like: 14: <a="b" val="c"/> 18: <a="x" val="d"/> 54: <a="b" val="c"/> 58: <a="x" val="e"/> I need to create a file with output: 14 d 54 e So basically, for every odd line I need 1st word if delimiter is ':' and for every even... (14 Replies)
Discussion started by: shekhar2010us
14 Replies

6. Shell Programming and Scripting

Delimiters in awk

Line from input file a : b : c " d " e " f : g : h " i " j " k " l output k b a Its taking 7th word when " is the delimiter, 2nd and 1st word when : is the delimiter and returning all in one line.... I am on solaris Thanks..... (1 Reply)
Discussion started by: shekhar2010us
1 Replies

7. UNIX for Dummies Questions & Answers

comparing two files having different delimiters using awk

hi, i have a file called file1.txt and it's contents are as below: file1.txt: ------- abc,123, thomas dab,234,muller gab,456,ram The lookup file's contents are as below: lookup.txt ---------- abc|japan dcd|US dab|china gab|brazil (3 Replies)
Discussion started by: amar1003
3 Replies

8. Shell Programming and Scripting

Two delimiters with AWK

Hello, this thread is more about scripting style than a specific issue. I've to grep from a output some lines and from them obtain a specific entry delimited by < and >. This is my way : 1) grep -i user list | awk '{FS="<";print $NF}' | sed -e 's/>//g' 2) grep -i user list | cut -d","... (10 Replies)
Discussion started by: gogol_bordello
10 Replies

9. Shell Programming and Scripting

AWK with multiple delimiters

I have the following string sample: bla bla bla bla bla I would like to extract the "123" using awk. I thought about awk -F"]" '{ print $1 }' but it doesn't work Any ideas ? (7 Replies)
Discussion started by: gdub
7 Replies

10. Shell Programming and Scripting

two delimiters with awk, one of them blank

Hello, I need use comma and spaces as field delimiters, but I can't: text: hello myfriend,I need,some help I need something like: awk -F"<blank>|," '{print $1, $3}' Thanks (3 Replies)
Discussion started by: albertogarcia
3 Replies
Login or Register to Ask a Question