Detect blank spaces in a CSV file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Detect blank spaces in a CSV file
# 1  
Old 10-03-2016
Detect blank spaces in a CSV file

I'm monitoring a WLAN network to keep track of new SSIDs popping up. The SSIDs are stored along with the AP MAC address and a few other parameters in a CSV file. A typical line could look like this:

Code:
18:70:9f:e3:80:aa 10:11:15  MyNetwork  2437

Now, the problem is that some networks use SSIDs with blank spaces. Then it looks like this:

Code:
18:70:9f:e3:80:aa 10:11:15  My Wi-Fi Network  2437

Some SSIDs come with no displayed SSID at all (hidden). The post-processing of these files of course become a bit cumbersome as some of the lines have three columns, some four and some more (depending on how many words the SSID is built up from).

Does someone know a smart way of detecting the SSID string and remove the blank spaces? Each line ends with the channel number (2437 above) and I could of course re-order the position of the columns so that the SSID is placed in the last column.

As the result, I would for example want "My Wi-Fi Network" to be converted to "My_Wi-Fi_Network". Like this:

Code:
18:70:9f:e3:80:aa 10:11:15  My_Wi-Fi_Network  2437

Cheers & thanks!
Sam
# 2  
Old 10-03-2016
If it's not comma-separated, it's not CSV. You've got a flatfile.

Code:
awk '{ S=""; for(N=3; N<NF; N++) S=S "_" $N ; print $1, $2, substr(S,2), $NF }' inputfile

These 2 Users Gave Thanks to Corona688 For This Post:
# 3  
Old 10-04-2016
Hello Zooma,

Could you please try following too and let us know if this helps.
Let's say following is Input_file.
Code:
18:70:9f:e3:80:aa 10:11:15  My Wi-Fi Network  2437
18:70:9f:e3:80:aa 10:11:15  MyNetwork  2437

Following is the code as follows then.
Code:
awk 'NF>4{for(i=3;i<=NF-1;i++){S=S?S"_"$i:$i};print $1,$2,S,$NF;S="";next} 1'  Input_file

Output will be as follows.
Code:
18:70:9f:e3:80:aa 10:11:15 My_Wi-Fi_Network 2437
18:70:9f:e3:80:aa 10:11:15  MyNetwork  2437

Thanks,
R. Singh

