Ignore delimiter within a column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ignore delimiter within a column
# 1  
Old 06-08-2014
Ignore delimiter within a column

Hi,

So I was trying this awk snippet


Code:
awk -F, '$1 ~ /Match/' File

This is my sample file output

Code:
Name,Age,Nationality,Description
Jack,20,American,Tall, caucasian, lean
Mary,30,British,Short,white,slim

I would expect expected Output to be, when a certain match is met say $1 is equal to Jack

Code:
Name Age Nationality Description
Jack 20 American Tall, caucasian, lean

But as you can tell, because of delimiter "," it delimits the description output as below

Code:
Name Age Nationality Description
Jack 20 American Tall caucasian lean

Throwing results out of columns boundary.

Any idea how to have a workaround of this?
# 2  
Old 06-08-2014
Here is one solution:

Code:
awk -F, '{if (NR==1) {gsub(","," ",$0); print $0} else {if ($1 ~ /Jack/) {print $1,$2,substr($0,length($1)+length($2)+3,length($0))}}}' File

Name Age Nationality Description
Jack 20 American,Tall, caucasian, lean

# 3  
Old 06-08-2014
If you want to display the description without fixing commas inconsistency among records:
Code:
awk -F, '$1 ~ /Jack|Mary/ {match($0, $4); print $1,$2,$3, substr($0, RSTART)}' file 

Jack 20 American Tall, caucasian, lean
Mary 30 British Short,white,slim

If you want to fix the commas inconsistency of descriptions
Code:
awk -F, '$1 ~ /Jack|Mary/ {match($0, $4); d = substr($0, RSTART); gsub(/(,|, )/, ", ", d); print $1,$2,$3, d}' file 

Jack 20 American Tall, caucasian, lean
Mary 30 British Short, white, slim


Last edited by Aia; 06-08-2014 at 09:52 PM.. Reason: Add the condition of first field
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. UNIX for Advanced & Expert Users

How to remove the delimiter from the column value within a file?

Hello All, we have some 10 files wherein we are using the ASCII NULL as separator which is nothing but '^@' and we need to change it to pipe delimited file before loading to database. Most of the data seems to be fine but there are instances where this separator tends to appear in the middle of... (9 Replies)
Discussion started by: dJHa
9 Replies

3. Shell Programming and Scripting

Replace specific column delimiter

Hi All, I have a file with a pipe delimiter. I need to replace the delimiter with html tags. I managed to get all the delimiters replaced along with first and last but the requirement is that I need to change 7th delimiter with slight change. File1: ... (2 Replies)
Discussion started by: shash
2 Replies

4. Shell Programming and Scripting

awk code to ignore the first occurence unknown number of rows in a data column

Hello experts, Shown below is the 2 column sample data(there are many data columns in actual input file), Key, Data A, 1 A, 2 A, 2 A, 3 A, 1 A, 1 A, 1 I need the below output. Key, Data A, 2 A, 2 A, 3 A, 1 A, 1 A, 1 (2 Replies)
Discussion started by: ks_reddy
2 Replies

5. Shell Programming and Scripting

Column to row with delimiter

Hi, i have data in a file f1.txt a b c d and i want to print the above column values in single line with a delimiter, say ',' The output should look like: a,b,c,d I could find rows to columns help online but not vice versa Thanks, -srinivas yelamanchili (4 Replies)
Discussion started by: ysrini
4 Replies

6. Shell Programming and Scripting

Grep but ignore first column

Hi, I need to perform a grep from a file, but ignore any results from the first column. For simplicity I have changed the actual data, but for arguments sake, I have a file that reads: MONACO Monaco ASMonaco MANUTD ManUtd ManchesterUnited NEWCAS NewcastleUnited NAC000 NAC ... (5 Replies)
Discussion started by: danhodges99
5 Replies

7. Shell Programming and Scripting

How to remove delimiter from specific column?

I have 5 column in sample txt file where in i have to create report based upon 1,3 and 5 th column.. I have : in first and third coulmn. But I want to retain the colon of fifth coulmn and remove the colon of first column.. 5th column contains String message (for example,... (7 Replies)
Discussion started by: Shirisha
7 Replies

8. Shell Programming and Scripting

How to ignore delimiter between the quotes?

Hi Guys, I have following script which counts number of tilde in file but i want to count tilde which are field saparator.but my script count tilde between word also what i need is if line is like abcd~das~1212~fsddf~ so tilde count is = 4 if line like abcd~das~1212~fsd"~"df~ so tilda count... (4 Replies)
Discussion started by: Ganesh Khandare
4 Replies

9. Shell Programming and Scripting

rearrange the column names with comma as column delimiter

Hi, I am new to shell scripting, i have requirement can any one help me out in this regrads, in directory i have file like invoice1.txt, invoice2.txt in each file i have fixed number of columns, 62 in number but they are randomly arranged.like for first file invoice1.txt can have columns... (5 Replies)
Discussion started by: madhav62
5 Replies

10. Shell Programming and Scripting

Deleting column from a flatfile with delimiter

I have a set of flatfiles which have columns delimited by #. How can a particular column be deleted in all the flatfiles. All flatfiles have same number of columns. (5 Replies)
Discussion started by: rsprabha
5 Replies
Login or Register to Ask a Question