awk - CSV file - field with single or multiple spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - CSV file - field with single or multiple spaces
# 1  
Old 01-28-2015
IBM awk - CSV file - field with single or multiple spaces

Hi,

In a csv file, I want to select records where first column has zero or multiple spaces.

Eg: abc.csv
HTML Code:
,123,a
  ,22,b
    ,11,c
a,11,d
So output should be:

Code:
,123,a
  ,22,b
    ,11,c


Please advise
# 2  
Old 01-28-2015
What have you tried so far?
# 3  
Old 01-28-2015
Tried below options of awk.. but it works only for zero spaces.

Code:
 awk -F',' '! $1 ~  /./' 
awk -F',' '$1=="" {print $0}'

# 4  
Old 01-28-2015
Code:
awk -F"," ' ! $1 ' file

Or

Code:
sed -n "/^ *,/p" file


Last edited by anbu23; 01-28-2015 at 03:39 AM.. Reason: Added Sed solution
This User Gave Thanks to anbu23 For This Post:
# 5  
Old 01-28-2015
Hello vegasluxor,

Following may help you in same.
Code:
awk -F, '($1 == "" || $1 ~ /[[:space:]]/)'  Input_file

But if your first field has space and some value then that input will be catch also, as you didn't mention this condition in your input so I am giving you above solution. If you have first field only empty NOT a space with values following may help you in same too.
Code:
awk '{match($1,/^ *,/);A=substr($1,RSTART,RLENGTH);if(A){print $0}}'  Input_file

EDIT: Adding one more solution on same.
Code:
grep '^ *,'  Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 01-28-2015 at 03:59 AM.. Reason: Added a more solution
# 6  
Old 01-28-2015
Thanks !!! Smilie
second awk and grep worked!!!

---------- Post updated at 04:07 AM ---------- Previous update was at 04:01 AM ----------

@anbu
sed solution worked well!!! Thanks a lot Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Tabbed multiple csv files into one single excel file with using shell script not perl

Hi Experts, I am querying backup status results for multiple databases and getting each and every database result in one csv file. so i need to combine all csv files in one excel file with separate tabs. I am not familiar with perl script so i am using shell script. Could anyone please... (4 Replies)
Discussion started by: ramakrk2
4 Replies

2. Shell Programming and Scripting

Replacing Multiple spaces with a single space but excluding few regular expressions

Hi All. Attached are two files. I ran a query and have the output as in the file with name "FILEWITHFOURRECORDS.txt " I didn't want all the spaces between the columns so I squeezed the spaces with the "tr" command and also added a carriage return at the end of every line. But in two... (3 Replies)
Discussion started by: sparks
3 Replies

3. Shell Programming and Scripting

Single Field to multiple fields searching

I have a fileA with one column (1000 rows), and fileB with 26 columns(13000 rows). I need to search each value of fileA with fileB and return all the 26 values from FileB to a new file- File C if matches. The search value (from FileA) may present in any of the 26 values in FileB. This value is not... (7 Replies)
Discussion started by: vamsikrishna928
7 Replies

4. Linux

How do I format a Date field of a .CSV file with multiple commas in a string field?

I have a .CSV file (file.csv) whose data are all enclosed in double quotes. Sample format of the file is as below: column1,column2,column3,column4,column5,column6, column7, Column8, Column9, Column10 "12","B000QRIGJ4","4432","string with quotes, and with a comma, and colon: in... (3 Replies)
Discussion started by: dhruuv369
3 Replies

5. UNIX for Dummies Questions & Answers

[SOLVED] splitting a single column(with spaces) into multiple rows

Hi All, My requisite is to split a single column of phonemes seperated by spaces into multiple rows. my input file is: a dh u th a qn ch A v U r k my o/p should be like: adhu a dh u (3 Replies)
Discussion started by: girlofgenuine
3 Replies

6. Shell Programming and Scripting

How to split file into multiple files using awk based on 1 field in the file?

Good day all I need some helps, say that I have data like below, each field separated by a tab DATE NAME ADDRESS 15/7/2012 LX a.b.c 15/7/2012 LX1 a.b.c 16/7/2012 AB a.b.c 16/7/2012 AB2 a.b.c 15/7/2012 LX2 a.b.c... (2 Replies)
Discussion started by: alexyyw
2 Replies

7. Shell Programming and Scripting

Awk match multiple columns in multiple lines in single file

Hi, Input 7488 7389 chr1.fa chr1.fa 3546 9887 chr5.fa chr9.fa 7387 7898 chrX.fa chr3.fa 7488 7389 chr21.fa chr3.fa 7488 7389 chr1.fa chr1.fa 3546 9887 chr9.fa chr5.fa 7898 7387 chrX.fa chr3.fa Desired Output 7488 7389 chr1.fa chr1.fa 2 3546 9887 chr5.fa chr9.fa 2... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

8. UNIX for Dummies Questions & Answers

How to translate multiple spaces into a single space using tr command?

I am trying to read a txt file and trying to translate multiples spaces into single spaces so the file is more organized, but whenever I try the command: tr ' ' ' ' w.txt The output is: tr: extra operand `w.txt' Try `tr --help' for more information. Can someone please help? :wall: ... (2 Replies)
Discussion started by: Nonito84
2 Replies

9. Shell Programming and Scripting

csv file field needs to be changed current system date with awk

HI, I have csv file with records as shown below. 4102,Bangalore,G10,21,08/17/2011 09:28:33:188,99,08/17/2011 09:27:33:881,08/17/2011... (1 Reply)
Discussion started by: raghavendra.nsn
1 Replies

10. Shell Programming and Scripting

How to avoid the truncating of multiple spaces into a single space while reading a line from a file?

consider the small piece of code while read line do echo $line done < example content of example file sadasdasdasdsa erwerewrwr ergdgdfgf rgerg erwererwr the output is like sadasdasdasdsa erwerewrwr ergdgdfgf rgerg erwererwr the... (4 Replies)
Discussion started by: Kesavan
4 Replies
Login or Register to Ask a Question