Nawk command not working for Question mark (?)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Nawk command not working for Question mark (?)
# 1  
Old 11-23-2016
Nawk command not working for Question mark (?)

Hi Folks,

I am facing an issue with nawk command.

The data is as below:

Code:
ABC0022,BASC,Scene Package,INR,02May17,XXX4266,be?. Hotel,3,AW01,Twin Room,61272,41308,39590,39590,X,X

ABC0022,BASC,Scene Package,INR,02May17,XXX4266,be?. Hotel,3,AW02,Twin Room with Balcony,9272,85638,4520,9590,X,X


If the first 8 columns matches in the two records then we should append the data as one records as shown below:

Code:
ABC0022,BASC,Scene Package,INR,02May17,XXX4266,be?. Hotel,3,AW01,Twin Room,61272,41308,39590,39590,X,X,AW02,Twin Room with Balcony,9272,85638,4520,9590,X,X

I am using nawk to achieve this.
Code:
nawk -F, ' RES ~ "^" $1","$2","$3","$4","$5","$6","$7","$8 FS {X= $1","$2","$3","$4","$5","$6","$7","$8
			   sub (X FS,"")  	
			   RES=RES FS $0
		          next
			 }  
			{if (NR > 1)
	 	       print RES   
			RES=$0 
                    } 
END			{print RES}
' holiday_file.csv > holiday_file_merge.csv

---

But though first 8 columns are matching due to a question mark (?) in 7th column "be?. Hotel" i am not achieving proper output which should be in a single line.

But i am getting it as 2 separate rows.

When there is no question mark (?) in the data, nawk is giving proper output.

Can you please help me out in understanding as to why a question mark (?) is causing an issue and how to mitigate this?



Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 11-23-2016 at 11:09 AM.. Reason: Added CODE tags.
# 2  
Old 11-23-2016
The question mark is a special character in regular expressions. man regex:
Quote:
An atom followed by '?' matches a sequence of 0 or 1 matches of the atom.
You can't use it by itself but need to take extra measures, e.g. escape it, or replace it.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

--SunOS 5.10 nawk for paragraph not working

The machine is using bash: ================== bash -version GNU bash, version 3.2.51(1)-release (i386-pc-solaris2.10) Copyright (C) 2007 Free Software Foundation, Inc. ========================= I have the following xml file. am trying to get a whole paragraph if it meets certain criteria.... (9 Replies)
Discussion started by: gilgamesh
9 Replies

2. Shell Programming and Scripting

Nawk Script not working

Any idea why this isn't working? YESTERF=`TZ=aaa24 date +%b"-"%d | sed 's/-0/--/'` filelist2=$(find /export/home/gen/cks/traces \( -name \*YESTERF\* -name \*DNA\* \) -print | tr '\n' ' ') print "Date/Time,Location,Shelf,IP,Reason,Log Filename" >> $OUTPUT nawk -F':' ' $2 ~... (2 Replies)
Discussion started by: ther2000
2 Replies

3. Shell Programming and Scripting

Backwards question mark appearing in FTP'd file

Hi all, I'm trying to FTP what looks like a simple .txt file from my Windows XP desktop to my UNIX server. I've tried using several programs to do this including UltraEdit and FTP Commander. I have tried sending it ascii, binary and even let the program decide. But every time it arrives in UNIX... (4 Replies)
Discussion started by: Korn0474
4 Replies

4. Shell Programming and Scripting

Question mark in filename

Dear All, I am trying to run some commands and I am getting question mark in filename as output files. Which is not a literal question mark however it is not standard output format for UNIX and it can not print it. The output files are extended with ? For example,... (2 Replies)
Discussion started by: hsmart
2 Replies

5. Shell Programming and Scripting

NAWK Script not working fine

Hello to all can any one help me out with a nawk script. Actually i am having a shell script which uses nawk pattern searching and it is not parsing the file properly. I have been debugging it since long time, but nt able 2 find the root cause.. If any one can help me out with this one .. (3 Replies)
Discussion started by: dheeraj19584
3 Replies

6. Shell Programming and Scripting

nawk question

I am trying to write the following c code in nawk: while ((ch = getc (fp)) != EOF) { total_bytes++; checksum = (checksum >> 1) + ((checksum & 1) << 15); checksum += ch; checksum &= 0xffff; /* Keep it within bounds. */ } I appreciate your help (10 Replies)
Discussion started by: DeltaX
10 Replies

7. Shell Programming and Scripting

NAWK question

Hello everybody !! I just started messing around with NAWK/AWK and I find it very interesting. I was trying to have script that prints only the the different lines (lines that are identical and adjacent, some that are identical and not adjacent, and some that are just different) I tried... (6 Replies)
Discussion started by: DeltaX
6 Replies

8. UNIX for Dummies Questions & Answers

trailing question mark in filename

I have a script(ex.sh) with one line in it, running in bash shell. ls -l > /usr/ngasi/contexts/tdevoe/private/ex.txt when I run it , it creates the file with a trailing question mark -rwx------ 1 tdevoe webapp 59 Jun 7 06:42 ex.sh -rw------- 1 tdevoe webapp 3761 Jun ... (3 Replies)
Discussion started by: devoetfd
3 Replies

9. UNIX for Dummies Questions & Answers

? question mark, how to get back to the root directory

hiyas I am trying to get back to the root directory: I went into MAIL directory and now I can't get back to the root directory. What are the commands... I have '?' coming up and I cannot proceed with this, HELP Cheers (1 Reply)
Discussion started by: etravels
1 Replies
Login or Register to Ask a Question