[bash] - Replace blank and string in csv file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [bash] - Replace blank and string in csv file
# 1  
Old 01-16-2017
Linux [bash] - Replace blank and string in csv file

Hi all,
i have a .csv file with only two columns, like:
Code:
Login;Status
Luca;S
Marco;
Stefano;
Elettra;S
Laura;
...

I need to replace the blank space on Status column whit Enabled end, on the same column, S whit Disabled, like:
Code:
Login;Status
Luca;Disabled
Marco;Enabled
Stefano;Enabled
Elettra;Disabled
Laura;Enabled
...

I try with awk and sed, many errors results Smilie i was not familiar with bash... so i hope in your help.
Thank's all.
# 2  
Old 01-16-2017
Code:
awk -F';' 'FNR>1 {$2=($2)?"Disabled":"Enabled"}1' OFS=';' myFile

These 2 Users Gave Thanks to vgersh99 For This Post:
# 3  
Old 01-16-2017
Hello kamose,

If you have only 2 fields as shown in your sample Input_file then following may help you too.
Code:
awk -F";" '{printf("%s;%s\n",$1,($2==""?"Enabled":((NR==1)?$2:"Disabled")))}'  Input_file

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 01-16-2017
Another variation:
Code:
awk '$2=="S"{$2="Disabled"} $2==""{$2="Enabled"}1' FS=\; OFS=\; file

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 01-17-2017
bash
Code:
while IFS=";" read f1 f2 
do 
[[ $f2 == "S" ]] && echo "$f1;Disabled" || { echo "$f1;Enabled" ;}
done < file

This User Gave Thanks to looney For This Post:
# 6  
Old 01-17-2017
Quote:
Originally Posted by looney
bash
Code:
while IFS=";" read f1 f2 
do 
[[ $f2 == "S" ]] && echo "$f1;Disabled" || { echo "$f1;Enabled" ;}
done < file

Hi looney,
Note that the above code changes the first line of input:
Code:
Login;Status

to:
Code:
Login;Enabled

but the desired output showed that that line was not supposed to be changed.

To meet the stated requirements just using built-ins in most standard shells, you would need something more like:
Code:
while IFS=';' read -r f1 f2
do	[ "$f2" = "S" ] && f2=Disabled
	[ -z "$f2" ] && f2=Enabled
	printf '%s;%s\n' "$f1" "$f2"
done < file.csv

or:
Code:
while IFS=';' read -r f1 f2
do	[ "$f2" = "S" ] && f2=Disabled || { [ -z "$f2" ] && f2=Enabled; }
	printf '%s;%s\n' "$f1" "$f2"
done < file.csv


Last edited by Don Cragun; 01-17-2017 at 02:28 AM.. Reason: Add alternative.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 7  
Old 01-17-2017
Are all good solutions, I will keep good for many occasions. I have to decide whether to use awk or while. Maybe I will use awk because it's faster in terms of performance (for me), especially on very large file (over 5k lines).

Thanks to all for feedback.

---------- Post updated at 05:53 AM ---------- Previous update was at 04:20 AM ----------

Sorry guys,
but if in file have lines whit only ";" like:
Code:
Login;Status
;
;
Luca;Disabled 
Marco;Enabled 
Stefano;Enabled
;
Elettra;Disabled 
Laura;Enabled
;
;
...

how can I remove those lines?
I can not interpret the special character ";" to make awk and sed. Do you have idea how to do?

Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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: 18:70:9f:e3:80:aa 10:11:15 MyNetwork 2437 Now, the problem is that some networks use SSIDs... (4 Replies)
Discussion started by: Zooma
4 Replies

2. Shell Programming and Scripting

Assign variables to CSV string (bash)

Hi guys, New to the forum, and been messing around with Linux for about a year now. I'm still very much a rookie, so just assume that I'm a total idiot: I currently have a shell that spits out a CSV number string of about 8 numbers as follows: 1.00,2.00,3.00 ... ,8.00I need to assign a... (7 Replies)
Discussion started by: hansol
7 Replies

3. Shell Programming and Scripting

Inserting blank columns in already present CSV file

Hi, i have a csv file which have headers and values of it like below : headers --> CI Ref SerialNumber LastScanDate values --> VMware-42,VMware-42,Tue, 20 May 2014 11:03:44 +0000 i want to have a above csv in below format : headers --> CI Name CI Description CI Ref... (6 Replies)
Discussion started by: omkar.jadhav
6 Replies

4. Shell Programming and Scripting

Copying down first row in to all the below blank rows in a .csv file

Hi All, I have many of files(.csv) of the format given below. Date,Name,Location 04/02/2012,A,India ,B,China ,C,USA Like this I have 1000's of rows and many columns in all my files. I need a shell script to copy down the Date(in this example column1) to the next 2 rows below(in the... (8 Replies)
Discussion started by: ks_reddy
8 Replies

5. Shell Programming and Scripting

Text file to CSV with field data separated by blank lines

Hello, I have some data in a text file where fields are separated by blank lines. There are only 6 fields however some fields have several lines of data as I will explain. Also data in a particular field is not consistently the same size but does end on a blank line. The first field start with... (6 Replies)
Discussion started by: vestport
6 Replies

6. Shell Programming and Scripting

Replace 2nd column for each line in a csv file with fixed string+random number

Hi experts, My csv file looks like this U;cake;michael;temp;;;; U;bread;john;temp;;;; U;cocktails;sarah;temp;;;; I'd like to change the value fo 2nd column to cf+random number , which will look maybe something like this U;cf20187;michael;temp;;;; U;cf8926;john;temp;;;;... (7 Replies)
Discussion started by: tententen
7 Replies

7. Shell Programming and Scripting

How to remove Blank rows in a csv file

Hi, I need help to remove blank rows at the end of file. Sample data: "Oslo, Symra kino",Oslo,130-7,Symra 1,130-7-91 "Tønsberg, Brygga Kino SF",Tønsberg,202-1,Tønsberg SF 4,202-1-4 ,,,, ,,,, ,,,, ,,,, Expected data: "Oslo, Symra kino",Oslo,130-7,Symra 1,130-7-91 "Tønsberg, Brygga... (6 Replies)
Discussion started by: cnraja
6 Replies

8. Shell Programming and Scripting

Replace blank values for string using sed

Hi everyone, I was wondering how could from a file where each row is separated by tabulations, the row values where are in blank replace them by a string or value. My file has this form: 26/01/09 13:45:00 0 0 26/01/09 14:00:00 1495.601318 0 26/01/09 14:15:00 1495.601318 0 ... (4 Replies)
Discussion started by: tonet
4 Replies

9. Shell Programming and Scripting

how to replace a character with blank in a file

hi, I have a doubt in replacing characters with blank. My requirement is that, i have one file and looks like below 4:ALTER SYSTEM DISCONNECT SESSION '193,191' IMMEDIATE; 6:ALTER SYSTEM DISCONNECT SESSION '205,7274' IMMEDIATE; 5:ALTER SYSTEM DISCONNECT SESSION '206,34158' IMMEDIATE;... (4 Replies)
Discussion started by: sridhusha
4 Replies

10. Shell Programming and Scripting

Blank Space is not appending in each row of CSV File - Shell Script

I am calling SQL script in my UNIX Shell script and trying to create the CSV file and my last column value of each row is 23 blank spaces. In my SQL script,the last column is like below. RPAD(' ',23,' ') -- Padding 23 blank Spaces The CSV file is generated but the sapce(23 spaces) is... (2 Replies)
Discussion started by: praka
2 Replies
Login or Register to Ask a Question