Last edited by RavinderSingh13; 10-04-2016 at 01:18 AM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 10-04-2016
With any POSIX conforming shell, you could also do this entirely in the shell:
Code:
#!/bin/ksh
while IFS= read -r line
do	printf 'Processing %s\n' "$line"
	set -- $line
	ip=$1
	tm=$2
	shift 2
	SSID=
	while [ $# -gt 1 ]
	do	SSID=${SSID}_$1
		shift
	done
	SSID=${SSID#_}
	printf '%s %s %s %s\n' "$ip" "$tm" "$SSID" "$1"
done < file.csv

which, if file.csv contains:
Code:
18:70:9f:e3:80:aa 10:11:15 MyNetwork 2437
18:70:9f:e3:80:aa 10:11:15 My Wi-Fi Network 2438
18:70:9f:e3:80:aa 10:11:15 2439

produces the output:
Code:
Processing 18:70:9f:e3:80:aa 10:11:15 MyNetwork 2437
18:70:9f:e3:80:aa 10:11:15 MyNetwork 2437
Processing 18:70:9f:e3:80:aa 10:11:15 My Wi-Fi Network 2438
18:70:9f:e3:80:aa 10:11:15 My_Wi-Fi_Network 2438
Processing 18:70:9f:e3:80:aa 10:11:15 2439
18:70:9f:e3:80:aa 10:11:15  2439

These 2 Users Gave Thanks to Don Cragun For This Post:
# 5  
Old 10-04-2016
Corona688's suggestion (#2) did the trick! Thanks!

---------- Post updated at 08:55 AM ---------- Previous update was at 08:46 AM ----------

I also tried Dons and RavinderSinghs solutions, and they both seem to solve the problem. Thanks a lot for all help.

Cheers
Sam

Last edited by Zooma; 10-04-2016 at 09:47 AM.. Reason: Typo
This User Gave Thanks to Zooma For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Delete blank spaces and blank lines in a file

Hi Gurus, Somebody can say me how to delete blank spaces and blank lines in a file unix, please. Thank you for advanced. (10 Replies)
Discussion started by: systemoper
10 Replies

2. Shell Programming and Scripting

How to remove all blank spaces in a file

I have a file which contains data such as that shown below. How do i remove all the blcnak spaces, before, during and at the end of each line in one command? 300015, 58.0823212, 230.424728 300016, 58.2276459, 229.141602 300017, 58.7590027, 226.960846 ... (9 Replies)
Discussion started by: carlr
9 Replies

3. Shell Programming and Scripting

Help with removal of blank spaces in a file

Hello.. I have a text file. I want to remove all the blank spaces(except tab) from the file.. I tried using sed command as shown below sed 's/ //g' file1 But the problem with the above command is that it also eliminates 'tab' which is between the columns.. For example if the contents... (7 Replies)
Discussion started by: abk07
7 Replies

4. Shell Programming and Scripting

how to remove blank spaces in file

hi i have a file which store some data.the contents of my file is data1:data2 data3:data4 i have a script which read this file correct="$(cat /root/sh | cut -d: -f1)" i used this syntax..please help me which syntax is used to remove blank spaces..then how to read this file.. (1 Reply)
Discussion started by: shubhig15
1 Replies

5. Shell Programming and Scripting

Blank spaces missing in the file

I have a file which has blank spaces($) and the blank spaces at the end of the file are not coming up while cutting the files.. please find the following file, desired output.. file1: 001_AHaris$$$$$020$$$$$$$$$ 001_ATony$$$$$$030$$$$$$$$$ 002_AChris$$$$$090$$$$$$$$$... (5 Replies)
Discussion started by: techmoris
5 Replies

6. Shell Programming and Scripting

Removing blank spaces, tab spaces from file

Hello All, I am trying to remove all tabspaces and all blankspaces from my file using sed & awk, but not getting proper code. Please help me out. My file is like this (<b> means one blank space, <t> means one tab space)- $ cat file NARESH<b><b><b>KUMAR<t><t>PRADHAN... (3 Replies)
Discussion started by: NARESH1302
3 Replies

7. Shell Programming and Scripting

Remove blank spaces in a text file...

Hi, I have this problem that there are blank spaces in my text file... i want to remove them line 1 line 2 line 3 I want to remove the space between line 2 and line 3... I tried sed... it work but it prints the whole text file at the command prompt which i dont want.... sde i tried was... (4 Replies)
Discussion started by: bhagya2340
4 Replies

8. Shell Programming and Scripting

how to remove blank spaces of a file with awk??

hello how to remove blank spaces of a file with awk?? i´m trying awk '{gsub(" ","",$0); print $0;}' filename.txt but it answers syntax error near line first of all i did this for download from netbackup database jobs privilege bpdbjobs -report -M sv88 -gdm -header |cut -c-1024... (4 Replies)
Discussion started by: pabloli150
4 Replies

9. Shell Programming and Scripting

Removing blank spaces from a file?

Guys, I need some help... how can I remove the blank spaces between the lines below? (between the date and the hour fields) 21/05/07 00:05:00 99 21/05/07 00:10:01 99 21/05/07 00:15:00 99 21/05/07 00:20:00 99 21/05/07 00:25:00 99 I want to make the file... (4 Replies)
Discussion started by: dfs
4 Replies

10. UNIX for Dummies Questions & Answers

To remove Continous blank spaces from a file in UNIX

All... I want to remove blank spaces in file . I just leraned that we can use " cat <Input filename> | tr -s ‘ ‘ > <Target file name> " i also know with SED we can replace a blank space by other character by sed s/ /*/g filename. Please let me know how can i do that by... (1 Reply)
Discussion started by: arunkumar_mca
1 Replies
Login or Register to Ask a Question